-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparserTree.cc
More file actions
1300 lines (1206 loc) · 42.4 KB
/
Copy pathparserTree.cc
File metadata and controls
1300 lines (1206 loc) · 42.4 KB
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 "parserTree.h"
#include "parser.h"
#include "scope.h"
#include "object.h"
#include "function.h"
#include "thread.h"
#include "debug.h"
#include "error.h"
// for string escape
#include <boost/algorithm/string.hpp>
#include <iostream>
using namespace std;
namespace ilang {
namespace parserNode {
Head::Head(list<Node*> *declars, ImportScopeFile *import): Import(import), Declars(declars) {
debug(-5, "head " << declars->size() );
debug(-5, "running ++++++++++++++++++++++++++++++++++++++++++++" );
scope=NULL;
}
void Head::Link () {
if(scope) return;
scope = new FileScope(this);
passScope = ScopePass(scope);
if(Import) // when used with eval, there is no import handler
Import->resolve(scope);
for(list<Node*>::iterator it = Declars->begin(); it != Declars->end(); it++) {
debug(-6, "calling run" )
(*it)->Run(passScope);
//scope->Debug();
}
}
void Head::Run() {
vector<ValuePass> v;
ValuePass ret;
scope->Debug();
ilang::Variable * find = scope->lookup("main");
error(find, "main function not found");
boost::any_cast<ilang::Function>(find->Get()->Get()).ptr(passScope, v, &ret);
//boost::any_cast<ilang::Function_ptr>(scope->lookup("main")->Get()->Get())(scope, v, &ret);
}
void Head::Print(Printer *p) {
for(auto it : *Declars) {
it->Print(p);
p->p() << ";\n";
}
}
static unsigned long _ilang_node_id;
Node::Node() {
_node_id = ++_ilang_node_id;
_parent = NULL;
}
void Node::_setParent(Node *n) {
assert(!_parent);
_parent = n;
}
// this does not need to have anything
void Constant::Run(ScopePass scope) {}
StringConst::StringConst(char *str) :string(str){
delete[] str;
using boost::replace_all;
replace_all(string, "\\n", "\n");
replace_all(string, "\\t", "\t");
replace_all(string, "\\\\", "\\");
replace_all(string, "\\\"", "\"");
replace_all(string, "\\'", "'");
}
ValuePass StringConst::GetValue (ScopePass scope) {
debug(-6, "string get value" );
return ValuePass(new ilang::Value(std::string(string)));
}
void StringConst::Print(Printer *p) {
using boost::replace_all;
std::string result = string;
replace_all(result, "\n", "\\n");
replace_all(result, "\t", "\\t");
replace_all(result, "\\", "\\\\");
replace_all(result, "\"", "\\\"");
replace_all(result, "\'", "\\'");
p->p() << "\"" << result << "\"";
}
IntConst::IntConst(long n) : num(n) {}
ValuePass IntConst::GetValue (ScopePass scope) {
ValuePass r = ValuePass(new ilang::Value(num));
return r;
}
void IntConst::Print(Printer *p) {
p->p() << num;
}
FloatConst::FloatConst(double d) : num(d) {}
ValuePass FloatConst::GetValue (ScopePass scope) {
ValuePass r = ValuePass(new ilang::Value(num));
return r;
}
void FloatConst::Print(Printer *p) {
p->p() << num;
}
IfStmt::IfStmt (Node *test_, Node* True_, Node* False_): True(True_), False(False_) {
Value *t = dynamic_cast<Value*>(test_);
error(t, "If statement does not contain a test");
//assert(t);
test=t;
}
void IfStmt::Run(ScopePass scope) {
ValuePass search = test->GetValue(scope);
assert(search);
if(search->isTrue()) {
if(True) True->Run(scope);
}else{
if(False) False->Run(scope);
}
}
void IfStmt::Print(Printer *p) {
p->p() << "if(";
test->Print(p);
p->p() << ") ";
p->up();
if(True) True->Print(p);
else p->p() << ';';
if(False) {
p->down();
p->p() << "else";
p->up();
False->Print(p);
p->down();
}
}
ForStmt::ForStmt (Node *pre_, Node *test_, Node *each_, Node *exe_) :pre(pre_), each(each_), exe(exe_) {
Value *t = dynamic_cast<Value*>(test_);
error(t, "Test in for statment is not a test");
//assert(t);
test = t;
}
void ForStmt::Run(ScopePass scope) {
pre->Run(scope);
ValuePass t = test->GetValue(scope);
while(t->isTrue()) {
exe->Run(scope);
each->Run(scope);
ValuePass n = test->GetValue(scope);
t = n;
}
}
void ForStmt::Print(Printer *p) {
p->p() << "for(";
pre->Print(p);
p->p() << ';';
test->Print(p);
p->p() << ';';
each->Print(p);
p->p() << ") ";
p->up();
exe->Print(p);
p->down();
}
WhileStmt::WhileStmt (Node *test_, Node *exe_): exe(exe_) {
Value *t=dynamic_cast<Value*>(test_);
error(t, "Test in while Statement is not a test");
//assert(t);
test=t;
}
void WhileStmt::Run(ScopePass scope) {
// note to self: must recall GetValue each time as it is what is actually doing the updating
ValuePass t = test->GetValue(scope);
while(t->isTrue()) {
exe->Run(scope);
ValuePass n = test->GetValue(scope);
t = n;
//test->GetValue(scope)->Print();
}
}
void WhileStmt::Print(Printer *p) {
p->p() << "while(";
test->Print(p);
p->p() << ") ";
p->up();
exe->Print(p);
p->down();
}
ReturnStmt::ReturnStmt (Node *r) {
if(r) {
ret = dynamic_cast<Value*>(r);
error(ret, "Return is not given a value");
///assert(ret);
}else
ret = NULL;
}
void ReturnStmt::Run(ScopePass scope) {
ValuePass v;
if(ret) v = ret->GetValue(scope);
scope->ParentReturn(&v); // this should avoid copying any smart pointers and run much faster
}
void ReturnStmt::Print(Printer *p) {
if(ret) {
p->p() << "return ";
ret->Print(p);
p->p() << ";\n";
}else{
p->p() << "return;\n";
}
}
Function::Function (list<Node*> *p, list<Node*> *b):body(b), params(p) {
debug(-5, "\t\t\tfunction constructed" );
}
void Function::Run(ScopePass scope) {
vector<ValuePass> p;
Call(scope, ScopePass(), p);
}
ValuePass Function::GetValue(ScopePass this_scope) {
// this need to track the scope at this point so that it could be use later in the funciton
debug(-6, "Function get value");
errorTrace("Getting value of a function");
Function *self = this;
ilang::Function fun;
fun.func = this;
fun.ptr = [self, this_scope](ScopePass scope, std::vector<ValuePass> & args, ValuePass *ret) {
self->Call(ScopePass(this_scope), scope, args, ret);
};
return ValuePass(new ilang::Value(fun));
}
void Function::Call(ScopePass _scope_made, ScopePass _scope_self, vector<ValuePass> &p, ValuePass *_ret) {
bool returned=false;
errorTrace("Function called");
// could clean this up to use boost::function and then would not have to template the whole class
auto returnHandle = [&returned, _ret] (ValuePass *ret) {
returned=true;
if(_ret) *_ret = *ret;
debug(-6, "return hook set");
};
ScopePass scope = ScopePass(new FunctionScope<decltype(returnHandle)>(_scope_made, _scope_self, returnHandle));
debug(-6,"function called");
if(params) { // the parser set params to NULL when there are non
list<Node*>::iterator it = params->begin();
for(ValuePass v : p) {
if(it==params->end()) break;
error(dynamic_cast<parserNode::Variable*>(*it), "Argument to function is not variable name");
assert(v);
dynamic_cast<parserNode::Variable*>(*it)->Set(scope, ValuePass(v), true);
it++;
}
}
std::list<std::string> arguments_mod;
ilang::Variable *args = scope->forceNew("arguments", arguments_mod);
ilang::Object *arguments_arr = new ilang::Array(p);
args->Set(ValuePass(new ilang::Value(arguments_arr)));
if(body) { // body can be null if there is nothing
for(Node *n : *body) {
if(returned) return;
assert(n);
debug(-6,"function run");
n->Run(scope);
}
}
// scope deleted
//delete scope; // not going to be needed when smart pointer
}
void Function::Print(Printer *p) {
if(!body && !params) {
p->p() << ";";
return;
}
p->p() << "{";
if(params && !params->empty()) {
p->p() << "|";
bool first=true;
for(Node *it : *params) {
if(!first) p->p() << ", ";
it->Print(p);
first = false;
}
p->p() << "|";
}
if(body && !body->empty()) {
p->up();
bool first=true;
for(Node *it : *body) {
p->line();
it->Print(p);
p->p() << ";";
}
p->down();
p->line();
}
p->p() << "}";
}
Variable::Variable (list<string> *n, list<string> *mod):
name(n), modifiers(mod) {
if(!name) name = new list<string>;
if(!modifiers) modifiers = new list<string>;
//cout << "\t\t\t" << name << "\n";
}
void Variable::Run (ScopePass scope) {
// iirc this does not happen as the syntax does not allow for variables to be created without a default value
error(0, "Variables must always have some value set to them");
debug(-6,"\t\t\tSetting variable: " << name->front());
scope->forceNew(GetFirstName(), *modifiers);
}
void Variable::Set (ScopePass scope, ValuePass var, bool force) {
errorTrace("Setting value of variable: " << GetFirstName());
scope->Debug();
ilang::Variable *v;
if(force || !modifiers->empty()) {
v = scope->forceNew(GetFirstName(), *modifiers);
} else {
//v = scope->lookup(name->front());
v = Get(scope);
}
assert(v);
v->Set(var);
debug(-6,"Set: " << GetFirstName() << " " << var << " " << v->Get());
scope->Debug();
}
ilang::Variable * Variable::Get(ScopePass scope) {
// this should not happen
assert(0);
return NULL;
}
ValuePass Variable::GetValue(ScopePass scope) {
errorTrace("Getting value of variable: "<<GetFirstName());
ilang::Variable *v = Get(scope);
error(v, "Variable " << GetFirstName() << " Not found"); // maybe an assert, not an error?
//assert(v);
ValuePass p = v->Get();
// TODO: fix this to make it correct, if there is no value set to a variable then it is an error atm
error(p, "Variable " << GetFirstName() << " Not found");
return p;
}
std::string Variable::GetFirstName() {
return name->front();
}
void Variable::Print(Printer *p) {
if(modifiers) {
for(std::string it : *modifiers) {
p->p() << it << " ";
}
}
bool first=true;
for(std::string it : *name) {
p->p() << (first ? "" : ".") << it;
first = false;
}
}
FieldAccess::FieldAccess (Node *obj, std::string id) :Variable(NULL, NULL), identifier(id), Obj(NULL) {
if(obj) {
assert(dynamic_cast<Value*>(obj));
Obj = dynamic_cast<Value*>(obj);
}
}
ilang::Variable * FieldAccess::Get(ScopePass scope) {
// bug in this b/c of function pointer
assert(0);
errorTrace("FieldAccess on " << GetFirstName());
ilang::Variable *v;
if(Obj) {
_errors.stream() << "\n\t\tLooking where there is an object";
// this might cause problems if what is returned is not an object
ValuePass obj_val = Obj->GetValue(scope);
assert(obj_val);
boost::any & a = obj_val->Get();
error(a.type() == typeid(ilang::Object*), "trying to find fields in value that does not contain fields, essently not an object\n\t\twas looking for: " << GetFirstName());
ilang::Object *obj = boost::any_cast<ilang::Object*>(a);
v = obj->operator[](identifier);
if(v->Get()->Get().type() == typeid(ilang::Function)) {
// so that the reference to the object is keep around
// Need to fix this so that clean up works
boost::any_cast<ilang::Function>( &(v->Get()->Get()) )->object = obj_val;
}
}else{
v = scope->lookup(identifier);
}
error(v, "Did not find " << GetFirstName());
return v;
}
ValuePass FieldAccess::GetValue(ScopePass scope) {
errorTrace("FieldAccess on " << GetFirstName());
if(Obj) {
_errors.stream() << "\n\tLooking up FieldAccess where there is an object";
ValuePass obj_val = Obj->GetValue(scope);
assert(obj_val);
boost::any & a = obj_val->Get();
error(a.type() == typeid(ilang::Object*), "Trying to find field in value that does not contain field, essently not an object\n\tWas looking in: " << GetFirstName());
ilang::Object *obj = boost::any_cast<ilang::Object*>(a);
ilang::Variable *v = obj->operator[](identifier);
ValuePass ret = v->Get();
if(ret->Get().type() == typeid(ilang::Function)) {
// doing it this way creates a new pointer to the function and then sets the new function to point back to the object, otherwise the object can have smart pointers to itself which causes it to no be deleted from memory
ilang::Function fun = boost::any_cast<ilang::Function>(ret->Get());
fun.object = obj_val;
ret = ValuePass(new ilang::Value(fun));
}
return ret;
}else{
ilang::Variable *v = scope->lookup(identifier);
error(v, "Did not find " << GetFirstName());
ValuePass ret = v->Get();
return ret;
}
}
void FieldAccess::Set(ScopePass scope, ValuePass var, bool force) {
errorTrace("Setting value of variable : " << GetFirstName());
ilang::Variable *v;
if(Obj) {
_errors.stream() << "\n\tLooking up FieldAccess of object to set";
ValuePass obj_val = Obj->GetValue(scope);
assert(obj_val);
boost::any & a = obj_val->Get();
error(a.type() == typeid(ilang::Object*), "Trying to find field in value that does not contain field, essently not an object\n\tWas looking in: " << GetFirstName());
ilang::Object *obj = boost::any_cast<ilang::Object*>(a);
v = obj->operator[](identifier);
}else{
if(force || !modifiers->empty()) {
v = scope->forceNew(GetFirstName(), *modifiers);
}else{
v = scope->lookup(identifier);
}
}
assert(v);
v->Set(var);
}
std::string FieldAccess::GetFirstName() {
return identifier;
}
void FieldAccess::Print(Printer *p) {
if(Obj) {
Obj->Print(p);
p->p() << ".";
}
p->p() << identifier;
}
ArrayAccess::ArrayAccess(Node *obj, Node *look):Variable(NULL,NULL) {
assert(dynamic_cast<Value*>(obj));
Obj = dynamic_cast<Value*>(obj);
assert(dynamic_cast<Value*>(look));
Lookup = dynamic_cast<Value*>(look);
}
ilang::Variable * ArrayAccess::Get(ScopePass scope) {
assert(0);
errorTrace("Getting element using []: "<<GetFirstName());
ValuePass obj_val = Obj->GetValue(scope);
boost::any & a = obj_val->Get();
error(a.type() == typeid(ilang::Object*), "Not of an object or array type");
ilang::Object *obj = boost::any_cast<ilang::Object*>(a);
ValuePass val = Lookup->GetValue(scope);
ilang::Variable *var = obj->operator[](val);
if(var->Get()->Get().type() == typeid(ilang::Function)) {
boost::any_cast<ilang::Function>( &(var->Get()->Get()) )->object = obj_val;
}
return var;
}
ValuePass ArrayAccess::GetValue(ScopePass scope) {
errorTrace("Getting element using []: "<<GetFirstName());
ValuePass obj_val = Obj->GetValue(scope);
boost::any & a = obj_val->Get();
error(a.type() == typeid(ilang::Object*), "Not of an object or array type");
ilang::Object *obj = boost::any_cast<ilang::Object*>(a);
ValuePass val = Lookup->GetValue(scope);
ilang::Variable *var = obj->operator[](val);
ValuePass ret = var->Get();
if(ret->Get().type() == typeid(ilang::Function)) {
ilang::Function fun = boost::any_cast<ilang::Function>(ret->Get());
fun.object = obj_val;
ret = ValuePass(new ilang::Value(fun));
}
return ret;
}
void ArrayAccess::Set(ScopePass scope, ValuePass var, bool force) {
assert(!force);
errorTrace("Setting element using []: " << GetFirstName());
ValuePass obj_val = Obj->GetValue(scope);
boost::any & a = obj_val->Get();
error(a.type() == typeid(ilang::Object*), "Not of an object or array type");
ilang::Object *obj = boost::any_cast<ilang::Object*>(a);
ValuePass index = Lookup->GetValue(scope);
ilang::Variable *v = obj->operator[](index);
v->Set(var);
}
std::string ArrayAccess::GetFirstName() {
return "Array access name";
}
void ArrayAccess::Print(Printer *p) {
Obj->Print(p);
p->p() << "[";
Lookup->Print(p);
p->p() << "]";
}
Call::Call (Value *call, list<Node*> *args):
calling(call), params(args) {
debug(-5,"\t\t\tCalling function");
}
void Call::Run(ScopePass scope) {
debug(-6,"call run");
GetValue(scope);
}
ValuePass Call::GetValue (ScopePass scope) {
errorTrace("Calling function");
std::vector<ValuePass> par;
for(Node * n : *params) {
assert(dynamic_cast<parserNode::Value*>(n));
par.push_back(dynamic_cast<parserNode::Value*>(n)->GetValue(scope));
}
ValuePass ret = ValuePass(new ilang::Value);
ValuePass func = calling->GetValue(scope);
boost::any &an = func->Get();
// TODO: this error is overly cryptic
error(an.type() == typeid(ilang::Function), "Calling a non function " << an.type().name());
ilang::Function *function = boost::any_cast<ilang::Function>(&an);
if(function->native) {
function->ptr(scope, par, &ret);
}else if(function->object) {
assert(function->object->Get().type() == typeid(ilang::Object*));
ScopePass obj_scope = ScopePass(new ObjectScope(boost::any_cast<ilang::Object*>(function->object->Get())));
function->ptr(obj_scope, par, &ret);
}else{
function->ptr(ScopePass(), par, &ret);
}
return ret;
}
void Call::Print(Printer *p) {
calling->Print(p);
p->p() << "(";
bool first=true;
for(auto it : *params) {
if(!first) p->p() << ", ";
it->Print(p);
first = false;
}
p->p() << ")";
}
PrintCall::PrintCall(list<Node*> *args):
Call(NULL, args) {}
ValuePass PrintCall::GetValue (ScopePass scope) {
errorTrace("Print call");
debug(-6,"made into the print Call");
for(Node *it : *params) {
assert(dynamic_cast<parserNode::Value*>(it));
dynamic_cast<parserNode::Value*>((it))->GetValue(scope)->Print();
}
debug(-5,"made it out of print");
}
void PrintCall::Print (Printer *p) {
p->p() << "Print(";
bool first = true;
for(Node *it : *params) {
if(!first) p->p() << ", ";
it->Print(p);
first = false;
}
p->p() << ")";
}
AssignExpr::AssignExpr (Variable *t, Value *v):target(t), eval(v) {
assert(eval);
assert(target);
}
void AssignExpr::Run (ScopePass scope) {
errorTrace("Assignment expression");
ValuePass v = eval->GetValue(scope);
target->Set(scope, v);
scope->Debug();
}
ValuePass AssignExpr::GetValue (ScopePass scope) {
errorTrace("Assignment expression with gettin value");
ValuePass v = eval->GetValue(scope);
target->Set(scope, v);
return v;
}
void AssignExpr::Print(Printer *p) {
target->Print(p);
p->p() << " = ";
eval->Print(p);
}
MathEquation::MathEquation(Value *l, Value *r, action a) : left(l), right(r), Act(a) {
assert(left);
if(a != uMinus) assert(right);
}
void MathEquation::Run(ScopePass scope) {
errorTrace("Running math equation w/o getting value");
left->Run(scope);
if(Act != uMinus) right->Run(scope);
// we do not need the value so we should not request the vale from the nodes
}
ValuePass MathEquation::GetValue(ScopePass scope) {
errorTrace("Math equation");
ValuePass left = this->left->GetValue(scope);
ValuePass right;
if(Act != uMinus) right = this->right->GetValue(scope);
/* when trying for speed boost
auto left_type = left->Get().type();
auto right_type = right->Get().type();
*/
switch(Act) {
case uMinus:
if(left->Get().type() == typeid(std::string)) {
error(0, "can not uMinus a string type");
}else if(left->Get().type() == typeid(double)) {
return ValuePass(new ilang::Value(- boost::any_cast<double>(left->Get())));
}else if(left->Get().type() == typeid(long)) {
return ValuePass(new ilang::Value(- boost::any_cast<long>(left->Get())));
}
break;
case add:
if(left->Get().type() == typeid(std::string) || right->Get().type() == typeid(std::string)) {
return ValuePass(new ilang::Value(std::string(left->str() + right->str())));
}else if(left->Get().type() == typeid(double)) {
if(right->Get().type() == typeid(double))
return ValuePass(new ilang::Value(boost::any_cast<double>(left->Get()) + boost::any_cast<double>(right->Get())));
else if(right->Get().type() == typeid(long))
return ValuePass(new ilang::Value(boost::any_cast<double>(left->Get()) + boost::any_cast<long>(right->Get())));
}else if(left->Get().type() == typeid(long)) {
if(right->Get().type() == typeid(double))
return ValuePass(new ilang::Value(boost::any_cast<long>(left->Get()) + boost::any_cast<double>(right->Get())));
else if(right->Get().type() == typeid(long))
return ValuePass(new ilang::Value(boost::any_cast<long>(left->Get()) + boost::any_cast<long>(right->Get())));
}
break;
case subtract:
if(left->Get().type() == typeid(double)) {
if(right->Get().type() == typeid(double))
return ValuePass(new ilang::Value(boost::any_cast<double>(left->Get()) - boost::any_cast<double>(right->Get())));
else if(right->Get().type() == typeid(long))
return ValuePass(new ilang::Value(boost::any_cast<double>(left->Get()) - boost::any_cast<long>(right->Get())));
}else if(left->Get().type() == typeid(long)) {
if(right->Get().type() == typeid(double))
return ValuePass(new ilang::Value(boost::any_cast<long>(left->Get()) - boost::any_cast<double>(right->Get())));
else if(right->Get().type() == typeid(long))
return ValuePass(new ilang::Value(boost::any_cast<long>(left->Get()) - boost::any_cast<long>(right->Get())));
}
break;
case multiply:
if(left->Get().type() == typeid(double)) {
if(right->Get().type() == typeid(double))
return ValuePass(new ilang::Value(boost::any_cast<double>(left->Get()) * boost::any_cast<double>(right->Get())));
else if(right->Get().type() == typeid(long))
return ValuePass(new ilang::Value(boost::any_cast<double>(left->Get()) * boost::any_cast<long>(right->Get())));
}else if(left->Get().type() == typeid(long)) {
if(right->Get().type() == typeid(double))
return ValuePass(new ilang::Value(boost::any_cast<long>(left->Get()) * boost::any_cast<double>(right->Get())));
else if(right->Get().type() == typeid(long))
return ValuePass(new ilang::Value(boost::any_cast<long>(left->Get()) * boost::any_cast<long>(right->Get())));
}
break;
case divide:
if(left->Get().type() == typeid(double)) {
if(right->Get().type() == typeid(double))
return ValuePass(new ilang::Value(boost::any_cast<double>(left->Get()) / boost::any_cast<double>(right->Get())));
else if(right->Get().type() == typeid(long))
return ValuePass(new ilang::Value(boost::any_cast<double>(left->Get()) / boost::any_cast<long>(right->Get())));
}else if(left->Get().type() == typeid(long)) {
if(right->Get().type() == typeid(double))
return ValuePass(new ilang::Value(boost::any_cast<long>(left->Get()) / boost::any_cast<double>(right->Get())));
else if(right->Get().type() == typeid(long))
return ValuePass(new ilang::Value(boost::any_cast<long>(left->Get()) / boost::any_cast<long>(right->Get())));
}
break;
}
error(0, "Problem with math equation");
}
void MathEquation::Print(Printer *p) {
if(Act != uMinus) left->Print(p);
switch(Act) {
case add:
p->p() << " + ";
break;
case subtract:
p->p() << " - ";
break;
case multiply:
p->p() << " * ";
break;
case divide:
p->p() << " / ";
break;
case uMinus:
p->p() << "-";
break;
}
if(Act == uMinus) left->Print(p);
else right->Print(p);
}
LogicExpression::LogicExpression(Value *l, Value *r, action a): left(l), right(r), Act(a) {
assert(left);
if(a != Not) assert(right) ;
}
void LogicExpression::Run (ScopePass scope) {
errorTrace("Logic Expression w/o getting value");
switch(Act) {
case And:
if(left->GetValue(scope)->isTrue())
right->Run(scope);
break;
case Or:
if(!left->GetValue(scope)->isTrue())
right->Run(scope);
break;
case Not:
left->Run(scope);
// no right
default:
left->Run(scope);
right->Run(scope);
}
}
ValuePass LogicExpression::GetValue(ScopePass scope) {
errorTrace("Logic Expression");
ValuePass left = this->left->GetValue(scope);
if(Act == Not)
return ValuePass(new ilang::Value( ! left->isTrue()));
ValuePass right = this->right->GetValue(scope);
// cout << "logic check " << left->Get().type().name() << " " << right->Get().type().name() << endl;
// cout << left->str() << " " << right->str() << endl
// << (left->Get().type() == typeid(bool)) << " " << (right->Get().type() == typeid(long)) << endl;
switch(Act) {
case And:
if(left->isTrue())
if(right->isTrue())
return ValuePass(new ilang::Value(true));
return ValuePass(new ilang::Value(false));
break;
case Or:
if(left->isTrue() || right->isTrue()) return ValuePass(new ilang::Value(true));
return ValuePass(new ilang::Value(false));
case Equal:
if(left->Get().type() == typeid(std::string) && right->Get().type() == typeid(std::string))
return ValuePass(new ilang::Value(boost::any_cast<std::string>(left->Get()) == boost::any_cast<std::string>(right->Get())));
if(left->Get().type() == typeid(double)) {
if(right->Get().type() == typeid(double))
return ValuePass(new ilang::Value(boost::any_cast<double>(left->Get()) == boost::any_cast<double>(right->Get())));
else if(right->Get().type() == typeid(long))
return ValuePass(new ilang::Value(boost::any_cast<double>(left->Get()) == boost::any_cast<long>(right->Get())));
else
return ValuePass(new ilang::Value(false));
}else if(left->Get().type() == typeid(long)) {
if(right->Get().type() == typeid(double))
return ValuePass(new ilang::Value(boost::any_cast<long>(left->Get()) == boost::any_cast<double>(right->Get())));
else if(right->Get().type() == typeid(long))
return ValuePass(new ilang::Value(boost::any_cast<long>(left->Get()) == boost::any_cast<long>(right->Get())));
else
return ValuePass(new ilang::Value(false));
}
break;
case Not_Equal:
if(left->Get().type() == typeid(std::string) && right->Get().type() == typeid(std::string))
return ValuePass(new ilang::Value(boost::any_cast<std::string>(left->Get()) != boost::any_cast<std::string>(right->Get())));
if(left->Get().type() == typeid(double)) {
if(right->Get().type() == typeid(double))
return ValuePass(new ilang::Value(boost::any_cast<double>(left->Get()) != boost::any_cast<double>(right->Get())));
else if(right->Get().type() == typeid(long))
return ValuePass(new ilang::Value(boost::any_cast<double>(left->Get()) != boost::any_cast<long>(right->Get())));
else
return ValuePass(new ilang::Value(false));
}else if(left->Get().type() == typeid(long)) {
if(right->Get().type() == typeid(double))
return ValuePass(new ilang::Value(boost::any_cast<long>(left->Get()) != boost::any_cast<double>(right->Get())));
else if(right->Get().type() == typeid(long))
return ValuePass(new ilang::Value(boost::any_cast<long>(left->Get()) != boost::any_cast<long>(right->Get())));
else
return ValuePass(new ilang::Value(false));
}
break;
case Less_Equal:
if(left->Get().type() == typeid(double)) {
if(right->Get().type() == typeid(double))
return ValuePass(new ilang::Value(boost::any_cast<double>(left->Get()) <= boost::any_cast<double>(right->Get())));
else if(right->Get().type() == typeid(long))
return ValuePass(new ilang::Value(boost::any_cast<double>(left->Get()) <= boost::any_cast<long>(right->Get())));
else
return ValuePass(new ilang::Value(false));
}else if(left->Get().type() == typeid(long)) {
if(right->Get().type() == typeid(double))
return ValuePass(new ilang::Value(boost::any_cast<long>(left->Get()) <= boost::any_cast<double>(right->Get())));
else if(right->Get().type() == typeid(long))
return ValuePass(new ilang::Value(boost::any_cast<long>(left->Get()) <= boost::any_cast<long>(right->Get())));
else
return ValuePass(new ilang::Value(false));
}
break;
case Greater_Equal:
if(left->Get().type() == typeid(double)) {
if(right->Get().type() == typeid(double))
return ValuePass(new ilang::Value(boost::any_cast<double>(left->Get()) >= boost::any_cast<double>(right->Get())));
else if(right->Get().type() == typeid(long))
return ValuePass(new ilang::Value(boost::any_cast<double>(left->Get()) >= boost::any_cast<long>(right->Get())));
else
return ValuePass(new ilang::Value(false));
}else if(left->Get().type() == typeid(long)) {
if(right->Get().type() == typeid(double))
return ValuePass(new ilang::Value(boost::any_cast<long>(left->Get()) >= boost::any_cast<double>(right->Get())));
else if(right->Get().type() == typeid(long))
return ValuePass(new ilang::Value(boost::any_cast<long>(left->Get()) >= boost::any_cast<long>(right->Get())));
else
return ValuePass(new ilang::Value(false));
}
break;
case Less:
if(left->Get().type() == typeid(double)) {
if(right->Get().type() == typeid(double))
return ValuePass(new ilang::Value(boost::any_cast<double>(left->Get()) < boost::any_cast<double>(right->Get())));
else if(right->Get().type() == typeid(long))
return ValuePass(new ilang::Value(boost::any_cast<double>(left->Get()) < boost::any_cast<long>(right->Get())));
else
return ValuePass(new ilang::Value(false));
}else if(left->Get().type() == typeid(long)) {
if(right->Get().type() == typeid(double))
return ValuePass(new ilang::Value(boost::any_cast<long>(left->Get()) < boost::any_cast<double>(right->Get())));
else if(right->Get().type() == typeid(long))
return ValuePass(new ilang::Value(boost::any_cast<long>(left->Get()) < boost::any_cast<long>(right->Get())));
else
return ValuePass(new ilang::Value(false));
}
break;
case Greater:
if(left->Get().type() == typeid(double)) {
if(right->Get().type() == typeid(double))
return ValuePass(new ilang::Value(boost::any_cast<double>(left->Get()) > boost::any_cast<double>(right->Get())));
else if(right->Get().type() == typeid(long))
return ValuePass(new ilang::Value(boost::any_cast<double>(left->Get()) > boost::any_cast<long>(right->Get())));
else
return ValuePass(new ilang::Value(false));
}else if(left->Get().type() == typeid(long)) {
if(right->Get().type() == typeid(double))
return ValuePass(new ilang::Value(boost::any_cast<long>(left->Get()) > boost::any_cast<double>(right->Get())));
else if(right->Get().type() == typeid(long))
return ValuePass(new ilang::Value(boost::any_cast<long>(left->Get()) > boost::any_cast<long>(right->Get())));
else
return ValuePass(new ilang::Value(false));
}
break;
}
return ValuePass(new ilang::Value(false));
}
void LogicExpression::Print(Printer *p) {
if(Act == Not) {
p->p() << "!";
left->Print(p);
return;
}
left->Print(p);
switch(Act) {
case Equal:
p->p() << " == ";
break;
case Not_Equal:
p->p() << " != ";
break;
case Less_Equal:
p->p() << " <= ";
break;
case Greater_Equal:
p->p() << " >= ";
break;
case Less:
p->p() << " < ";
break;
case Greater:
p->p() << " < ";
break;
case And:
p->p() << " && ";
break;
case Or:
p->p() << " || ";
break;
}
right->Print(p);
}
SingleExpression::SingleExpression(Variable *target, Value *value, action a) : m_target(target), m_value(value), Act(a) {
assert(target);
assert(value);
}
void SingleExpression::Run(ScopePass scope) {
ValuePass v = GetValue(scope);
}
ValuePass SingleExpression::GetValue(ScopePass scope) {
errorTrace("Single Expression");
ValuePass val = m_value->GetValue(scope);
ValuePass setTo = m_target->GetValue(scope);
ValuePass ret;
if(setTo->Get().type() == typeid(std::string)) {
error(Act == add, "Can only add to a string");
ret = ValuePass(new ilang::Value(boost::any_cast<std::string>(setTo->Get()) + val->str()));
m_target->Set(scope, ret);
return ret;
}
if(val->Get().type() == typeid(long)) {
long n = boost::any_cast<long>(val->Get());
switch(Act) {
case add:
if(setTo->Get().type() == typeid(long)) {
ret = ValuePass(new ilang::Value(boost::any_cast<long>(setTo->Get()) + n));
}else if(setTo->Get().type() == typeid(double)) {
ret = ValuePass(new ilang::Value(boost::any_cast<double>(setTo->Get()) + n));
}else{
error(0, "Can't perform math on non numbers");
}
break;
case subtract:
if(setTo->Get().type() == typeid(long)) {
ret = ValuePass(new ilang::Value(boost::any_cast<long>(setTo->Get()) - n));
}else if(setTo->Get().type() == typeid(double)) {
ret = ValuePass(new ilang::Value(boost::any_cast<double>(setTo->Get()) - n));
}else{
error(0, "Can't perform math on non numbers");
}
break;
case multiply:
if(setTo->Get().type() == typeid(long)) {
ret = ValuePass(new ilang::Value(boost::any_cast<long>(setTo->Get()) * n));
}else if(setTo->Get().type() == typeid(double)) {
ret = ValuePass(new ilang::Value(boost::any_cast<double>(setTo->Get()) * n));
}
break;
case divide:
if(setTo->Get().type() == typeid(long)) {
ret = ValuePass(new ilang::Value(boost::any_cast<long>(setTo->Get()) / n));
}else if(setTo->Get().type() == typeid(double)) {
ret = ValuePass(new ilang::Value(boost::any_cast<double>(setTo->Get()) / n));
}else{
error(0, "Can't perform math on non numbers");
}
break;
default:
assert(0);
}
m_target->Set(scope, ret);
return ret;
}else if(val->Get().type() == typeid(double)) {
double n = boost::any_cast<double>(val->Get());
switch(Act) {
case add:
if(setTo->Get().type() == typeid(double)) {
ret = ValuePass(new ilang::Value(boost::any_cast<double>(setTo->Get()) + n));
}else if(setTo->Get().type() == typeid(long)) {
ret = ValuePass(new ilang::Value(boost::any_cast<long>(setTo->Get()) + n));
}else{