-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcompiler.cpp
1633 lines (1449 loc) · 48.7 KB
/
compiler.cpp
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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
////////////////////////////////////////////////////////////////////////////////
// DScript Scripting Language
// Copyright (C) 2003 Bryan "daerid" Ross
//
// Permission to copy, use, modify, sell and distribute this software is
// granted provided this copyright notice appears in all copies. This
// software is provided "as is" without express or implied warranty, and
// with no claim as to its suitability for any purpose.
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// For debugging the parser. Undefine to track the parser's progress
// See documentation at http://spirit.sourceforge.net
// #define BOOST_SPIRIT_DEBUG
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// Spirit Parser Framework Includes
#include <boost/spirit/home/classic.hpp>
#include <boost/spirit/home/classic/tree/parse_tree.hpp>
#include <boost/spirit/home/classic/tree/parse_tree_utils.hpp>
#include <boost/spirit/home/classic/tree/tree_to_xml.hpp>
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// Standard Library Includes
#include <stack>
#include <fstream>
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// DScript Includes
#include "compiler.h"
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// Use some namespaces to avoid wrist injury :)
using namespace std;
using namespace dscript;
using namespace boost::spirit::classic;
////////////////////////////////////////////////////////////////////////////////
namespace dscript
{
/// This struct contains all the information that the compiler
/// needs to convert a parse tree into a codeblock
struct compile_context
{
compile_context(
string_table& _strings,
float_table& _floats
) : strings(_strings), floats(_floats)
{
}
string_table& strings;
float_table& floats;
size_t func_depth;
size_t loop_count;
stack< stack<size_t> > break_indices;
stack< stack<size_t> > continue_indices;
codeblock_t code; // get the count by calling size()
};
////////////////////////////////////////////////////////////////////////////////
// THE SPIRIT PARSER FOR DSCRIPT
////////////////////////////////////////////////////////////////////////////////
/// This is the skip grammar. This grammar defines the language that
/// the parser will use to skip white space and others
struct skip_grammar : public boost::spirit::classic::grammar<skip_grammar>
{
template<typename ScanT>
struct definition
{
definition(const skip_grammar& self)
{
skip
= space_p
| comment_p("//")
;
debug();
}
rule<ScanT> skip;
void debug()
{
#ifdef BOOST_SPIRIT_DEBUG
BOOST_SPIRIT_DEBUG_RULE(skip);
#endif
}
const rule<ScanT>& start()
{
return skip;
}
};
};
/// This enum specifies all the node ids that the parse tree will generate
/// if the code is parsed correctly.
enum parser_ids
{
stmt_list_id,
stmt_block_id,
stmt_id,
func_call_id,
ident_id,
expr_id,
func_decl_id,
var_id,
lvar_id,
gvar_id,
constant_id,
flt_const_id,
int_const_id,
str_const_id,
return_stmt_id,
assign_stmt_id,
assign_op_id,
logical_op_id,
bitwise_expr_id,
expr_atom_id,
bitwise_op_id,
equality_expr_id,
equality_op_id,
compare_expr_id,
compare_op_id,
shift_expr_id,
shift_op_id,
add_expr_id,
add_op_id,
mul_expr_id,
mul_op_id,
unary_expr_id,
unary_op_id,
if_stmt_id,
while_stmt_id,
for_stmt_id,
break_stmt_id,
continue_stmt_id,
inc_dec_expr_id,
inc_dec_stmt_id,
inc_dec_op_id,
aidx_id,
num_rule_ids
};
/// This is the actual language specification for DScript
struct grammar : public boost::spirit::classic::grammar<grammar>
{
template<typename ScanT>
struct definition
{
rule<ScanT, parser_tag<stmt_list_id> > stmt_list;
rule<ScanT, parser_tag<stmt_block_id> > stmt_block;
rule<ScanT, parser_tag<stmt_id> > stmt;
rule<ScanT, parser_tag<func_call_id> > func_call;
rule<ScanT, parser_tag<ident_id> > ident;
rule<ScanT, parser_tag<expr_id> > expr;
rule<ScanT, parser_tag<func_decl_id> > func_decl;
rule<ScanT, parser_tag<var_id> > var;
rule<ScanT, parser_tag<lvar_id> > lvar;
rule<ScanT, parser_tag<gvar_id> > gvar;
rule<ScanT, parser_tag<constant_id> > constant;
rule<ScanT, parser_tag<flt_const_id> > flt_const;
rule<ScanT, parser_tag<int_const_id> > int_const;
rule<ScanT, parser_tag<str_const_id> > str_const;
rule<ScanT, parser_tag<return_stmt_id> > return_stmt;
rule<ScanT, parser_tag<assign_stmt_id> > assign_stmt;
rule<ScanT, parser_tag<assign_op_id> > assign_op;
rule<ScanT, parser_tag<bitwise_expr_id> > bitwise_expr;
rule<ScanT, parser_tag<expr_atom_id> > expr_atom;
rule<ScanT, parser_tag<bitwise_op_id> > bitwise_op;
rule<ScanT, parser_tag<equality_expr_id> > equality_expr;
rule<ScanT, parser_tag<equality_op_id> > equality_op;
rule<ScanT, parser_tag<compare_expr_id> > compare_expr;
rule<ScanT, parser_tag<compare_op_id> > compare_op;
rule<ScanT, parser_tag<logical_op_id> > logical_op;
rule<ScanT, parser_tag<shift_expr_id> > shift_expr;
rule<ScanT, parser_tag<shift_op_id> > shift_op;
rule<ScanT, parser_tag<add_expr_id> > add_expr;
rule<ScanT, parser_tag<add_op_id> > add_op;
rule<ScanT, parser_tag<mul_expr_id> > mul_expr;
rule<ScanT, parser_tag<mul_op_id> > mul_op;
rule<ScanT, parser_tag<unary_expr_id> > unary_expr;
rule<ScanT, parser_tag<unary_op_id> > unary_op;
rule<ScanT, parser_tag<if_stmt_id> > if_stmt;
rule<ScanT, parser_tag<while_stmt_id> > while_stmt;
rule<ScanT, parser_tag<for_stmt_id> > for_stmt;
rule<ScanT, parser_tag<break_stmt_id> > break_stmt;
rule<ScanT, parser_tag<continue_stmt_id> > continue_stmt;
rule<ScanT, parser_tag<inc_dec_op_id> > inc_dec_op;
rule<ScanT, parser_tag<inc_dec_expr_id> > inc_dec_expr;
rule<ScanT, parser_tag<inc_dec_stmt_id> > inc_dec_stmt;
rule<ScanT, parser_tag<aidx_id> > aidx;
void debug()
{
#ifdef BOOST_SPIRIT_DEBUG
//// *E DBG*
BOOST_SPIRIT_DEBUG_RULE(stmt_list);
BOOST_SPIRIT_DEBUG_RULE(stmt_block);
BOOST_SPIRIT_DEBUG_RULE(stmt);
BOOST_SPIRIT_DEBUG_RULE(func_call);
BOOST_SPIRIT_DEBUG_RULE(ident);
BOOST_SPIRIT_DEBUG_RULE(expr);
BOOST_SPIRIT_DEBUG_RULE(func_decl);
BOOST_SPIRIT_DEBUG_RULE(var);
BOOST_SPIRIT_DEBUG_RULE(lvar);
BOOST_SPIRIT_DEBUG_RULE(gvar);
BOOST_SPIRIT_DEBUG_RULE(constant);
BOOST_SPIRIT_DEBUG_RULE(flt_const);
BOOST_SPIRIT_DEBUG_RULE(int_const);
BOOST_SPIRIT_DEBUG_RULE(str_const);
BOOST_SPIRIT_DEBUG_RULE(return_stmt);
BOOST_SPIRIT_DEBUG_RULE(assign_stmt);
BOOST_SPIRIT_DEBUG_RULE(assign_op);
BOOST_SPIRIT_DEBUG_RULE(bitwise_expr);
BOOST_SPIRIT_DEBUG_RULE(expr_atom);
BOOST_SPIRIT_DEBUG_RULE(bitwise_op);
BOOST_SPIRIT_DEBUG_RULE(equality_expr);
BOOST_SPIRIT_DEBUG_RULE(equality_op);
BOOST_SPIRIT_DEBUG_RULE(compare_expr);
BOOST_SPIRIT_DEBUG_RULE(compare_op);
BOOST_SPIRIT_DEBUG_RULE(logical_op);
BOOST_SPIRIT_DEBUG_RULE(shift_expr);
BOOST_SPIRIT_DEBUG_RULE(shift_op);
BOOST_SPIRIT_DEBUG_RULE(add_expr);
BOOST_SPIRIT_DEBUG_RULE(add_op);
BOOST_SPIRIT_DEBUG_RULE(mul_expr);
BOOST_SPIRIT_DEBUG_RULE(mul_op);
BOOST_SPIRIT_DEBUG_RULE(unary_expr);
BOOST_SPIRIT_DEBUG_RULE(unary_op);
BOOST_SPIRIT_DEBUG_RULE(if_stmt);
BOOST_SPIRIT_DEBUG_RULE(while_stmt);
BOOST_SPIRIT_DEBUG_RULE(for_stmt);
BOOST_SPIRIT_DEBUG_RULE(inc_dec_op);
BOOST_SPIRIT_DEBUG_RULE(inc_dec_expr);
BOOST_SPIRIT_DEBUG_RULE(inc_dec_stmt);
BOOST_SPIRIT_DEBUG_RULE(aidx);
//// *E DBG*
#endif
}
definition(const dscript::grammar& self)
{
stmt_list
= *stmt
;
stmt_block
= inner_node_d[
'{' >> *stmt >> '}'
]
;
stmt
= func_call >> discard_node_d[ ch_p(';') ]
| func_decl
| return_stmt >> discard_node_d[ ch_p(';') ]
| assign_stmt >> discard_node_d[ ch_p(';') ]
| stmt_block
| if_stmt
| while_stmt
| break_stmt >> discard_node_d[ ch_p(';') ]
| continue_stmt >> discard_node_d[ ch_p(';') ]
| for_stmt
| ';' // null statement
;
return_stmt
= str_p("return") >> !expr
;
break_stmt
= str_p("break")
;
continue_stmt
= str_p("continue")
;
assign_stmt
= var >> assign_op >> expr
| inc_dec_stmt
;
inc_dec_stmt
= inc_dec_op >> var
| var >> inc_dec_op
;
inc_dec_op
= str_p("++")
| "--"
;
if_stmt
= discard_node_d[ str_p("if") ] >> inner_node_d[ '(' >> expr >> ')' ]
>> stmt
>> !(
discard_node_d[ str_p("else") ] >> stmt
)
;
while_stmt
= discard_node_d[ str_p("while") ] >> inner_node_d[ '(' >> expr >> ')' ]
>> stmt
;
for_stmt
= discard_node_d[ str_p("for") >> '(' ]
>> !assign_stmt >> ch_p(';') >>
!expr >> ch_p(';') >>
!assign_stmt
>> discard_node_d[ ch_p(')') ]
>> stmt
;
assign_op
= longest_d[
ch_p('=')
| "*="
| "/="
| "%="
| "+="
| "-="
| "&="
| "|="
| "^="
| "@="
| "<<="
| ">>="
]
;
func_call
= ident >> discard_node_d[ ch_p('(') ]
>> infix_node_d[ !list_p(expr,',') ]
>> discard_node_d[ ch_p(')') ]
;
ident
= lexeme_d[
token_node_d[
alpha_p >> *(alnum_p | '_' | ':')
]
]
;
expr
= bitwise_expr >> *(logical_op >> bitwise_expr)
;
logical_op
= str_p("||")
| "&&"
;
bitwise_expr
= equality_expr >> *(bitwise_op >> equality_expr)
;
bitwise_op
= ch_p('&')
| '|'
| '^'
;
equality_expr
= compare_expr >> *(equality_op >> compare_expr)
;
equality_op
= str_p("==")
| "!="
;
compare_expr
= shift_expr >> *(compare_op >> shift_expr)
;
compare_op
= longest_d[
ch_p('<')
| '>'
| "<="
| ">="
]
;
shift_expr
= add_expr >> *(shift_op >> add_expr)
;
shift_op
= str_p("<<")
| ">>"
;
add_expr
= mul_expr >> *(add_op >> mul_expr)
;
add_op
= ch_p('+')
| '-'
| '@'
;
mul_expr
= unary_expr >> *(mul_op >> unary_expr)
;
mul_op
= ch_p('*')
| '/'
| '%'
;
unary_expr
= inc_dec_expr
| !unary_op >> expr_atom
;
inc_dec_expr
= inc_dec_op >> var
| var >> inc_dec_op
;
unary_op
= ch_p('!')
| '+'
| '-'
| '~'
;
expr_atom
= constant
| var
| inner_node_d[ '(' >> expr >> ')' ]
| func_call
;
constant
= flt_const
| int_const
| str_const
;
flt_const
= strict_real_p
;
int_const
= lexeme_d[
token_node_d[
"0x" >> uint_parser<unsigned, 16, 1, 8>()
]
]
| int_p
;
str_const
= lexeme_d[
token_node_d[
'"' >> *(c_escape_ch_p - '"') >> '"'
]
]
;
gvar
= lexeme_d[
token_node_d[
ch_p('$') >> alpha_p >> *(alnum_p | '_' | ':')
]
]
;
lvar
= lexeme_d[
token_node_d[
ch_p('%') >> alpha_p >> *(alnum_p | '_' | ':')
]
]
;
var
= (gvar | lvar)
>> !inner_node_d[ '[' >> aidx >> ']' ]
;
aidx
= infix_node_d[ list_p(expr,',') ]
;
func_decl
= discard_node_d[ str_p("function") ] >> ident
>> discard_node_d[ ch_p('(') ]
>> infix_node_d[ !list_p(lvar,',') ]
>> discard_node_d[ ch_p(')') ]
>> stmt_block
;
debug();
}
const rule<ScanT,parser_tag<stmt_list_id> >& start()
{
return stmt_list;
}
};
};
////////////////////////////////////////////////////////////////////////////////
// The DScript compiler framework.
//
// These functions take the parse tree generated by spirit's pt_parse function
// and use the information in it to create the codeblock, which is akin to
// assembly language.
////////////////////////////////////////////////////////////////////////////////
template<typename TreeIterT>
struct compile_error : public std::runtime_error
{
compile_error(const std::string& msg,TreeIterT _node) : runtime_error(msg),node(_node) {}
TreeIterT node;
};
template<typename TreeIterT>
void compile_func_call(const TreeIterT& iter, compile_context& ctx)
{
assert(iter->value.id() == func_call_id);
// Are there params to be pushed?
if(iter->children.size() > 1)
{
TreeIterT expr = iter->children.end();
--expr; // last first (push args from right to left
assert(expr->value.id() == expr_id);
TreeIterT end = iter->children.begin();
for(; expr != end; --expr)
{
assert(expr->value.id() == expr_id);
// compile the expression
compile_expr(expr,ctx);
// and push it onto the param stop
// (popping it from the runtime stack)
ctx.code.push_back(op_push_param);
}
}
// first node is the identifier
const typename TreeIterT::value_type& ident = get_first_leaf(*iter);
assert(ident.value.id() == ident_id);
// Call the function
ctx.code.push_back(op_call_func);
string name(ident.value.begin(),ident.value.end());
ctx.code.push_back(ctx.strings.insert(name));
}
template<typename TreeIterT>
void compile_constant(const TreeIterT& iter, compile_context& ctx)
{
assert(iter->value.id() == constant_id);
// the child holds the actual type of constant
const typename TreeIterT::value_type& lit = get_first_leaf(*iter);
string val(lit.value.begin(),lit.value.end());
switch(lit.value.id().to_long())
{
case str_const_id:
// push a string constant
val = unescape(val);
ctx.code.push_back(op_push_str);
ctx.code.push_back(ctx.strings.insert(val));
break;
case int_const_id:
// push an integer constant
{
stringstream i;
int ival;
i << val;
if(val.length() > 2 && val.substr(0,2) == "0x")
i >> hex >> ival;
else
i >> ival;
ctx.code.push_back(op_push_int);
ctx.code.push_back(ival);
}
break;
case flt_const_id:
// push a float constant
{
stringstream d;
double dval;
d << val;
d >> dval;
ctx.code.push_back(op_push_float);
ctx.code.push_back(ctx.floats.insert(dval));
}
break;
default:
throw compile_error<TreeIterT>("Unknown constant node type",iter);
break;
};
}
template<typename TreeIterT>
void compile_aidx(const TreeIterT& iter, compile_context& ctx)
{
assert(iter->value.id() == aidx_id);
TreeIterT expr = iter->children.begin();
TreeIterT end = iter->children.end();
for( ; expr != end; ++expr )
{
compile_expr(expr,ctx);
// push the cat_aidx op
ctx.code.push_back(op_cat_aidx_expr);
}
}
template<typename TreeIterT>
void compile_var(const TreeIterT& iter, compile_context& ctx)
{
assert(iter->value.id() == var_id);
if(iter->children.size() == 1)
{
// push the variable's value
typedef typename TreeIterT::value_type node_t;
const node_t& leaf = get_first_leaf(*iter);
string token(leaf.value.begin(),leaf.value.end());
ctx.code.push_back(op_push_var);
ctx.code.push_back(ctx.strings.insert(token));
}
else
{
assert(iter->children.size() == 2);
TreeIterT child = iter->children.begin();
// the prefix
typedef typename TreeIterT::value_type node_t;
const node_t& leaf = get_first_leaf(*child);
string pref_token(leaf.value.begin(),leaf.value.end());
// push the name of the variable as a string
ctx.code.push_back(op_push_str);
ctx.code.push_back(ctx.strings.insert(pref_token));
// move on to the array index
++child;
// and compile it
compile_aidx(child,ctx);
// this op takes the name of the variable on the top
// of the stack, and replaces it with the value
// of that variable
ctx.code.push_back(op_push_var_value);
}
}
template<typename TreeIterT>
void compile_expr_atom(const TreeIterT& iter, compile_context& ctx)
{
assert(iter->value.id() == expr_atom_id);
// gonna be a constant, var, or expr
// no children
assert(iter->children.size() == 1);
// the atom!
const TreeIterT& atom = iter->children.begin();
switch(atom->value.id().to_long())
{
case var_id:
compile_var(atom,ctx);
break;
case constant_id:
compile_constant(atom,ctx);
break;
case expr_id:
compile_expr(atom,ctx);
break;
case func_call_id:
compile_func_call(atom,ctx);
// load the return value to the top of the stack
ctx.code.push_back(op_load_ret);
break;
default:
throw compile_error<TreeIterT>("Unknown expr_atom node",iter);
}
}
template<typename TreeIterT>
void compile_inc_dec_expr(const TreeIterT& iter, compile_context& ctx)
{
assert(iter->value.id() == inc_dec_expr_id);
assert(iter->children.size() == 2); // op and var
// get a node type
typedef typename TreeIterT::value_type node_t;
// post or pre inc/dec?
if(iter->children.begin()->value.id() == var_id)
{
// post
assert((iter->children.begin() + 1)->value.id() == inc_dec_op_id);
const node_t& op = get_first_leaf(*(iter->children.begin()+1));
const node_t& var = get_first_leaf(*(iter->children.begin()));
string op_token(op.value.begin(),op.value.end());
string var_token(var.value.begin(),var.value.end());
compile_var(iter->children.begin(),ctx);
if(op_token[0] == '+') // increment
ctx.code.push_back(op_inc_var);
else
ctx.code.push_back(op_dec_var);
ctx.code.push_back(ctx.strings.insert(var_token));
}
else
{
// pre
assert((iter->children.begin())->value.id() == inc_dec_op_id);
assert((iter->children.begin()+1)->value.id() == var_id);
const node_t& op = get_first_leaf(*(iter->children.begin()));
const node_t& var = get_first_leaf(*(iter->children.begin()+1));
string op_token(op.value.begin(),op.value.end());
string var_token(var.value.begin(),var.value.end());
if(op_token[0] == '+')
ctx.code.push_back(op_inc_var);
else // decrement
ctx.code.push_back(op_dec_var);
ctx.code.push_back(ctx.strings.insert(var_token));
compile_var(iter->children.begin()+1,ctx);
}
}
template<typename TreeIterT>
void compile_unary_expr(const TreeIterT& iter, compile_context& ctx)
{
assert(iter->value.id() == unary_expr_id);
// This compiles all the !x ~x +x -x expr
// get a node type
typedef typename TreeIterT::value_type node_t;
// If we have a unary op
TreeIterT i = iter->children.begin();
parser_ids id = static_cast<parser_ids>(i->value.id().to_long());
switch(id)
{
case expr_atom_id:
// only child should be the expr_atom
compile_expr_atom(iter->children.begin(),ctx);
break;
case unary_op_id:
{
// first is op
const node_t& op = get_first_leaf(*(iter->children.begin()));
// second is the expr_atom
compile_expr_atom(iter->children.begin() + 1,ctx);
// perform the op
switch(*(op.value.begin()))
{
case '+':
// no need to do anything, positive is default
break;
case '-':
ctx.code.push_back(op_neg);
break;
case '!':
ctx.code.push_back(op_log_not);
break;
case '~':
ctx.code.push_back(op_bit_not);
break;
default:
throw compile_error<TreeIterT>("Unknown unary operator",iter);
}
}
break;
case inc_dec_expr_id:
compile_inc_dec_expr(iter->children.begin(),ctx);
break;
default:
throw compile_error<TreeIterT>("Unknown Unary Expr Node",iter);
}
}
template<typename TreeIterT>
void compile_mul_expr(const TreeIterT& iter, compile_context& ctx)
{
assert(iter->value.id() == mul_expr_id);
// This compiles < > <= >= expressions
// always an odd number of children
assert(iter->children.size() % 2 == 1);
// get a node type
typedef typename TreeIterT::value_type node_t;
TreeIterT sub_expr = iter->children.begin();
TreeIterT end = iter->children.end();
// compile the left sub_expr
compile_unary_expr(sub_expr,ctx);
for(++sub_expr;sub_expr != end;++sub_expr)
{
// get the node representing the op
const node_t& op = get_first_leaf(*sub_expr);
++sub_expr; // next sub_expr (skip op)
// compile the next sub_expr
compile_unary_expr(sub_expr,ctx);
// perform the op
switch(*(op.value.begin()))
{
case '*':
ctx.code.push_back(op_mul);
break;
case '/':
ctx.code.push_back(op_div);
break;
case '%':
ctx.code.push_back(op_mod);
break;
default:
throw compile_error<TreeIterT>("Unknown mul op",iter);
}
}
}
template<typename TreeIterT>
void compile_add_expr(const TreeIterT& iter, compile_context& ctx)
{
assert(iter->value.id() == add_expr_id);
// This compiles < > <= >= expressions
// always an odd number of children
assert(iter->children.size() % 2 == 1);
// get a node type
typedef typename TreeIterT::value_type node_t;
TreeIterT sub_expr = iter->children.begin();
TreeIterT end = iter->children.end();
// compile the left sub_expr
compile_mul_expr(sub_expr,ctx);
for(++sub_expr;sub_expr != end;++sub_expr)
{
// get the node representing the op
const node_t& op = get_first_leaf(*sub_expr);
++sub_expr; // next sub_expr (skip op)
// compile the next sub_expr
compile_mul_expr(sub_expr,ctx);
// perform the op
switch(*(op.value.begin()))
{
case '+': // ==
ctx.code.push_back(op_add);
break;
case '-': // !=
ctx.code.push_back(op_sub);
break;
case '@':
ctx.code.push_back(op_cat);
break;
default:
throw compile_error<TreeIterT>("Unknown add op",iter);
}
}
}
template<typename TreeIterT>
void compile_shift_expr(const TreeIterT& iter, compile_context& ctx)
{
assert(iter->value.id() == shift_expr_id);
// This compiles < > <= >= expressions
// always an odd number of children
assert(iter->children.size() % 2 == 1);
// get a node type
typedef typename TreeIterT::value_type node_t;
TreeIterT sub_expr = iter->children.begin();
TreeIterT end = iter->children.end();
// compile the left sub_expr
compile_add_expr(sub_expr,ctx);
for(++sub_expr;sub_expr != end;++sub_expr)
{
// get the node representing the op
const node_t& op = get_first_leaf(*sub_expr);
++sub_expr; // next sub_expr (skip op)
// compile the next sub_expr
compile_add_expr(sub_expr,ctx);
// perform the op
switch(*(op.value.begin()))
{
case '<': // ==
ctx.code.push_back(op_shl);
break;
case '>': // !=
ctx.code.push_back(op_shr);
break;
default:
throw compile_error<TreeIterT>("Unknown shift op",iter);
}
}
}
template<typename TreeIterT>
void compile_compare_expr(const TreeIterT& iter, compile_context& ctx)
{
assert(iter->value.id() == compare_expr_id);
// This compiles < > <= >= expressions
// always an odd number of children
assert(iter->children.size() % 2 == 1);
// get a node type
typedef typename TreeIterT::value_type node_t;
TreeIterT sub_expr = iter->children.begin();
TreeIterT end = iter->children.end();
// compile the left sub_expr
compile_shift_expr(sub_expr,ctx);
for(++sub_expr;sub_expr != end;++sub_expr)
{
// get the node representing the op
const node_t& op = get_first_leaf(*sub_expr);
++sub_expr; // next sub_expr (skip op)
// compile the next sub_expr
compile_shift_expr(sub_expr,ctx);
// perform the op
string op_token(op.value.begin(),op.value.end());
switch(op_token[0])
{
case '<': // ==
if(op_token.length() > 1 && op_token[1] == '=')
ctx.code.push_back(op_cmp_less_eq);
else
ctx.code.push_back(op_cmp_less);
break;
case '>': // !=
if(op_token.length() > 1 && op_token[1] == '=')
ctx.code.push_back(op_cmp_grtr_eq);
else
ctx.code.push_back(op_cmp_grtr);
break;
default:
throw compile_error<TreeIterT>("Unknown comparison op",iter);
}
}
}
template<typename TreeIterT>
void compile_equality_expr(const TreeIterT& iter, compile_context& ctx)
{
assert(iter->value.id() == equality_expr_id);
// This compiles == !=
// always an odd number of children
assert(iter->children.size() % 2 == 1);
// get a node type
typedef typename TreeIterT::value_type node_t;
TreeIterT sub_expr = iter->children.begin();
TreeIterT end = iter->children.end();
// compile the left sub_expr
compile_compare_expr(sub_expr,ctx);
for(++sub_expr;sub_expr != end;++sub_expr)
{
// get the node representing the op
const node_t& op = get_first_leaf(*sub_expr);
++sub_expr; // next sub_expr (skip op)
// compile the next sub_expr
compile_compare_expr(sub_expr,ctx);
// perform the op
switch(*(op.value.begin()))
{
case '=': // ==
ctx.code.push_back(op_eq);
break;
case '!': // !=
ctx.code.push_back(op_neq);
break;
default:
throw compile_error<TreeIterT>("Unknown equality op",iter);
}
}
}
template<typename TreeIterT>
void compile_bitwise_expr(const TreeIterT& iter, compile_context& ctx)