-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecafs.cc
executable file
·1555 lines (1244 loc) · 40.9 KB
/
decafs.cc
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
//#define Tokens
#include <iostream>
#include <fstream>
#include <vector>
//#include "tokentype.h"
#include "parsetree.h"
#include "semantics.h" // for variable scopes and semantics objects
extern FILE* yyin;
extern Token *myTok;
extern int yylineno;
extern ParseTree *top;
extern int yydebug;
using namespace std;
int yylex();
int yyparse();
ParseTree * parse_decaf(FILE *);
//global declarations of pass2 so all functions can call it
void pass2(ParseTree * current);
void generating(ParseTree * tree, fstream & file);
int loopcount = 0;
bool is_in_a_class = false;
Symtab * scope;
S_function * currfunc = NULL;
S_class * currclass;
int curr_varnum = 0;
/*struct Holder{
int itype;
float dtype;
bool btype;
string stype;
};*/
string cut_extension(string name){
string cut = "";
bool adding = false;
for (int i = name.size(); i >= 0; i--){
char letter = name[i];
if (adding){
cut = letter + cut;
}
else{
if (letter == '.'){
adding = true;
}
}
}
return cut;
}
char codetype(S_type * type){
//can be primtype, arraytype, class, or interface
if (type == NULL){
return 'V';
}
S_primtype * ptype = dynamic_cast<S_primtype *>(type);
if (ptype){
string thetype = ptype->name;
char typeletter = thetype[0];
typeletter = typeletter - 32;
if (typeletter == 'B'){
return 'Z';
}
return typeletter;
}
return 'T';
}
//not space efficient
//MIGHT need to double this in case everything is a double
int stackneeded(ParseTree * current){
if (current->children.size() == 0){
return 1;
}
int max_depth = 0;
for (ParseTree * child: current->children){
int branchdepth = stackneeded(child);
if (branchdepth > max_depth){
max_depth = branchdepth;
}
}
return 1 + max_depth;
}
int localsneeded(ParseTree * current){
S_function * func = dynamic_cast<S_function *>(current->sem);
int vars = 0;
if (is_in_a_class) {
vars += 1;
}
vars += func->formals.size();
vars += current->children[3]->children[0]->children.size();
return vars * 2;
}
void generating_functions(ParseTree * current, fstream & file){
S_function * func = dynamic_cast<S_function *>(current->sem);
file << ".method static ";
file << func->name << "(";
for (S_variable * argument : func->formals){
cout << codetype(argument->type) << endl;
}
file << ")";
file << codetype(func->returnType) << endl;
file << " .limit stack ";
file << stackneeded(current->children[3]) << endl;
file << " .limit locals ";
file << localsneeded(current) << endl;
generating(current->children[3], file);
file << " return" << endl;
file << ".end method" << endl;
}
/*vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv*/
string find_type(ParseTree * current){
S_primtype * ptype = dynamic_cast<S_primtype *>(current->type);
return ptype->name;
}
string evaluate_string(ParseTree * current){
return current->token->text;
}
int evaluate_int(ParseTree * current){
//how do I handle readint?
if(current->children.size() == 0){
//only works if not a variable!!
return stoi(current->token->text);
}
int value = 0;
if(current->description == "binop"){
string op = current->children[1]->token->text;
int lval = evaluate_int(current->children[0]);
int rval = evaluate_int(current->children[2]);
if (op == "+"){
value = lval + rval;
}
else if (op == "-"){
value = lval - rval;
}
else if (op == "/"){
value = lval / rval;
}
else if (op == "*"){
value = lval * rval;
}
else if (op == "%"){
value = lval % rval;
}
}
else if(current->description == "uop"){
//negation
int absvalue = evaluate_int(current->children[1]);
value = value - absvalue;
}
//cout << value << endl;
return value;
}
float evaluate_double(ParseTree * current){
//how do I handle readint?
if(current->children.size() == 0){
//only works if not a variable!!
return stof(current->token->text);
}
float value = 0;
if(current->description == "binop"){
string op = current->children[1]->token->text;
float lval = evaluate_int(current->children[0]);
float rval = evaluate_int(current->children[2]);
if (op == "+"){
value = lval + rval;
}
else if (op == "-"){
value = lval - rval;
}
else if (op == "/"){
value = lval / rval;
}
else if (op == "*"){
value = lval * rval;
}
//might need to find a way to make mods work for doubles
else if (op == "%"){
cout << "Cannot use modulus operator on doubles" << endl;
exit(1);
}
}
else if(current->description == "uop"){
//negation
float absvalue = evaluate_double(current->children[1]);
value = value - absvalue;
}
return value;
}
int evaluate_bool(ParseTree * current){
int value = 0;
if(current->children.size() == 0){
//only works if not a variable!!
string trueorfalse = current->token->text;
if(trueorfalse == "false"){
return 0;
}
else{
return 1;
}
}
if(current->description == "binop"){
string op = current->children[1]->token->text;
string lvaltype = find_type(current->children[0]);
//string rvaltype = find_type(current->children[2]);
//focuses on operands
/*Holder * left = new Holder;
Holder * right = new Holder;
if (lvaltype == "int"){
left->itype = evaluate_int(current->children[0]);
right->itype = evaluate_int(current->children[2]);
}
else if(lvaltype == "double"){
left->dtype = evaluate_double(current->children[0]);
right->dtype = evaluate_double(current->children[2]);
}
else if(lvaltype == "bool"){
left->btype = evaluate_bool(current->children[0]);
right->btype = evaluate_bool(current->children[2]);
}
else if(lvaltype == "string"){
left->stype = evaluate_string(current->children[0]);
right->stype = evaluate_string(current->children[2]);
}
//focuses on operator
if(op == "<" || op == "<=" || op == ">" || op == ">="){
//must be matching numbers
}
else if(op == "==" || op == "!="){
//must be same type
}
else if(op == "&&"){
//operands must be of bool type
}
else if(op == "||"){
}*/
if (lvaltype == "int"){
int lval = evaluate_int(current->children[0]);
int rval = evaluate_int(current->children[2]);
//must be matching numbers
if(op == "<"){
return (lval < rval);
}
else if (op == "<="){
return (lval <= rval);
}
else if (op == ">"){
return (lval > rval);
}
else if (op == ">="){
return (lval >= rval);
}
//must be same type
else if (op == "=="){
return (lval == rval);
}
else if (op == "!="){
return (lval != rval);
}
//operands must be of bool type
//only here to assist with copy/paste later
/*else if(op == "&&"){
return (lval && rval);
}
else if(op == "||"){
return (lval && rval);
}*/
}
else if(lvaltype == "double"){
float lval = evaluate_double(current->children[0]);
float rval = evaluate_double(current->children[2]);
if(op == "<"){
return (lval < rval);
}
else if (op == "<="){
return (lval <= rval);
}
else if (op == ">"){
return (lval > rval);
}
else if (op == ">="){
return (lval >= rval);
}
//must be same type
else if (op == "=="){
return (lval == rval);
}
else if (op == "!="){
return (lval != rval);
}
}
else if(lvaltype == "bool"){
bool lval = evaluate_bool(current->children[0]);
bool rval = evaluate_bool(current->children[2]);
//must be same type
if (op == "=="){
return (lval == rval);
}
else if (op == "!="){
return (lval != rval);
}
//operands must be of bool type
else if(op == "&&"){
return (lval && rval);
}
else if(op == "||"){
return (lval && rval);
}
}
else if(lvaltype == "string"){
string lval = evaluate_string(current->children[0]);
string rval = evaluate_string(current->children[2]);
//must be same type
if (op == "=="){
return (lval == rval);
}
else if (op == "!="){
return (lval != rval);
}
}
}
else if(current->description == "uop"){
//negation
int absvalue = evaluate_bool(current->children[1]);
if (absvalue == 0){
return 1;
}
else{
return 0;
}
}
return value;
}
/*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
void generating_print(ParseTree * current, fstream & file){
file << " getstatic java/lang/System/out Ljava/io/PrintStream;" << endl;
S_type * printtype = current->children[1]->children[0]->type;
S_primtype * ptype = dynamic_cast<S_primtype *>(printtype);
if (ptype){
if (ptype->name == "string"){
string output = current->children[1]->children[0]->token->text;
file << " ldc " << output << endl;
file << " invokevirtual java/io/PrintStream/println(Ljava/lang/String;)V" << endl;
}
if (ptype->name == "bool"){
int value = evaluate_bool(current->children[1]->children[0]);
file << " iconst_" << value << endl;
file << " invokevirtual java/io/PrintStream/print(Z)V" << endl;
}
if (ptype->name == "int"){
int value = evaluate_int(current->children[1]->children[0]);
if ((value > -1) and (value < 6)){
file << " iconst_" << value << endl;
}
else{
file << " bipush " << value << endl;
}
file << " invokevirtual java/io/PrintStream/println(I)V" << endl;
}
if (ptype->name == "double"){
//DOUBLE THE FREAK DOWN ON THIS ONE
float value = evaluate_double(current->children[1]->children[0]);
file << " ldc2_w " << value << endl;
file << " invokevirtual java/io/PrintStream/println(D)V" << endl;
}
}
}
void generating_binops(ParseTree * current, fstream & file){
if (current->children[1]->token->text == "="){
S_primtype * ptype = dynamic_cast<S_primtype *>(current->children[2]->type);
//rhs
if (ptype){
if (ptype->name == "string"){
string output = current->children[2]->token->text;
file << " ldc " << output << endl;
}
if (ptype->name == "int"){
int value = evaluate_int(current->children[2]);
if ((value > -1) and (value < 6)){
file << " iconst_" << value << endl;
}
else{
file << " bipush " << value << endl;
}
}
}
//lhs
S_variable * lvalue = dynamic_cast<S_variable *>(current->children[0]->sem);
if(lvalue){
cout << lvalue->name << endl;
//genus options: local, global, instance
if (lvalue->genus == "local"){
file << " istore_" << lvalue->seq_num << endl;
}
}
}
}
void generating_if(ParseTree * current, fstream & file){
if (current){
}
if (file){
}
}
void generating(ParseTree * current, fstream & file){
if (current->description == "functiondecl"){
//compatible return value
generating_functions(current, file);
}
else if(current->description == "print"){
generating_print(current, file);
}
else if(current->description == "binop"){
//only do assign for now
generating_binops(current, file);
}
else if (current->description == "if"){
generating_if(current, file);
}
else{
for (ParseTree * child : current->children){
generating(child, file);
}
}
/*if (current->description.size() == 0){
pass2_const_or_var(current);
}
else if (current->description == "return"){
pass2_return(current);
}
else if (current->description == "class"){
//class object valid in class being declared: class A { A a; }
//Instance variable lookup is used when scope lookup fails. Inherited instance variables are found
Symtab * oldscope = scope;
scope = current->symtab;
pass2_class(current);
scope = oldscope;
}
else if (current->description == "interface"){
Symtab * oldscope = scope;
scope = current->symtab;
pass2_interface(current);
scope = oldscope;
}
//else if (current->description == "extends"){}
//else if (current->description == "implements"){}
else if (current->description == "fields"){
pass2_fields(current);
}
else if (current->description == "variable"){
pass2_variables(current);
}
else if (current->description == "formals"){
pass2_formals(current);
}
else if (current->description == "stmtblock"){
Symtab * oldscope = scope;
scope = current->symtab;
pass2_stmtblock(current);
scope = oldscope;
}
else if (current->description == "vardecls"){
pass2_vardecls(current);
}
else if (current->description == "stmts"){
pass2_stmts(current);
}
else if (current->description == "if"){
//expr is a bool
pass2_ifstmts(current);
}
else if (current->description == "while"){
//expr is a bool
pass2_whilestmts(current);
}
else if (current->description == "for"){
//check the three exprs (looks just like a c++ for loop)
pass2_forloops(current);
}
else if (current->description == "break"){
pass2_break();
}
else if (current->description == "print"){
pass2_print(current);
}
//else if (current->description == "nullstmt"){} ** might need to make type null or something
else if (current->description == "uop"){
//type match
pass2_uop(current);
}
else if (current->description == "readinteger"){
pass2_readint(current);
}
else if (current->description == "new"){
//valid parameters
pass2_new(current);
}
else if (current->description == "newarray"){
//valid parameters
pass2_newarray(current);
}
else if (current->description == "field_access"){
//valid field access
pass2_field_access(current);
}
else if (current->description == "aref"){
//type check (integer?)
pass2_aref(current);
}
else if (current->description == "call"){
//actuals in a function call compatible with declared formal types
//class object accepted in place of an interface object when passing actuals
//valid/invalid interface object function call
pass2_call(current);
}
else if (current->description == "actuals"){
pass2_actuals(current);
}
else if (current->description == "usertype"){
pass2_type(current);
}
else if (current->description == "primtype"){
pass2_type(current);
}
else if (current->description == "arraytype"){
pass2_type(current);
}
// else if(current->token){
// if (current->token->text == "this"){
//
// }
// }
*/
}
void write_header(string simpname, string filename, fstream & file){
file << ".source " << filename << endl;
file << ".class public " << simpname << endl;
file << ".super java/lang/Object" << endl;
file << endl << endl;
file << ".method public <init>()V" << endl;
file << " .limit stack 1" << endl;
file << " .limit locals 1" << endl;
file << " .line 1" << endl;
file << " aload_0" << endl;
file << " invokespecial java/lang/Object/<init>()V" << endl;
file << " return" << endl;
file << ".end method" << endl;
file << endl;
}
void write_main(string simpname, fstream & file){
file << ".method public static main([Ljava/lang/String;)V" << endl;
file << " .limit stack 0" << endl;
file << " .limit locals 1" << endl;
file << " invokestatic " << simpname << "/main()V" << endl;
file << " return" << endl;
file << ".end method" << endl << endl;
}
void code_generation(string filename){
string simpname = cut_extension(filename);
fstream mainfile;
mainfile.open(simpname + ".j", ios::out);
if (!mainfile){
cout << "Error creating file" << endl;
exit(1);
}
write_header(simpname, filename, mainfile);
write_main(simpname, mainfile);
generating(top, mainfile);
}
bool is_compatible(S_type * lhs, S_type * rhs){
S_class * lclass = dynamic_cast<S_class *>(lhs);
S_interface * linterface = dynamic_cast<S_interface *>(lhs);
S_primtype * lprim = dynamic_cast<S_primtype *>(lhs);
S_class * rclass = dynamic_cast<S_class *>(rhs);
S_interface * rinterface = dynamic_cast<S_interface *>(rhs);
S_primtype * rprim = dynamic_cast<S_primtype *>(rhs);
if (lprim || rprim){
if (!lprim){
semantic_error("Non-matching types for assignment expression", 0);
}
else if (!rprim){
semantic_error("Non-matching types for assignment expression", 0);
}
else if(lprim->name != rprim->name){
semantic_error("Non-matching types for assignment expression", 0);
}
}
if (lclass && rclass){
if(lclass != rclass){
bool is_super = false;
for (S_class * parent : rclass->inheritedClasses){
if (lclass == parent){
is_super = true;
}
}
if (!is_super){
semantic_error("Incompatible types", 0);
}
}
}
if (lclass && rinterface){
semantic_error("Attempting to pass an interface where a class is expected", 0);
}
if (linterface && rclass){
bool implemented = false;
for (S_type * interf : rclass->interfaces){
S_interface * interface = dynamic_cast<S_interface *>(interf);
if (interface == linterface){
implemented = true;
}
}
if (!implemented){
for (S_class * parent : rclass->inheritedClasses){
for (S_type * interf : parent->interfaces){
S_interface * interface = dynamic_cast<S_interface *>(interf);
if (interface == linterface){
implemented = true;
}
}
}
}
if(!implemented){
semantic_error("Attempting to pass an incompatible class when an interface is expected", 0);
}
}
if (linterface && rinterface){
if (linterface != rinterface){
semantic_error("Incompatible interfaces", 0);
}
}
return true;
}
//probably needs to be fixed?
void pass2_const_or_var(ParseTree * current){
string text = current->token->text;
if(text == "this"){
if (!currclass){
semantic_error("cannot use 'this' outside of a class", 0);
}
}
else{
S_primtype * constant = dynamic_cast<S_primtype *>(current->sem);
if (constant){
current->type = constant;
}
else{
semantics * found = scope->lookup(text);
if (!found){
semantic_error("Undefined variable", 0);
}
S_variable * var = dynamic_cast<S_variable *>(found);
if (var){
current->type = var->type;
current->sem = var;
}
/*else{
for(semantics *sem : all_decls){
S_class * isclass = dynamic_cast<S_class *>(sem);
S_interface * isinterface = dynamic_cast<S_interface *>(sem);
if (isclass){
if (isclass->name == current->token->text){
current->type = isclass;
}
}
if (isinterface){
if (isinterface->name == current->token->text){
current->type = isinterface;
}
}
}
}*/
}
}
}
void pass2_functions(ParseTree * functree){
//checks for correct return value
S_function * function = dynamic_cast<S_function *>(functree->sem);
currfunc = function;
bool returned = false;
//calls pass 2 on formals
pass2(functree->children[2]);
pass2(functree->children[3]);
//check that the return matches (goes through stmtblock, 4th child)
ParseTree * stmts = functree->children[3]->children[1];
for(ParseTree * child : stmts->children){
//pass2(child);
if (child->description == "return"){
returned = true;
if(function->returnType == child->type){
functree->type = child->type;
}
else{
semantic_error("Return of incorrect types", function->line);
}
}
}
if (function->returnType && !returned){
semantic_error("No return in non-void function", function->line);
}
currfunc = NULL;
}
void pass2_return(ParseTree * retree){
if(retree->children.size() > 1){
ParseTree * ret_stmt = retree->children[1];
pass2(ret_stmt);
retree->type = ret_stmt->type;
}
else{
retree->type = NULL;
}
}
void pass2_binop(ParseTree * bintree){
string op = bintree->children[1]->token->text;
ParseTree * left = bintree->children[0];
ParseTree * right = bintree->children[2];
pass2(left);
pass2(right);
if(op == "+" || op == "-" || op == "/" || op == "*" || op == "%"){
//arithmetic type match
if(left->type->name != right->type->name){
semantic_error("Non-matching types for arithmetic expression", 0);
}
string type = left->type->name;
if(type != "int" && type != "double"){
semantic_error("Non-numeric types for arithmetic expression", 0);
}
bintree->type = left->type;
}
else if(op == "<" || op == "<=" || op == ">" || op == ">="){
//relational types match int or double
if(left->type->name != right->type->name){
semantic_error("Non-matching types for relational expression", 0);
}
string type = left->type->name;
if(type != "int" && type != "double"){
semantic_error("Non-numeric types for relational expression", 0);
}
bintree->type = new S_primtype("bool");
}
else if(op == "==" || op == "!="){
//equality types match
if (! is_compatible(left->type, right->type)){
semantic_error("Non-matching types for relational expression", 0);
}
bintree->type = new S_primtype("bool");
}
else if(op == "="){
//assignment type compatibility
//Interface assignment type compatibility
S_primtype * isprim = dynamic_cast<S_primtype *>(left->type);
if (isprim){
if(left->type->name != right->type->name){
semantic_error("Non-matching types for assignment expression", 0);
}
bintree->type = left->type;
}
else if(left->type->name != right->type->name){
if(is_compatible(left->type, right->type)){
bintree->type = left->type;
}
else{
semantic_error("Non-matching types for assignment expression", 0);
}
}
}
else if(op == "&&" || op == "||"){
//logical types match
if(left->type->name != "bool" || right->type->name != "bool"){
semantic_error("Non boolean values used in logical expression", 0);
}
bintree->type = new S_primtype("bool");
}
}
void pass2_uop(ParseTree * optree){
string op = optree->children[0]->token->text;
pass2(optree->children[1]);
if(op == "-"){
if(optree->children[1]->type->name != "int" && optree->children[1]->type->name != "double"){
semantic_error("Non-numeric type used with negation operator", 0);
}
optree->type = optree->children[1]->type;
}
if(op == "!"){
if(optree->children[1]->type->name != "bool"){
semantic_error("Non-boolean type used with not operator", 0);
}
optree->type = new S_primtype("bool");
}
}
void pass2_variables(ParseTree * vartree){
S_variable * variable = dynamic_cast<S_variable *>(vartree->sem);
if((scope != topScope) and (currfunc)){
//find a way to check if we are in a function! (make sure we are tracking class, currfunc, and currclass)
//GO INTO FUNCTIONDECL AND MAKE SURE TO RESET TO 0 (OR 1 IF IN A CLASS)
variable->seq_num = curr_varnum;
curr_varnum += 1;
variable->genus = "local";
}
else if(is_in_a_class){
variable->genus = "instance";
}
else{
variable->genus = "global";
}
vartree->sem = variable;