-
Notifications
You must be signed in to change notification settings - Fork 2
/
grammar_actions.hpp
498 lines (385 loc) · 13.7 KB
/
grammar_actions.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
#ifndef BASIC_INT_GRAMMAR_ACTIONS_H
#define BASIC_INT_GRAMMAR_ACTIONS_H
#include "runtime.h"
namespace actions
{
using boost::fusion::at_c;
using runtime::str_t;
using runtime::int_t;
using runtime::float_t;
template< class CtxT >
auto GetPos( const CtxT &ctx )
{
namespace x3 = boost::spirit::x3;
const auto res = x3::_where( ctx ).begin() - x3::get<runtime::line_begin_tag>( ctx );
assert( res >= 0 );
return static_cast<unsigned>(res);
}
constexpr auto cpy_op = []( auto& ctx )
{
_val( ctx ) = _attr( ctx );
};
constexpr auto append_op = []( auto& ctx )
{
auto& op1 = _val( ctx );
auto&& op2 = _attr( ctx );
op1 += op2;
};
constexpr auto append_idx_op = []( auto& ctx )
{
auto& op1 = _val( ctx );
auto&& op2 = _attr( ctx );
const auto idx = ForceInt( op2 );
op1 += std::to_string( idx );
};
constexpr auto cpy_int_op = []( auto& ctx )
{
_val( ctx ) = static_cast<int_t>(_attr( ctx ));
};
constexpr auto force_int_op = []( auto& ctx )
{
_val( ctx ) = ForceInt(_attr( ctx ));
};
constexpr auto eq_op = []( auto& ctx )
{
auto& op1 = _val( ctx );
auto&& op2 = _attr( ctx );
const str_t* pS1 = boost::get<str_t>( &op1 );
const str_t* pS2 = boost::get<str_t>( &op2 );
op1 = int_t{ pS1 && pS2 ?
*pS1 == *pS2 :
ForceFloat( op1 ) == ForceFloat( op2 )
};
};
constexpr auto not_eq_op = []( auto& ctx )
{
auto& op1 = _val( ctx );
auto&& op2 = _attr( ctx );
const str_t* pS1 = boost::get<str_t>( &op1 );
const str_t* pS2 = boost::get<str_t>( &op2 );
op1 = int_t{ pS1 && pS2 ?
*pS1 != *pS2 :
ForceFloat( op1 ) != ForceFloat( op2 )
};
};
constexpr auto add_op = []( auto& ctx )
{
auto& op1 = _val( ctx );
auto&& op2 = _attr( ctx );
op1 = AddImpl( op1, op2 );
};
constexpr auto sub_op = []( auto& ctx )
{
_val( ctx ) = ForceFloat( _val( ctx ) ) - ForceFloat( _attr( ctx ) );
};
constexpr auto mul_op = []( auto& ctx )
{
_val( ctx ) = ForceFloat( _val( ctx ) ) * ForceFloat( _attr( ctx ) );
};
constexpr auto div_op = []( auto& ctx )
{
_val( ctx ) = ForceFloat( _val( ctx ) ) / ForceFloat( _attr( ctx ) );
};
constexpr auto exp_op = []( auto& ctx )
{
_val( ctx ) = std::pow( ForceFloat( _val( ctx ) ), ForceFloat( _attr( ctx ) ) );
};
constexpr auto not_op = []( auto& ctx )
{
_val( ctx ) = int_t{ !ForceInt( _attr( ctx ) ) };
};
constexpr auto neg_op = []( auto& ctx )
{
_val( ctx ) = -ForceFloat( _attr( ctx ) ); };
const auto less_op = []( auto& ctx )
{
_val( ctx ) = int_t{ ForceFloat( _val( ctx ) ) < ForceFloat( _attr( ctx ) ) };
};
constexpr auto greater_op = []( auto& ctx )
{
_val( ctx ) = int_t{ ForceFloat( _val( ctx ) ) > ForceFloat( _attr( ctx ) ) };
};
constexpr auto less_eq_op = []( auto& ctx )
{
_val( ctx ) = LessEqImpl( _val( ctx ), _attr( ctx ) );
};
constexpr auto greater_eq_op = []( auto& ctx )
{
_val( ctx ) = int_t{ ForceFloat( _val( ctx ) ) >= ForceFloat( _attr( ctx ) ) };
};
constexpr auto and_op = []( auto& ctx )
{
_val( ctx ) = int_t{ ForceInt( _val( ctx ) ) && ForceInt( _attr( ctx ) ) };
};
constexpr auto or_op = []( auto& ctx )
{
_val( ctx ) = int_t{ ForceInt( _val( ctx ) ) || ForceInt( _attr( ctx ) ) };
};
str_t SubStrImpl( const str_t& str, int_t pos, int_t count = -1 )
{
--pos;
if( pos < 0 )
pos = 0;
else if( static_cast<size_t>(pos) > str.size() )
return str_t{};
return str.substr( pos, count );
}
constexpr auto left_op = []( auto& ctx ) {
auto&& [o1, o2] = _attr( ctx );
auto&& str = ForceStr( o1 );
auto&& count = ForceInt( o2 );
_val( ctx ) = SubStrImpl( str, 1, count );
};
constexpr auto mid_op = []( auto& ctx ) {
auto&& [o1, o2, o3] = _attr( ctx );
auto&& str = ForceStr( o1 );
auto&& pos = ForceInt( o2 );
auto&& count = ForceInt( o3 );
_val( ctx ) = SubStrImpl( str, pos, count );
};
constexpr auto mid2_op = []( auto& ctx ) {
auto&& [o1, o2] = _attr( ctx );
auto&& str = ForceStr( o1 );
auto&& pos = ForceInt( o2 );
_val( ctx ) = SubStrImpl( str, pos );
};
constexpr auto right_op = []( auto& ctx ) {
auto&& [o1, o2] = _attr( ctx );
auto&& str = ForceStr( o1 );
auto&& count = ForceInt( o2 );
_val( ctx ) = SubStrImpl( str, static_cast<int_t>(str.length()) - count + 1, count );
};
constexpr auto str_op = []( auto& ctx ) {
auto&& v = _attr( ctx );
_val( ctx ) = ToStrImpl(v );
};
constexpr auto val_op = []( auto& ctx ) {
auto&& arg = _attr( ctx );
auto&& s = ForceStr( arg );
char* ending = nullptr;
const float res = std::strtof( s.c_str(), &ending );
_val( ctx ) = *ending == '\0' ? res : float_t{0};
};
constexpr auto len_op = []( auto& ctx ) {
auto&& arg = _attr( ctx );
_val( ctx ) = static_cast<int_t>( ForceStr( arg ).length() );
};
constexpr auto asc_op = []( auto& ctx ) {
auto&& arg = _attr( ctx );
auto&& s = ForceStr( arg );
if( s.empty() )
throw std::runtime_error( "Illegal ASC() call" );
_val( ctx ) = int_t{ s[0] };
};
constexpr auto chr_op = []( auto& ctx ) {
auto&& arg = _attr( ctx );
auto&& ch = ForceInt( arg );
_val( ctx ) = str_t( 1, static_cast<char>(ch) );
};
constexpr auto sqr_op = []( auto& ctx ) {
const auto& v = _attr( ctx );
_val( ctx ) = std::sqrt( ForceFloat( v ) );
};
constexpr auto int_op = []( auto& ctx ) {
const auto& v = _attr( ctx );
float_t val = ForceFloat( v );
if ( val < 0 )
val -= 0.5f;
_val( ctx ) = static_cast<int_t>(val);
};
constexpr auto abs_op = []( auto& ctx ) {
const auto& v = _attr( ctx );
_val( ctx ) = std::fabs( ForceFloat( v ) );
};
constexpr auto rnd_op = []( auto& ctx ) {
const auto v = ForceFloat(_attr( ctx ));
if( v <= FLT_EPSILON )
throw std::runtime_error("Only positive arguments of RND are supported");
_val( ctx ) = v * std::rand() / (RAND_MAX + 1);
};
constexpr auto inkey_op = []( auto& ctx ) {
auto& runtime = x3::get<runtime_tag>( ctx ).get();
_val( ctx ) = runtime.Inkey();
};
constexpr auto print_op = []( auto& ctx )
{
auto& runtime = x3::get<runtime_tag>( ctx ).get();
boost::apply_visitor( [&runtime]( auto&& v ) { runtime.Print( v ); }, _attr( ctx ) );
};
constexpr auto print_tab_op = []( auto& ctx )
{
const auto& v = _attr( ctx );
auto& runtime = x3::get<runtime_tag>( ctx ).get();
runtime.Print( str_t( ForceInt( v ), ' ' ) );
};
constexpr auto assing_var_op = []( auto& ctx ) {
auto&& v = _attr( ctx );
auto&& name = at_c<0>( v );
auto&& value = at_c<1>( v );
auto& runtime = x3::get<runtime_tag>( ctx ).get();
runtime.Store( name, value );
};
constexpr auto load_var_op = []( auto& ctx ) {
auto&& name = _attr( ctx );
auto& runtime = x3::get<runtime_tag>( ctx ).get();
_val( ctx ) = runtime.Load( std::move( name ) );
};
constexpr auto read_stmt_op = []( auto& ctx ) {
auto&& name = _attr( ctx );
auto& runtime = x3::get<runtime_tag>( ctx ).get();
runtime.Read( std::move( name ) );
};
constexpr auto randomize_stmt_op = []( auto& ctx ) {
auto&& val = _attr( ctx );
auto& runtime = x3::get<runtime_tag>( ctx ).get();
runtime.Randomize( ForceInt(val) );
};
constexpr auto if_stmt_op = []( auto& ctx ) {
auto&& v = _attr( ctx );
auto&& cond = at_c<0>( v );
auto&& gotoLine = at_c<1>( v );
const bool res = ToBoolImpl( cond );
auto& runtime = x3::get<runtime_tag>( ctx ).get();
auto& parseMode = x3::get<parse_mode_tag>( ctx ).get();
if( !res )
{
parseMode = gotoLine == MaxLineNum ?
ParseMode::SkipStatementParseElse:
ParseMode::ParseElse;
return;
}
if( gotoLine == MaxLineNum )
parseMode = ParseMode::ParseStatementSkipElse;
else
{
runtime.Goto( gotoLine );
//Only needed when `runtime` is the skipping one
parseMode = ParseMode::SkipElse;
}
};
constexpr auto else_stmt_op = []( auto& ctx ) {
auto&& gotoLine = _attr( ctx );
auto& runtime = x3::get<runtime_tag>( ctx ).get();
runtime.Goto( gotoLine );
};
constexpr auto on_stmt_impl_op = []( auto& ctx ) {
auto&& v = _attr( ctx );
const auto num = ForceInt( at_c<0>( v ) );
auto&& lines = at_c<1>( v );
if( num <= 0 || (size_t)num > lines.size() )
throw std::runtime_error( "ON statement incorrect branch #" + std::to_string(num) );
return lines[num - 1];
};
constexpr auto on_goto_stmt_op = []( auto& ctx ) {
auto& runtime = x3::get<runtime_tag>( ctx ).get();
runtime.Goto( on_stmt_impl_op(ctx) );
};
constexpr auto on_gosub_stmt_op = []( auto& ctx ) {
auto& runtime = x3::get<runtime_tag>( ctx ).get();
const auto lineOffset = GetPos( ctx );
runtime.Gosub( on_stmt_impl_op( ctx ), lineOffset );
};
constexpr auto goto_stmt_op = []( auto& ctx ) {
auto&& v = _attr( ctx );
auto& runtime = x3::get<runtime_tag>( ctx ).get();
runtime.Goto( v );
};
constexpr auto end_stmt_op = []( auto& ctx ) {
auto& runtime = x3::get<runtime_tag>( ctx ).get();
runtime.Goto( MaxLineNum );
};
constexpr auto stop_stmt_op = []( auto& ctx ) {
end_stmt_op(ctx);
};
constexpr auto restore_stmt_op = []( auto& ctx ) {
auto& runtime = x3::get<runtime_tag>( ctx ).get();
auto&& gotoLine = _attr( ctx );
if( gotoLine == MaxLineNum )
runtime.Restore();
else
runtime.Restore( gotoLine );
};
constexpr auto gosub_stmt_op = []( auto& ctx ) {
auto&& v = _attr( ctx );
auto& runtime = x3::get<runtime_tag>( ctx ).get();
const auto lineOffset = GetPos(ctx);
runtime.Gosub( v, lineOffset );
};
constexpr auto return_stmt_op = []( auto& ctx ) {
auto&& v = _attr( ctx );
auto& runtime = x3::get<runtime_tag>( ctx ).get();
runtime.Return();
};
constexpr auto for_stmt_op = []( auto& ctx ) {
auto&& v = _attr( ctx );
auto&& name = at_c<0>( v );
auto&& initVal = at_c<1>( v );
auto&& endVal = at_c<2>( v );
auto&& step = at_c<3>( v );
auto& runtime = x3::get<runtime_tag>( ctx ).get();
const auto lineOffset = GetPos( ctx );
runtime.ForLoop( std::move(name), std::move(initVal), std::move(endVal),
step ? std::move( *step ) : value_t{ int_t{1} }, lineOffset );
};
constexpr auto def_stmt_op = []( auto& ctx ) {
auto&& v = _attr( ctx );
auto&& fncName = at_c<0>( v );
auto&& varName = at_c<1>( v );
auto&& exprStr = at_c<2>( v );
auto& runtime = x3::get<runtime_tag>( ctx ).get();
runtime.DefineFuntion( std::move(fncName), std::move(varName), std::move(exprStr) );
};
constexpr auto call_fn_op = []( auto& ctx ) {
auto&& v = _attr( ctx );
auto&& fncName = at_c<0>( v );
auto&& arg = at_c<1>( v );
auto& runtime = x3::get<runtime_tag>( ctx ).get();
_val( ctx ) = runtime.CallFuntion( std::move(fncName), std::move(arg) );
};
constexpr auto next_stmt_op = []( auto& ctx ) {
auto&& v = _attr( ctx );
auto& runtime = x3::get<runtime_tag>( ctx ).get();
runtime.Next( std::move(v) );
};
constexpr auto dim_stmt_op = []( auto& ctx ) {
auto&& v = _attr( ctx );
auto&& varName = at_c<0>( v );
auto&& dimension = at_c<1>( v );
auto& runtime = x3::get<runtime_tag>( ctx ).get();
runtime.Dim( std::move(varName), std::move( dimension) );
};
constexpr auto input_op = []( auto& ctx ) {
auto&& v = _attr( ctx );
auto&& prompt = at_c<0>( v );
auto&& varName = at_c<1>( v );
auto& runtime = x3::get<runtime_tag>( ctx ).get();
runtime.Input( prompt, varName );
};
constexpr auto add_num_line_op = []( auto& ctx )
{
auto& runtime = x3::get<runtime_tag>( ctx ).get();
auto&& v = _attr( ctx );
auto&& line = at_c<0>( v );
auto&& str = at_c<1>( v );
runtime.AddLine( line, str );
};
constexpr auto append_line_op = []( auto& ctx )
{
auto& runtime = x3::get<runtime_tag>( ctx ).get();
auto&& str = _attr( ctx );
runtime.AppendToPrevLine( str );
};
constexpr auto update_cur_line_op = []( auto& ctx )
{
auto& runtime = x3::get<runtime_tag>( ctx ).get();
auto&& line = _attr( ctx );
runtime.UpdateCurParseLine( line );
};
constexpr auto data_op = []( auto& ctx )
{
auto& runtime = x3::get<runtime_tag>( ctx ).get();
auto&& v = _attr( ctx );
runtime.AddData( std::move( v ) );
};
}
#endif // BASIC_INT_GRAMMAR_ACTIONS_H