-
Notifications
You must be signed in to change notification settings - Fork 0
/
parserInterp.cpp
1432 lines (1145 loc) · 36 KB
/
parserInterp.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
/**
* Jack Robbins
* Recursive-Descent Parser and Interpreter for a Pascal-Like language
* See README for full details
*/
#include "parserInterp.h"
#include <iostream>
#include <set>
// defVar keeps track of all variables that have been defined in the program thus far
map<string, bool> defVar;
// SymTable keeps track of the type for all of our variables
map<string, Token> SymTable;
//Container of temporary locations of Value objects for results of expressions, variables values and constants
//Key is a variable name, value is its Value. Holds all variables defined by assign statements
map<string, Value> TempsResults;
//declare a pointer variable to a queue of Value objects
queue <Value> * ValQue;
//The parser namespace that interacts with lex for us
namespace Parser {
bool pushed_back = false;
LexItem pushed_token;
static LexItem GetNextToken(istream& in, int& line) {
if( pushed_back ) {
pushed_back = false;
return pushed_token;
}
return getNextToken(in, line);
}
static void PushBackToken(LexItem & t) {
if( pushed_back ) {
abort();
}
pushed_back = true;
pushed_token = t;
}
}
//Initialize error count to be 0
static int error_count = 0;
// A simple wrapper that allows access to the number of syntax errors
int ErrCount(){
return error_count;
}
//A simple error wrapper that incrememnts error count, and prints out the error
void ParseError(int line, string msg)
{
++error_count;
cout << line << ": " << msg << endl;
}
/**
* Prog is the entry point to our entire interpreter, the "root" of our parse tree
* To start, the program must use the keyword Program and give an identifier name.
* It must then go into the Declaritive part followed by a compound statement
* Prog ::= PROGRAM IDENT ; DeclPart CompoundStmt
*/
bool Prog(istream& in, int& line){
bool status = false;
//This should be the keyword "program"
LexItem l = Parser::GetNextToken(in, line);
//We're missing the required program keyword, throw an error and exit
if (l != PROGRAM){
ParseError(line, "Missing PROGRAM keyword.");
return false;
} else {
//We have the program keyword, move on to more processing
l = Parser::GetNextToken(in, line);
//This token should be an IDENT if all is correct, if not we have an error
if (l != IDENT){
ParseError(line, "Missing Program name.");
return false;
}
//If we're at this point we have PROGRAM IDENT, need a semicol
l = Parser::GetNextToken(in, line);
//If there's no semicolon, syntax error
if (l != SEMICOL) {
ParseError(line, "Syntax Error.");
return false;
}
//By this point, we have checked up to PROGRAM IDENT ;
//Check the DeclPart
status = DeclPart(in, line);
//If the declaration was bad, no point in continuing
if (!status){
ParseError(line, "Incorrect Declaration Section.");
return false;
}
//Up to here we have gotten PROGRAM IDENT ; DeclPart
//Check for the compound statement, make sure that there actually is a BEGIN
l = Parser::GetNextToken(in, line);
if (l != BEGIN){
ParseError(line, "Syntactic Error in Declaration Block.");
ParseError(line, "Incorrect Declaration Section");
return false;
}
//If we have BEGIN, consume it and call CompoundStmt
status = CompoundStmt(in, line);
//If the compound statement was bad, return false
if (!status){
ParseError(line, "Incorrect Program Body.");
return false;
}
}
//There could also be some unrecognizable token here
if (l == ERR) {
ParseError(line, "Unrecognized input pattern.");
cout << "(" << l.GetLexeme() << ")" << endl;
return false;
}
//If we reach here, status will be true and parsing will have been successful
return status;
}
/**
* The declarative part must start with the var keyword, followed by one or more colon separated declStmt's
* There will be no actual value processing, that is handled further down the parse tree
* DeclPart ::= VAR DeclStmt; { DeclStmt ; }
*/
bool DeclPart(istream& in, int& line){
bool status = false;
LexItem l = Parser::GetNextToken(in, line);
//This first token should be VAR, if not throw an error
if (l != VAR){
ParseError(line, "Non-recognizable Declaration Part.");
return false;
}
//Once we're here, we should be seeing DeclStmt's followed by SEMICOLs
//There can be as many as we like, so use iteration
//We will use this lexitem to look ahead
LexItem lookAhead = Parser::GetNextToken(in, line);
while(lookAhead == IDENT){
//Once we know its an ident, put it back for processing by DeclStmt
Parser::PushBackToken(lookAhead);
//DeclStmt processing
status = DeclStmt(in, line);
//If its a bad DeclStmt, throw error
if (!status) {
ParseError(line, "Syntactic error in Declaration Block.");
return false;
}
//We had a good DeclStmt, it has to be followed by a semicol
l = Parser::GetNextToken(in, line);
//If no semicolon, throw syntax error
if (l != SEMICOL){
//error right here
ParseError(line, "Syntactic error in Declaration Block.");
return false;
}
//Update lookahead, this will tell us if we have more declstmts
lookAhead = Parser::GetNextToken(in, line);
}
//If we get here, lookAhead must not have been an IDENT. We need to put it back for processing by the CompoundStmt block
Parser::PushBackToken(lookAhead);
//If we get here, our DeclPart will have been successful
return status;
}
/**
* A delcaration statement can have one or more comma separated identifiers, followed by a valid type and an optional assignment
* DeclStmt ::= IDENT {, IDENT } : Type [:= Expr]
*/
bool DeclStmt(istream& in, int& line){
//All of the variables in a declstmt are going to have the same type, store in a set for type assignment
set<string> tempSet;
//The token that may be used for type checking after the optional ASSOP
Token t;
//Dummy lexItem to make the first iteration of the while loop run
LexItem lookAhead = LexItem(COMMA, ",", 0);
LexItem l;
//We should see an IDENT first
while (lookAhead == COMMA) {
l = Parser::GetNextToken(in, line);
//l must be an IDENT
if (l != IDENT) {
ParseError(line, "Non-indentifier declaration.");
return false;
}
//If this variable is already in defVars, we have a redeclaration, throw error
if (defVar.find(l.GetLexeme()) -> second){
ParseError(line, "Variable Redefinition");
ParseError(line, "Incorrect identifiers list in Declaration Statement.");
return false;
}
//If we get here, it wasn't in defVars, so we should add it
defVar.insert(pair<string, bool> (l.GetLexeme(), true));
//Put the variable name into the tempSet for type processing
tempSet.insert(l.GetLexeme());
lookAhead = Parser::GetNextToken(in, line);
}
//If we're out of the loop, we know it wasn't a comma
//If there's an ident after this, then we know the user forgot to put a comma in between
if (lookAhead == IDENT){
ParseError(line, "Missing comma in declaration statement");
//Having this would also make it a bad identifier list, so return this error as well
ParseError(line, "Incorrect identifiers list in Declaration Statement.");
return false;
}
//If its not a colon at this point, there's some syntax error here
//Let caller handle
if (lookAhead != COLON){
return false;
}
//following this, we need to have a type for our variables
//The next token should be a valid type
l = Parser::GetNextToken(in, line);
//allowed to be integer, boolean, real, string
if (l == STRING || l == INTEGER || l == REAL || l == BOOLEAN){
for(auto i : tempSet){
//symtable keeps track of the type for all variables
SymTable[i] = l.GetToken();
}
//Save the token value for type checking
t = l.GetToken();
} else {
//Unrecognized type
ParseError(line, "Incorrect Declaration Type.");
return false;
}
//Once we get here, we have found and put them all in the symtable
//DeclStmt ::= IDENT {, IDENT } : Type
//After type, there is an optional ASSOP, so get the next token to check
l = Parser::GetNextToken(in,line);
//If we find the optional ASSOP, process it
if (l == ASSOP){
//This is the value reference that we will pass into our expr
Value val;
bool status = Expr(in, line, val);
//Throw an error if Expr fails
if (!status) {
ParseError(line, "Invalid expression following assignment operator.");
return false;
}
//If we get here, we need to do type checking and then assign every variable the value found by expr
switch(t){
case STRING:
//Types must match for this to work
if(val.IsString()){
for(auto var : tempSet){
//Everything in our declstmt will have the same type, so set it all to that type
TempsResults[var] = val;
}
break;
} else {
//Otherwise we fail here
ParseError(line, "Illegal Assignment Operation");
return false;
}
case BOOLEAN:
//Types also must match for booleans
if(val.IsBool()){
for(auto var : tempSet){
TempsResults[var] = val;
}
break;
} else {
ParseError(line, "Illegal Assignment Operation");
return false;
}
case REAL:
//they match, nothing to do here
if(val.IsReal()){
for(auto var : tempSet){
TempsResults[var] = val;
}
break;
//here is the special case, cast to the type of the LHS(t in our case)
} else if(val.IsInt()){
val.SetReal((float)val.GetInt());
val.SetType(VREAL);
for(auto var : tempSet){
TempsResults[var] = val;
}
break;
} else {
ParseError(line, "Illegal Assignment Operation");
return false;
}
case INTEGER:
//they match, nothing to do here
if(val.IsInt()){
for(auto var : tempSet){
TempsResults[var] = val;
}
break;
//another special case here, cast to the LHS of INT in this case
} else if(val.IsReal()) {
val.SetInt((int)val.GetReal());
val.SetType(VINT);
for(auto var : tempSet){
TempsResults[var] = val;
}
break;
} else {
ParseError(line, "Illegal Assignment Operation");
return false;
}
default:
ParseError(line, "Illegal Assignment Operation");
return false;
}
//If its unrecognized throw and error
} else if (l == ERR){
ParseError(line, "Unrecognized input pattern.");
cout << "(" << l.GetLexeme() << ")";
return false;
//If we get here, l was not the optional ASSOP or ERR, push token back and return
} else {
Parser::PushBackToken(l);
}
return true;
}
/**
* Stmt is responsible for determining what kind of stmt we have and making appropriate calls
* Grammar Rules
* Stmt ::= SimpleStmt | StructuredStmtStmt
* SimpleStmt ::= AssignStmt | WriteLnStmt | WriteStmt
* StructuredStmt ::= IfStmt | CompoundStmt
*/
bool Stmt(istream& in, int& line) {
bool status;
// Get the next lexItem from the instream and analyze it
LexItem l = Parser::GetNextToken(in, line);
// If l is uncrecognizable, no use in checking anything
if (l == ERR){
ParseError(line, "Unrecognized input pattern.");
cout << "(" << l.GetLexeme() << ")";
return false;
}
// Check if we have a structured statement
if (l == BEGIN || l == IF){
//Put token back to be reprocessed
Parser::PushBackToken(l);
return StructuredStmt(in, line);
}
// Check to see if we have a simple statement
// Assignments start with IDENT
if (l == IDENT || l == WRITE || l == WRITELN){
//Put token back to be reprocessed
Parser::PushBackToken(l);
status = SimpleStmt(in, line);
if(!status){
ParseError(line, "Incorrect Simple Statement.");
return false;
}
return status;
}
//We didn't find anything so push the token back
Parser::PushBackToken(l);
//Stmt was not successful if we got here
return false;
}
/**
* stmt will call StructuredStmt if appropriate according to our grammar rules
* StructuredStmt ::= IfStmt | CompoundStmt
*/
bool StructuredStmt(istream& in, int& line){
bool status;
LexItem strd = Parser::GetNextToken(in, line);
switch (strd.GetToken()){
case IF:
status = IfStmt(in, line);
if (!status) {
ParseError(line, "Bad structured statement.");
return false;
}
return true;
//Compound statements begin with in
case BEGIN:
return CompoundStmt(in, line);
default:
//we won't ever get here, added to remove compile warnings
return false;
}
}
/**
* Compound statements start with BEGIN and stop with END
* CompoundStmt ::= BEGIN Stmt {; Stmt } END
*/
bool CompoundStmt(istream& in, int& line){
LexItem l;
//If we got here we already have consumed a BEGIN
bool status = Stmt(in, line);
//If status was bad, no point in continuing
if(!status){
ParseError(line, "Invalid Statement in Compound Statement");
return false;
}
//Get the next token for analysis
l = Parser::GetNextToken(in, line);
//While we have a semicol, keep processing stmts
while(l == SEMICOL){
//Process the next stmt
status = Stmt(in, line);
//If status was bad, no point in continuing
if(!status){
ParseError(line, "Invalid Statement in Compound Statement");
return false;
}
//refresh l
l = Parser::GetNextToken(in, line);
}
//If we get here, we know l was not a SEMICOL
//check for err
if(l == ERR){
ParseError(line, "Unrecognized input pattern.");
cout << "(" << l.GetLexeme() << ")";
return false;
}
//If we have an end, consume it and return true
if(l == END){
//we're done here
return true;
}
//Otherwise, we have no end and this is false
return false;
}
/**
* stmt will call SimpleStmt if appropriate according to our grammar rules
* SimpleStmt ::= AssignStmt | WriteLnStmt | WriteStmt
*/
bool SimpleStmt(istream& in, int& line){
LexItem smpl = Parser::GetNextToken(in, line);
switch (smpl.GetToken()){
//Assignments start with identifiers
case IDENT:
Parser::PushBackToken(smpl);
return AssignStmt(in, line);
case WRITELN:
return WriteLnStmt(in, line);
case WRITE:
return WriteStmt(in, line);
//We won't ever get here, added for compile safety on Vocareum
default:
return false;
}
}
/**
* WriteLnStmt
* WriteLnStmt ::= writeln (ExprList)
* */
bool WriteLnStmt(istream& in, int& line) {
LexItem t;
//A queue to be used to print out all of the values from exprList
ValQue = new queue<Value>;
//Get the first token and ensure its an LPAREN
t = Parser::GetNextToken(in, line);
if( t != LPAREN ) {
ParseError(line, "Missing Left Parenthesis");
return false;
}
//Call ExprList to populate the ValQueue using our pointer
bool ex = ExprList(in, line);
//If ExprList fails we have an error
if( !ex ) {
ParseError(line, "Missing expression list for WriteLn statement");
return false;
}
//finally check for the required RPAREN
t = Parser::GetNextToken(in, line);
if(t != RPAREN ) {
ParseError(line, "Missing Right Parenthesis");
return false;
}
//Evaluate: print out the list of expressions' values once all syntax works
while (!(*ValQue).empty())
{
Value nextVal = (*ValQue).front();
cout << nextVal;
ValQue->pop();
}
//Print the endl since this is a writeln statement
cout << endl;
return ex;
}
/**
* Write statements are the exact same as writelnstmt's, just without the added endl
* WriteStmt ::= write (ExprList)
*/
bool WriteStmt(istream& in, int& line){
//A queue to be used to print out all of the values from exprList
ValQue = new queue<Value>;
//Get the token after the word "write" and check if its an lparen
LexItem t = Parser::GetNextToken(in, line);
//No left parenthesis is an error, create error and exit
if (t != LPAREN) {
ParseError(line, "Missing Left Parenthesis");
return false;
}
//Generate the ExprList recursively
bool expr = ExprList(in, line);
//If no ExprList was gotten create a different error
if (!expr){
ParseError(line, "Missing expression list for Write statement");
return false;
}
//Check for a right parenthesis
t = Parser::GetNextToken(in, line);
//If no RPAREN, syntax error
if (t != RPAREN) {
ParseError(line, "Missing right Parenthesis");
return false;
}
//Evaluate: print out the list of expressions' values once all syntax works
while (!(*ValQue).empty())
{
Value nextVal = (*ValQue).front();
cout << nextVal;
ValQue->pop();
}
return expr;
}
// Processing all IF statements
// IfStmt ::= IF Expr THEN Stmt [ ELSE Stmt ]
bool IfStmt(istream& in, int& line){
LexItem l;
//The val value to be used with Expr
Value val;
//Once this function is called, the IF token has been consumed already
//We should see a valid expression at this point
bool status = Expr(in, line, val);
//if expression is not valid, throw an error
if(!status){
ParseError(line, "Invalid expression in IF statement.");
return false;
}
//If val is not a boolean, that's an error
if(val.GetType() != VBOOL){
ParseError(line, "Expression in IF Statement must be of type Boolean");
return false;
}
l = Parser::GetNextToken(in, line);
//If its unknown, throw error
if (l == ERR ){
ParseError(line, "Unrecognized Input Pattern");
cout << "(" << l.GetToken() << ")" << endl;
return false;
}
//If its not a THEN, we have an error
if (l != THEN) {
ParseError(line, "Missing THEN in IF statement.");
return false;
}
//Once we're here we know we have ::= IF expr = true THEN stmt
//If val is true, execute the stmt and return
if (val.GetBool()){
//Exceute the stmt
status = Stmt(in, line);
//Potential for a bad stmt here
if(!status){
ParseError(line, "Bad Statement in IF Statement");
return false;
}
//we also need to "skip over" the entire else statement if this is the case
l = Parser::GetNextToken(in, line);
//Skip over until l is a semicol
while(l != SEMICOL) {
//Catch any errors we might see while we're at it
if (l == ERR){
ParseError(line, "Unrecognized Input Pattern");
cout << "(" << l.GetToken() << ")" << endl;
return false;
}
//we are in a compound statement, skip until we see end
if (l == BEGIN){
LexItem cs;
while(cs != END){
cs = Parser::GetNextToken(in, line);
}
}
l = Parser::GetNextToken(in, line);
}
//We will have consumed one extra token by here, so return it
Parser::PushBackToken(l);
} else {
//If we get here the val was false, so there are 2 options based on whether or not there is an ELSE
l = Parser::GetNextToken(in, line);
//We want to skip over the statement after then, as it is not being executed
while(l != SEMICOL && l != ELSE){
//Catch any errors we might see while we're at it
if (l == ERR){
ParseError(line, "Unrecognized Input Pattern");
cout << "(" << l.GetToken() << ")" << endl;
return false;
}
//we are in a compound statement, skip until we see end
if (l == BEGIN){
LexItem cs;
while(cs != END){
cs = Parser::GetNextToken(in, line);
}
}
l = Parser::GetNextToken(in, line);
}
//If we get here, we know l is either a SEMICOL or an ELSE
//If we just have a semicol, return and be done
if(l == SEMICOL) {
//put the semicol back, it's not this function's job to procees it
Parser::PushBackToken(l);
return true;
}
//otherwise if we have an else stmt, keep the token and process the stmt
if (l == ELSE){
//Execute the else stmt
status = Stmt(in, line);
//If it fails return false
if(!status){
ParseError(line, "Invalid stmt in IF-ELSE stmt else block");
return false;
}
}
}
//If we make it here, everything worked
return true;
}
/**
* Assignment Statements take in a var, ASSOP and expression
* AssignStmt ::= Var := Expr
*/
bool AssignStmt(istream& in, int& line){
bool status = false;
bool varStatus = false;
LexItem l;
//This will be used for finding types from var
LexItem idtok;
//This will be used for Expr
Value val;
//Check to see the status of the identifier that we have(was it already declared?)
varStatus = Var(in, line, idtok);
//If the variable wasn't correct, we essentially have no variable
if(!varStatus){
ParseError(line, "Missing Left-Hand Side Variable in Assignment statement");
return false;
}
//If we get here, we know we have a valid Var
//Get the next token(should be assop)
l = Parser::GetNextToken(in, line);
//If we have an error token, throw error and exit
if (l == ERR){
ParseError(line, "Unrecognized Input Pattern");
//print out the unrecognized input
cout << "(" << l.GetLexeme() << ")" << endl;
return false;
}
//If we don't have an assop here, no reason in continuing
if (l != ASSOP){
ParseError(line, "Missing Assignment Operator in AssignStmt");
return false;
}
//Once we're here, we know we have Valid Var :=, now analyze the expr
status = Expr(in, line, val);
//If there's no expression, thats an error
if (!status){
ParseError(line, "Missing Expression in Assignment Statement");
return false;
}
//Once we're here, we know we have Var := Expr, but we don't know if our types match, so do type checking
//using our idtok, which has the variable name as its lexeme
string var = idtok.GetLexeme();
//right now, idtok is holding the type of the valid variable, and val is holding the the value gotten from some valid expr
switch(idtok.GetToken()){
case STRING:
//Types must match for this to work
if(val.IsString()){
TempsResults[var] = val;
return true;
}
break;
case BOOLEAN:
//Types also must match for booleans
if(val.IsBool()){
TempsResults[var] = val;
return true;
}
break;
case REAL:
//they match, nothing to do here
if(val.IsReal()){
TempsResults[var] = val;
return true;
}
//here is the special case, cast to the type of the LHS(t in our case)
if(val.IsInt()){
val.SetReal((float)val.GetInt());
val.SetType(VREAL);
TempsResults[var] = val;
return true;
}
break;
case INTEGER:
//they match, nothing to do here
if(val.IsInt()){
TempsResults[var] = val;
return true;
}
//another special case here, cast to the LHS of INT in this case
if(val.IsReal()) {
val.SetInt((int)val.GetReal());
val.SetType(VINT);
TempsResults[var] = val;
return true;
}
break;
//We should never get here in theory, added to remove compile warnings
default:
ParseError(line, "Illegal Assignment Operation");
return false;
}
//if we get here, we know there must have been some mismatched types
ParseError(line, "Mismatched types in assignment operation");
return false;
}
// Check to see if the variable is valid and has previously been declared
// Var ::= IDENT
bool Var(istream& in, int& line, LexItem& idtok){
//get the token, check to see if var was declared
LexItem l = Parser::GetNextToken(in, line);
//If we can find the variable, return true
if(defVar.find(l.GetLexeme())-> second){
//Use idtok to conveniently store the type of the variable using symTable
idtok = LexItem(SymTable[l.GetLexeme()], l.GetLexeme(), line);
return true;
//If lexeme is unrecognized, then give this error
} else if (l == ERR) {
ParseError(line, "Unrecognized Input Pattern");
cout << "(" << l.GetToken() << ")" << endl;
return false;
//If we get here, we have a valid variable name that was just not declared. Show appropriate error
} else {
ParseError(line, "Undeclared Variable");
return false;
}
//We should never get here, added to remove compile warnings
return false;
}
//Simply calls expr recursively, so long as there are more commas. This will be used in our writeln
//ExprList:= Expr {,Expr}
bool ExprList(istream& in, int& line) {
bool status = false;
Value retVal;
//Call expr, passing in retVal so we can have a result
status = Expr(in, line, retVal);
//If expr is bad, no point in continuing
if(!status){
ParseError(line, "Missing Expression");
return false;
}
//Push the value to the queue of values to be printed
ValQue->push(retVal);
//get and analyze next token, if it is a comma we should recursively call this function again
LexItem tok = Parser::GetNextToken(in, line);
if (tok == COMMA) {
status = ExprList(in, line);
} else if(tok.GetToken() == ERR){
ParseError(line, "Unrecognized Input Pattern");
cout << "(" << tok.GetLexeme() << ")" << endl;
return false;
} else {
Parser::PushBackToken(tok);
return true;
}
return status;
}
//Expr ::= LogOrExpr ::= LogAndExpr { OR LogAndExpr }
//So essentially: Expr ::= LogANDExpr { OR LogAndExpr}
bool Expr(istream& in, int& line, Value& retVal){
bool status = false;
LexItem l;
//Once we get here, first thing to do is call LogAndExpr
status = LogANDExpr(in, line, retVal);
//If this is all that we have, it doesn't matter the type of retVal
//If expression is bad, return false
if (!status){
return false;
}
//Once we're here, we can either have nothing or one or more OR's followed by more LogAndExpr
//Get the next token
l = Parser::GetNextToken(in, line);
//While we have an OR, keep processing LogAndExpr's
while (l == OR){
Value val;
status = LogANDExpr(in, line, val);
//If expression is bad, return error
if (!status){
return false;
}
//perform the "OR"ing for retval and val and reassign it to retVal
retVal = retVal || val;
//If this doesn't work, we had a bad or operation(i.e. we either val or retval isn't a boolean)
if(retVal.IsErr()){
ParseError(line, "Illegal use of non-boolean operand with OR");
return false;
}
//refresh the value of l
l = Parser::GetNextToken(in, line);
}
// if we have an ERR token, throw error
if (l == ERR) {
ParseError(line, "Unrecognized input pattern.");
cout << "(" << l.GetLexeme() << ")";
return false;
}
//Once we get here, l was not an OR, so put it back and we're done
Parser::PushBackToken(l);
//If we end up here, everything worked
return true;
}
// LogAndExpr ::= RelExpr {AND RelExpr }
bool LogANDExpr(istream& in, int& line, Value& retVal){
bool status = false;
LexItem l;