forked from gfwilliams/tiny-js
-
Notifications
You must be signed in to change notification settings - Fork 6
/
TinyJS_SyntaxTree.cpp
1114 lines (1036 loc) · 30.2 KB
/
TinyJS_SyntaxTree.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
#include "TinyJS_SyntaxTree.h"
#include <string.h>
#include <algorithm>
#include <assert.h>
#include <sstream>
#define ASSERT(X) assert(X)
// if this flag is enabled, the constructors of CSyntax constructs
// will ensure that the simplifying assumptions they make in their
// emit() methods are not violated
#define CHECK_SYNTAX_TREE
// these three macros are used to avoid memory leaking in JIT-ed code.
#define FUNCTION_VECTOR_NAME "__t_"
#define NO_LEAK_BEGIN() (std::string("(*") + FUNCTION_VECTOR_NAME + ".insert(" + FUNCTION_VECTOR_NAME + ".end(), ")
#define NO_LEAK_END() ("))")
CScriptSyntaxTree::CScriptSyntaxTree(CScriptLex* lexer)
{
this->lexer = lexer;
lexerOwned = false;
root = 0;
}
CScriptSyntaxTree::CScriptSyntaxTree(const std::string& buffer)
{
this->lexer = new CScriptLex(buffer);
lexerOwned = true;
root = 0;
}
CScriptSyntaxTree::~CScriptSyntaxTree()
{
if(root)
delete root;
if(lexerOwned)
delete lexer;
}
void CScriptSyntaxTree::parse()
{
CSyntaxSequence* stmts = 0;
while(lexer->tk)
stmts = new CSyntaxSequence(stmts, statement());
lexer->match(LEX_EOF);
if(stmts && !stmts->first())
root = stmts->second();
else
root = stmts;
}
void CScriptSyntaxTree::compile(std::ostream & out)
{
if(!(CSyntaxFunction*)root)
TRACE("Warning: the root of this syntax tree is not a function definition. Compiled code may not function correctly.\n");
root->emit(out);
}
std::vector<CSyntaxExpression*> CScriptSyntaxTree::functionCall()
{
std::vector<CSyntaxExpression*> args;
lexer->match('(');
while(lexer->tk != ')')
{
args.push_back(base());
if(lexer->tk != ')') lexer->match(',');
}
lexer->match(')');
return args;
}
CSyntaxExpression* CScriptSyntaxTree::factor()
{
if(lexer->tk == '(')
{
lexer->match('(');
CSyntaxExpression* a = base();
lexer->match(')');
return a;
}
if(lexer->tk == LEX_R_TRUE)
{
lexer->match(LEX_R_TRUE);
return new CSyntaxFactor("1");
}
if(lexer->tk == LEX_R_FALSE)
{
lexer->match(LEX_R_FALSE);
return new CSyntaxFactor("0");
}
if(lexer->tk == LEX_R_NULL)
{
lexer->match(LEX_R_NULL);
return new CSyntaxFactor("null");
}
if(lexer->tk == LEX_R_UNDEFINED)
{
lexer->match(LEX_R_UNDEFINED);
return new CSyntaxFactor("undefined");
}
if(lexer->tk == LEX_ID)
{
int nameStart = lexer->tokenStart;
std::string tokenName = lexer->tkStr;
lexer->match(LEX_ID);
CSyntaxExpression* a = 0;
while(lexer->tk == '(' || lexer->tk == '.' || lexer->tk == '[')
{
if(lexer->tk == '(')
{
std::string argString = lexer->getSubString(nameStart);
int argStart = lexer->tokenStart;
auto args = functionCall();
argString += lexer->getSubString(argStart);
a = new CSyntaxFunctionCall(a ? a : new CSyntaxID(tokenName), args, argString);
}
else if(lexer->tk == '.')
{
lexer->match('.');
const std::string &name = lexer->tkStr;
a = new CSyntaxBinaryOperator('.', a ? a : new CSyntaxID(tokenName), new CSyntaxID(name));
lexer->match(LEX_ID);
}
else if(lexer->tk == '[')
{
lexer->match('[');
CSyntaxExpression* index = base();
lexer->match(']');
a = new CSyntaxBinaryOperator('[', a ? a : new CSyntaxID(tokenName), index);
}
else ASSERT(0);
}
// likely the lhs of an assignment (or rhs, really)
if(!a)
a = new CSyntaxID(tokenName);
return a;
}
if(lexer->tk == LEX_INT || lexer->tk == LEX_FLOAT)
{
CSyntaxFactor* a = new CSyntaxFactor(lexer->tkStr);
lexer->match(lexer->tk);
return a;
}
if(lexer->tk == LEX_STR)
{
CSyntaxFactor* a = new CSyntaxFactor(lexer->tkStr);
lexer->match(LEX_STR);
return a;
}
if(lexer->tk == '{')
{
/* JSON-style object definition */
std::string arg = "{";
lexer->match('{');
// compile string
while(lexer->tk != '}')
{
arg += lexer->tkStr.size() ? lexer->tkStr : std::string(1, (char)lexer->tk);
lexer->match(lexer->tk);
}
arg += "}";
lexer->match('}');
// "__object_()" is a special native function that constructs an object from a string
return new CSyntaxFunctionCall(new CSyntaxID(TINYJS_OBJECT_FUNCTION_NAME), std::vector<CSyntaxExpression*>(1,
new CSyntaxFactor(arg.c_str())), TINYJS_OBJECT_FUNCTION_NAME + ("(\"" + arg + "\")"));
}
if(lexer->tk == '[')
{
/* JSON-style array definition */
lexer->match('[');
std::string arg = "[";
while(lexer->tk != ']')
{
arg += lexer->tkStr;
lexer->match(lexer->tk);
}
lexer->match(']');
arg += ']';
return new CSyntaxFunctionCall(new CSyntaxID(TINYJS_ARRAY_FUNCTION_NAME), std::vector<CSyntaxExpression*>(1,
new CSyntaxFactor(arg.c_str())), TINYJS_ARRAY_FUNCTION_NAME + ("(\"" + arg + "\")"));
}
if(lexer->tk == LEX_R_FUNCTION)
{
return parseFunctionDefinition();
}
if(lexer->tk == LEX_R_NEW)
{
// new -> create a new object
// this means that if the id referenced is a function,
// call the function with a new object "this" in scope,
// otherwise create a new object and set its .prototype attribute
// to the thing specified.
lexer->match(LEX_R_NEW);
const std::string &className = lexer->tkStr;
lexer->match(LEX_ID);
std::string arg = className + "(";
while(lexer->tk != ')')
{
arg += lexer->tkStr;
lexer->match(lexer->tk);
}
lexer->match(')');
arg += ')';
return new CSyntaxFunctionCall(new CSyntaxID(TINYJS_NEW_FUNCTION_NAME), std::vector<CSyntaxExpression*>(1,
new CSyntaxFactor(arg.c_str())), TINYJS_NEW_FUNCTION_NAME + arg);
}
// Nothing we can do here... just hope it's the end...
lexer->match(LEX_EOF);
return NULL;
}
CSyntaxExpression* CScriptSyntaxTree::unary()
{
if(lexer->tk == '!')
{
lexer->match('!'); // binary not
return new CSyntaxUnaryOperator('!', factor());
}
return factor();
}
CSyntaxExpression* CScriptSyntaxTree::term()
{
CSyntaxExpression* a = unary();
while(lexer->tk == '*' || lexer->tk == '/' || lexer->tk == '%')
{
int op = lexer->tk;
lexer->match(lexer->tk);
return new CSyntaxBinaryOperator(op, a, unary());
}
return a;
}
CSyntaxExpression* CScriptSyntaxTree::expression()
{
bool negate = false;
if(lexer->tk == '-')
{
lexer->match('-');
negate = true;
}
CSyntaxExpression* a = term();
if(negate)
{
a = new CSyntaxBinaryOperator('-', new CSyntaxFactor("0"), a);
}
while(lexer->tk == '+' || lexer->tk == '-' ||
lexer->tk == LEX_PLUSPLUS || lexer->tk == LEX_MINUSMINUS)
{
int op = lexer->tk;
lexer->match(lexer->tk);
if(op == LEX_PLUSPLUS || op == LEX_MINUSMINUS)
{
// more desugaring
a = new CSyntaxAssign('=', a,
new CSyntaxBinaryOperator(op == LEX_PLUSPLUS ? '+' : '-', a, new CSyntaxFactor("1")));
}
else
{
a = new CSyntaxBinaryOperator(op, a, term());
}
}
return a;
}
CSyntaxExpression* CScriptSyntaxTree::shift()
{
CSyntaxExpression* a = expression();
if(lexer->tk == LEX_LSHIFT || lexer->tk == LEX_RSHIFT || lexer->tk == LEX_RSHIFTUNSIGNED)
{
int op = lexer->tk;
lexer->match(op);
return new CSyntaxBinaryOperator(op, a, base());
}
return a;
}
CSyntaxExpression* CScriptSyntaxTree::condition()
{
CSyntaxExpression* a = shift();
while(lexer->tk == LEX_EQUAL || lexer->tk == LEX_NEQUAL ||
lexer->tk == LEX_TYPEEQUAL || lexer->tk == LEX_NTYPEEQUAL ||
lexer->tk == LEX_LEQUAL || lexer->tk == LEX_GEQUAL ||
lexer->tk == '<' || lexer->tk == '>')
{
int op = lexer->tk;
lexer->match(lexer->tk);
a = new CSyntaxRelation(op, a, shift());
}
return a;
}
CSyntaxExpression* CScriptSyntaxTree::logic()
{
CSyntaxExpression* a = condition();
CSyntaxExpression* b;
while(lexer->tk == '&' || lexer->tk == '|' || lexer->tk == '^' || lexer->tk == LEX_ANDAND || lexer->tk == LEX_OROR)
{
int op = lexer->tk;
lexer->match(lexer->tk);
b = condition();
if(op == LEX_ANDAND || op == LEX_OROR)
a = new CSyntaxCondition(op, a, b);
else
a = new CSyntaxBinaryOperator(op, a, b);
}
return a;
}
CSyntaxExpression* CScriptSyntaxTree::ternary()
{
CSyntaxExpression* lhs = logic();
if(lexer->tk == '?')
{
lexer->match('?');
CSyntaxExpression* b1 = base();
lexer->match(':');
lhs = new CSyntaxTernaryOperator('?', lhs, b1, base());
}
return lhs;
}
CSyntaxExpression* CScriptSyntaxTree::base()
{
CSyntaxExpression* lhs = ternary();
if(lexer->tk == '=' || lexer->tk == LEX_PLUSEQUAL || lexer->tk == LEX_MINUSEQUAL)
{
int op = lexer->tk;
lexer->match(lexer->tk);
CSyntaxExpression* rhs = base();
if(op == '=')
{
lhs = new CSyntaxAssign('=', lhs, rhs);
}
// hey look, syntactic desugaring
else if(op == LEX_PLUSEQUAL)
{
lhs = new CSyntaxAssign('=', lhs, new CSyntaxBinaryOperator('+', lhs, rhs));
}
else if(op == LEX_MINUSEQUAL)
{
lhs = new CSyntaxAssign('=', lhs, new CSyntaxBinaryOperator('-', lhs, rhs));
}
else ASSERT(0);
}
return lhs;
}
CSyntaxStatement* CScriptSyntaxTree::block()
{
lexer->match('{');
CSyntaxSequence* stmts = 0;
while(lexer->tk && lexer->tk != '}')
{
CSyntaxNode* stmt = statement();
CSyntaxSequence* seq = dynamic_cast<CSyntaxSequence*>(stmt);
if(seq)
{
std::vector<CSyntaxNode*> statements = seq->normalize();
delete seq;
for(auto& stmt2 : statements)
stmts = new CSyntaxSequence(stmts, stmt2);
}
else
stmts = new CSyntaxSequence(stmts, stmt);
}
lexer->match('}');
return stmts;
}
CSyntaxNode* CScriptSyntaxTree::statement()
{
if(lexer->tk == LEX_ID ||
lexer->tk == LEX_INT ||
lexer->tk == LEX_FLOAT ||
lexer->tk == LEX_STR ||
lexer->tk == '-')
{
/* Execute a simple statement that only contains basic arithmetic... */
CSyntaxNode* out = base();
lexer->match(';');
return out;
}
else if(lexer->tk == '{')
{
/* A block of code */
return block();
}
else if(lexer->tk == ';')
{
/* Empty statement - to allow things like ;;; */
lexer->match(';');
return statement();
}
else if(lexer->tk == LEX_R_VAR)
{
lexer->match(LEX_R_VAR);
CSyntaxSequence* stmts = 0;
CSyntaxNode* stmt = 0;
while(lexer->tk != ';')
{
CSyntaxExpression* lhs = new CSyntaxID(lexer->tkStr);
lexer->match(LEX_ID);
// now do stuff defined with dots
while(lexer->tk == '.')
{
lexer->match('.');
lhs = new CSyntaxBinaryOperator('.', lhs, new CSyntaxID(lexer->tkStr));
lexer->match(LEX_ID);
}
// sort out initialiser
if(lexer->tk == '=')
{
lexer->match('=');
stmt = new CSyntaxDefinition(lhs, base());
}
else
stmt = new CSyntaxDefinition(lhs, NULL);
if(lexer->tk != ';')
{
lexer->match(',');
stmts = new CSyntaxSequence(stmts, stmt ? stmt : lhs);
stmt = 0;
}
else if(stmts)
stmts = new CSyntaxSequence(stmts, stmt ? stmt : lhs);
}
lexer->match(';');
return stmts ? stmts : (CSyntaxNode*)stmt;
}
else if(lexer->tk == LEX_R_IF)
{
lexer->match(LEX_R_IF);
lexer->match('(');
CSyntaxExpression* cond = base();
lexer->match(')');
CSyntaxNode* body = statement();
CSyntaxNode* else_ = 0;
if(lexer->tk == LEX_R_ELSE)
{
lexer->match(LEX_R_ELSE);
else_ = statement();
}
return new CSyntaxIf(cond, body, else_);
}
else if(lexer->tk == LEX_R_WHILE)
{
lexer->match(LEX_R_WHILE);
lexer->match('(');
CSyntaxExpression* cond = base();
lexer->match(')');
CSyntaxNode* body = statement();
return new CSyntaxWhile(cond, body);
}
else if(lexer->tk == LEX_R_FOR)
{
lexer->match(LEX_R_FOR);
lexer->match('(');
CSyntaxNode* init = statement(); // initialisation
CSyntaxExpression* cond = base(); // condition
lexer->match(';');
CSyntaxExpression* iter = base(); // iterator
lexer->match(')');
CSyntaxNode* body = statement();
return new CSyntaxFor(init, cond, iter, body);
}
else if(lexer->tk == LEX_R_RETURN)
{
lexer->match(LEX_R_RETURN);
CSyntaxExpression* value = base();
lexer->match(';');
return new CSyntaxReturn(value);
}
else if(lexer->tk == LEX_R_FUNCTION)
{
return parseFunctionDefinition();
}
else
{
lexer->match(LEX_EOF);
return NULL;
}
}
CSyntaxFunction* CScriptSyntaxTree::parseFunctionDefinition()
{
lexer->match(LEX_R_FUNCTION);
std::string funcName = TINYJS_TEMP_NAME;
/* we can have functions without names */
if(lexer->tk == LEX_ID)
{
funcName = lexer->tkStr;
lexer->match(LEX_ID);
}
std::vector<CSyntaxID*> args = parseFunctionArguments();
CSyntaxStatement* body = block();
return new CSyntaxFunction(funcName == TINYJS_TEMP_NAME ? 0 : new CSyntaxID(funcName), args, body);
}
std::vector<CSyntaxID*> CScriptSyntaxTree::parseFunctionArguments()
{
std::vector<CSyntaxID*> out;
lexer->match('(');
while(lexer->tk != ')')
{
out.push_back(new CSyntaxID(lexer->tkStr));
lexer->match(LEX_ID);
if(lexer->tk != ')') lexer->match(',');
}
lexer->match(')');
return out;
}
CSyntaxNode::CSyntaxNode()
{
node = 0;
}
CSyntaxNode::~CSyntaxNode()
{
if(node)
delete node;
}
CSyntaxSequence::CSyntaxSequence(CSyntaxNode* front, CSyntaxNode* last)
{
node = front;
this->last = last;
disowned = false;
#ifdef CHECK_SYNTAX_TREE
ASSERT(this->last && !dynamic_cast<CSyntaxSequence*>(this->last));
ASSERT(this->node || this->last);
#endif
}
CSyntaxSequence::~CSyntaxSequence()
{
// make sure to still delete sequences
if(disowned)
{
if(node && !dynamic_cast<CSyntaxSequence*>(node))
node = 0;
if(last && dynamic_cast<CSyntaxSequence*>(last))
delete last;
}
else if(last)
delete last;
}
std::vector<CSyntaxNode*> CSyntaxSequence::normalize(bool disown_children)
{
disowned = disown_children;
std::vector<CSyntaxNode*> stmts;
CSyntaxSequence* child;
if(node)
{
if(child = dynamic_cast<CSyntaxSequence*>(node))
{
std::vector<CSyntaxNode*> children = child->normalize(disown_children);
stmts.insert(stmts.end(), children.begin(), children.end());
}
else
stmts.push_back(node);
}
if(last)
{
if(child = dynamic_cast<CSyntaxSequence*>(last))
{
std::vector<CSyntaxNode*> children = child->normalize(disown_children);
stmts.insert(stmts.end(), children.begin(), children.end());
}
else
stmts.push_back(last);
}
return stmts;
}
// this function assumes that the tree of sequencing is entirely left-heavy;
// ie the structure is:
// left := sequence|statement|null
// right := statement
// enable the compile-time constant CHECK_SYNTAX_TREE to confirm this
void CSyntaxSequence::emit(std::ostream & out, const std::string indentation)
{
std::vector<CSyntaxSequence*> parents;
CSyntaxNode* n = node;
CSyntaxSequence* seq;
while(seq = dynamic_cast<CSyntaxSequence*>(n))
{
parents.push_back(seq);
n = seq->node;
}
while(!parents.empty())
{
seq = *parents.rbegin();
parents.pop_back();
if(seq->node && !dynamic_cast<CSyntaxSequence*>(seq->node))
{
seq->node->emit(out, indentation);
if(seq->node->semicolonizable())
out << ";";
out << "\n";
}
if(seq->last)
{
seq->last->emit(out, indentation);
if(seq->last->semicolonizable())
out << ";";
out << "\n";
}
}
if(node && !dynamic_cast<CSyntaxSequence*>(node))
{
node->emit(out, indentation);
if(node->semicolonizable())
out << ";";
out << "\n";
}
if(last)
{
last->emit(out, indentation);
if(last->semicolonizable())
out << ";";
out << "\n";
}
}
CSyntaxIf::CSyntaxIf(CSyntaxExpression* expr, CSyntaxNode* body, CSyntaxNode* else_)
{
ASSERT(expr);
ASSERT(body);
node = body;
this->else_ = else_;
this->expr = expr;
}
CSyntaxIf::CSyntaxIf(CSyntaxExpression* expr, CSyntaxNode* body) : CSyntaxIf(expr, body, 0) { }
CSyntaxIf::~CSyntaxIf()
{
delete expr;
if(else_)
delete else_;
}
void CSyntaxIf::emit(std::ostream & out, const std::string indentation)
{
out << indentation << "if(";
expr->emit(out);
out << "->var->getBool()) {\n";
node->emit(out, indentation + " ");
out << indentation << "} ";
if(else_)
{
out << "else {\n";
else_->emit(out, indentation + " ");
out << indentation << "}";
}
}
CSyntaxWhile::CSyntaxWhile(CSyntaxExpression* expr, CSyntaxNode* body)
{
ASSERT(body);
ASSERT(expr);
node = body;
this->expr = expr;
}
CSyntaxWhile::~CSyntaxWhile()
{
delete expr;
}
void CSyntaxWhile::emit(std::ostream & out, const std::string indentation)
{
out << indentation << "while(";
expr->emit(out);
out << "->var->getBool()) {\n";
node->emit(out, indentation + " ");
out << indentation << "}";
}
CSyntaxFor::CSyntaxFor(CSyntaxNode* init, CSyntaxExpression* expr, CSyntaxExpression* update, CSyntaxNode* body)
{
ASSERT(body);
node = body;
this->init = init;
this->update = update;
cond = expr;
}
CSyntaxFor::~CSyntaxFor()
{
if(init)
delete init;
if(update)
delete update;
if(cond)
delete cond;
}
void CSyntaxFor::emit(std::ostream & out, const std::string indentation)
{
out << indentation << "for(";
if(init)
{
init->emit(out);
}
out << "; ";
if(cond)
{
cond->emit(out);
out << "->var->getBool()";
}
out << "; ";
if(update)
{
update->emit(out);
}
out << ") {\n";
node->emit(out, indentation + " ");
out << indentation << "}";
}
CSyntaxFactor::CSyntaxFactor(std::string val)
{
value = val;
node = 0;
char f = value.front();
if(f == '"')
factorType = F_TYPE_STRING;
else if(isdigit(f))
{
if(value.find('.') != std::string::npos)
factorType = F_TYPE_DOUBLE;
else
factorType = F_TYPE_INT;
}
else
factorType = F_TYPE_IDENTIFIER;
}
void CSyntaxFactor::emit(std::ostream & out, const std::string indentation)
{
out << indentation;
out << NO_LEAK_BEGIN();
out << "new CScriptVarLink(new CScriptVar(";
switch(factorType)
{
case F_TYPE_INT:
out << getInt();
break;
case F_TYPE_DOUBLE:
out << getDouble();
break;
case F_TYPE_STRING:
out << "\"" << value.c_str() << "\"";
break;
case F_TYPE_IDENTIFIER:
out << value.c_str();
break;
}
out << "))";
out << NO_LEAK_END();
}
CSyntaxID::CSyntaxID(std::string id) : CSyntaxFactor(id) { }
void CSyntaxID::emit(std::ostream & out, const std::string indentation)
{
// this is not correct
out << indentation << value.c_str();
}
CSyntaxFunction::CSyntaxFunction(CSyntaxID* name, std::vector<CSyntaxID*>& arguments, CSyntaxStatement* body)
{
ASSERT(body);
this->name = name;
this->arguments = arguments;
node = body;
}
CSyntaxFunction::~CSyntaxFunction()
{
if(name)
delete name;
for(CSyntaxID* arg : arguments)
if(arg)
delete arg;
}
void CSyntaxFunction::emit(std::ostream & out, const std::string indentation)
{
out << indentation << "extern \"C\" {\n";
std::string realIndent = indentation + " ";
out << realIndent << "void ";
getName()->emit(out);
out << "(CScriptVar* root, void* userData) {\n";
// to avoid memory leaks, we need a structure to hold any newly allocated CScriptVarLink*s.
// we need to mangle the name of any locals so as to avoid name collisions
// (however, if any variables in the function were named "__t_", this would
// generate bad code due to a redeclaration).
// thus, we should endeavor to declare as little extra as possible.
out << realIndent + " " << "std::vector<CScriptVarLink*> " << FUNCTION_VECTOR_NAME << ";\n";
for(auto& arg : arguments)
{
// setup variable declarations/assignments
out << realIndent + " " << "CScriptVarLink* ";
arg->emit(out);
out << " = new CScriptVarLink(root->getParameter(\"";
arg->emit(out);
out << "\"));\n";
}
node->emit(out, realIndent + " ");
// cleanup
for(auto& arg : arguments)
{
out << realIndent + " " << "delete ";
arg->emit(out);
out << ";\n";
}
out << realIndent + " " << "for(CScriptVarLink* __to_del_: " << FUNCTION_VECTOR_NAME << ")\n";
out << realIndent + " " << "delete __to_del_;\n";
out << realIndent << "}\n";
out << indentation << "}\n";
}
CSyntaxID* CSyntaxFunction::getName()
{
if(!name)
generateRandomId();
return name;
}
void CSyntaxFunction::generateRandomId()
{
// generate a random (likely unused) identifier so that this function is not unnamed
static const char alphanum[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";
std::string randomId = "";
for(int i = 0; i < 20; ++i)
{
randomId += alphanum[rand() % (sizeof(alphanum) - 1)];
}
name = new CSyntaxID(randomId);
}
CSyntaxAssign::CSyntaxAssign(int op, CSyntaxExpression* lvalue, CSyntaxExpression* rvalue)
{
ASSERT(lvalue);
ASSERT(rvalue);
this->op = op;
lval = lvalue;
node = rvalue;
}
CSyntaxAssign::~CSyntaxAssign()
{
delete lval;
}
void CSyntaxAssign::emit(std::ostream & out, const std::string indentation)
{
out << indentation;
lval->emit(out);
out << "->replaceWith(";
node->emit(out);
out << "->var)";
}
CSyntaxTernaryOperator::CSyntaxTernaryOperator(int op, CSyntaxExpression* cond, CSyntaxExpression* b1, CSyntaxExpression* b2)
{
ASSERT(cond);
ASSERT(b1);
ASSERT(b2);
this->op = op;
node = cond;
this->b1 = b1;
this->b2 = b2;
#ifdef CHECK_SYNTAX_TREE
ASSERT(op == '?');
#endif
}
CSyntaxTernaryOperator::~CSyntaxTernaryOperator()
{
delete b1;
delete b2;
}
void CSyntaxTernaryOperator::emit(std::ostream & out, const std::string indentation)
{
// op can only be '?'
out << indentation;
node->emit(out);
out << "->var->getBool() ? ";
b1->emit(out);
out << " : ";
b2->emit(out);
}
CSyntaxRelation::CSyntaxRelation(int rel, CSyntaxExpression* left, CSyntaxExpression* right)
: CSyntaxBinaryOperator(rel, left, right)
{ }
void CSyntaxRelation::emit(std::ostream & out, const std::string indentation)
{
CSyntaxBinaryOperator::emit(out);
}
CSyntaxBinaryOperator::CSyntaxBinaryOperator(int op, CSyntaxExpression* left, CSyntaxExpression* right)
{
ASSERT(left);
ASSERT(right);
this->op = op;
node = left;
this->right = right;
#ifdef CHECK_SYNTAX_TREE
// right side has to be an ID
ASSERT(op != '.' || dynamic_cast<CSyntaxID*>(right));
#endif
}
CSyntaxBinaryOperator::~CSyntaxBinaryOperator()
{
delete right;
}
void CSyntaxBinaryOperator::emit(std::ostream & out, const std::string indentation)
{
out << indentation;
if(op != '[' && op != '.')
{
out << NO_LEAK_BEGIN();
out << "new CScriptVarLink(";
node->emit(out);
out << "->var->mathsOp(";
right->emit(out);
out << "->var, " << op << "))";
out << NO_LEAK_END();
}
else
{
// beautifully enough, this will return a CScriptVarLink*, which can be used as
// /either/ an lvalue OR an rvalue depending on the calling code's fancy!
// The more I work on this code, the more I begin to think that it was actually
// fairly well designed.
node->emit(out);
out << "->var->findChildOrCreate(";
if(op == '.')
{
out << '"';
right->emit(out);
out << '"';
}
else
{
right->emit(out);
out << "->var->getString()";
}
out << ")";
}
}
std::string CSyntaxBinaryOperator::lvaluePath()
{
if(op == '.')
return ((CSyntaxExpression*)node)->lvaluePath() + "." + right->lvaluePath();
else if(op == '[')
{
generateRandomID();
return ((CSyntaxExpression*)node)->lvaluePath() + "_array__" + randomArrayName;
}
ASSERT(0);
return std::string();
}
void CSyntaxBinaryOperator::generateRandomID()
{
if(randomArrayName.length() != 0)
return;
// generate a random (likely unused) identifier so that this function is not unnamed
static const char alphanum[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";
std::string randomId = "";
for(int i = 0; i < 5; ++i)
{
randomId += alphanum[rand() % (sizeof(alphanum) - 1)];
}
randomArrayName = randomId;
}
CSyntaxUnaryOperator::CSyntaxUnaryOperator(int op, CSyntaxExpression* expr)