-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwings.cpp
1126 lines (1011 loc) · 26.2 KB
/
wings.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
////////////////////////////////////////////////////////////////////////////////////////////
// WINGS : Interpreter for BIRD programming language
////////////////////////////////////////////////////////////////////////////////////////////
#include <iostream>
#include <string>
#include <stdexcept>
#include <vector>
#include <set>
#include <map>
#include <fstream>
//Token types
#define INTEGER "INTEGER"
#define STRING "STRING"
#define PLUS "PLUS"
#define MINUS "MINUS"
#define MUL "MUL"
#define DIV "DIV"
#define MOD "MOD"
#define LPAREN "LPAREN"
#define RPAREN "RPAREN"
#define SEMI "SEMI"
#define ASSIGN "ASSIGN"
#define VARIABLE "VARIABLE"
#define STATEMENT_LIST "STATEMENT_LIST"
#define WRITE "write"
#define READ "read"
#define EQUALITY "EQUALITY"
#define IF "if"
#define LBRACKET "LBRACKET"
#define RBRACKET "RBRACKET"
#define LESSTHAN "LESSTHAN"
#define MORETHAN "MORETHAN"
#define EMPTY "EMPTY"
#define ELSE "else"
#define WHILE "while"
#define ENDL "endl"
//Containers to store reserved keywords, variables : value and types
std::set< std::string > RESERVED_KEYWORDS;
std::map<std::string, std::string> GLOBAL_SCOPE;
std::map<std::string, std::string> VARIABLE_TYPES;
////////////////////////////////////////////////////////////////////////////////////////////
// LEXER
////////////////////////////////////////////////////////////////////////////////////////////
class Token
{
private:
//token type: eg: int, plus, EOF, etc
std::string type;
//token value: eg: 0, 1, '+', 'EOF'
std::string value;
public:
Token()
{
type = "";
value = "";
}
Token(std::string _type, std::string _value)
{
type = _type;
value = _value;
}
~Token(){};
//String representation of class instance
// eg: Token(int,3) Token(plus,'+')
std::string str()
{
return ("Token("+type+","+value+")");
}
//returns value of this token
std::string _value()
{
return value;
}
//returns type of this token
std::string _type()
{
return type;
}
};
class Lexer
{
private:
//Code in bird
std::string text;
//current char at text[pos]
char current_char;
//current index of text to be tokenized
int pos;
public:
Lexer()
{
text = "";
pos = 0;
current_char = EOF;
}
Lexer(std::string _text)
{
text = _text;
pos = 0;
current_char = text[pos];
}
~Lexer(){};
void error()
{
std::cout<<"Lexer Error:\n";
std::cout<<"pos = "<<pos<<" text[pos] = "<<text[pos]<<"\n";
std::exit(0);
}
//Ignores whitespaces and put pos pointer to a non-whitespace character
void skip_whitespaces();
//Ignores comments
void skip_comments();
//Advances position of pos pointer in text, and check if EOF reached
void advance_pos();
//returns a multi-digit integer in form of a string starting at pos, and shift pos to next pointer
std::string integer();
//returns a string which is present under double quotes in bird
std::string get_string();
//get arguments for write()
void write_arg();
//Most important function in Lexical analyzer.
//This method breaks the text into tokens, and returns a token starting at pos
Token get_next_token();
char peek();
Token _id();
};
void Lexer::skip_whitespaces()
{
//as the name suggests, skip all whitespaces till any new character is encountered
while(text[pos] == ' ' or text[pos] == '\t' or text[pos] == '\n')
{
advance_pos();
}
}
void Lexer::skip_comments()
{
advance_pos();
advance_pos();
while((text[pos] != '/' or peek() != '/') and text[pos] != EOF)
{
advance_pos();
}
advance_pos();
advance_pos();
}
void Lexer::advance_pos()
{
pos++;
//if pos past the end of text, i.e tokenization is completed
//return EOF token
if(pos >= text.length())
current_char = EOF;
else current_char = text[pos];
}
std::string Lexer::integer()
{
std::string str;
while(current_char>=48 and current_char<=57)
{
str.push_back(current_char);
advance_pos();
}
// std::cout<<"Lexer: integer(): str = "<<str<<" current_char = "<<current_char<<"\n";
if(current_char == '.')
{
str.push_back(current_char);
advance_pos();
while(current_char>=48 and current_char<=57)
{
str.push_back(current_char);
advance_pos();
}
// std::cout<<"Double value here str = "<<str<<"\n";
}
return str;
}
std::string Lexer::get_string()
{
// std::cout<<"get_string()\n";
std::string str;
advance_pos();
while(current_char != '\"')
{
// std::cout<<"current_char = "<<current_char<<"\n";
str.push_back(current_char);
advance_pos();
}
advance_pos();
return str;
}
//Only peeks into input buffer without actually consuming the next character
char Lexer::peek()
{
//if peek_pos past the end of text, return EOF
if(pos+1 >= text.length())
return EOF;
else return text[pos+1];
}
Token Lexer::_id()
{
std::string result;
Token token;
while((current_char >= 48 and current_char <= 57) or (current_char >= 65 and current_char <=90) or current_char >= 97 and current_char <=122)
{
result.push_back(current_char);
advance_pos();
}
auto it = RESERVED_KEYWORDS.find(result);
if(it == RESERVED_KEYWORDS.end())
token = Token(VARIABLE, result);
else token = Token(result, result);
return token;
}
Token Lexer::get_next_token()
{
//ignore whitespaces
skip_whitespaces();
//skip comments
while(current_char == '/' and peek() == '/')
{
skip_comments();
}
std::string temp_str;
//if current_char is an integer, create an integer token and increment pos
if(current_char >= 48 and current_char <=57)
{
// std::cout<<"yup int here\n";
temp_str = integer();//remember integer() returned multi-digit interger in form of a string
return Token(INTEGER, temp_str);
}
//if token is + operator
if(current_char == '+')
{
// std::cout<<"plus here\n";
temp_str.push_back(current_char);
advance_pos();
return Token(PLUS, temp_str);
}
//if token is - operator
if(current_char == '-')
{
// std::cout<<"minus here\n";
temp_str.push_back(current_char);
advance_pos();
return Token(MINUS, temp_str);
}
//if token is * operator
if(current_char == '*')
{
// std::cout<<"mul here\n";
temp_str.push_back(current_char);
advance_pos();
return Token(MUL, temp_str);
}
//if token is / operator
if(current_char == '/')
{
// std::cout<<"div here\n";
temp_str.push_back(current_char);
advance_pos();
return Token(DIV, temp_str);
}
//if token is % operator
if(current_char == '%')
{
// std::cout<<"mod here\n";
temp_str.push_back(current_char);
advance_pos();
return Token(MOD, temp_str);
}
//if token is ( left parenthesis
if(current_char == '(')
{
// std::cout<<"lparen here\n";
temp_str.push_back(current_char);
advance_pos();
return Token(LPAREN, temp_str);
}
//if token is ) right paranthesis
if(current_char == ')')
{
// std::cout<<"RPAREN here\n";
temp_str.push_back(current_char);
advance_pos();
return Token(RPAREN, temp_str);
}
//EOF reached
if(current_char == EOF)
{
temp_str.push_back(current_char);
// std::cout<<"File ends here\n";
return Token("EOF", temp_str);//some work needs to be done here ;p
}
//indentifier: some variable or a keyword
if((current_char >= 65 and current_char <=90) or (current_char >= 97 and current_char <=122))
return _id();
if(current_char == '=' and peek() == '=')
{
advance_pos();
advance_pos();
return Token(EQUALITY, "==");
}
if(current_char == '<')
{
advance_pos();
return Token(LESSTHAN, "<");
}
if(current_char == '>')
{
advance_pos();
return Token(MORETHAN, ">");
}
//Assignment operator
if(current_char == '=' and peek() != '=')
{
Token token(ASSIGN, "=");
advance_pos();
return token;
}
if(current_char == ';')
{
advance_pos();
return Token(SEMI, ";");
}
if(current_char == '{')
{
advance_pos();
return Token(LBRACKET, "{");
}
if(current_char == '}')
{
advance_pos();
return Token(RBRACKET, "}");
}
if(current_char == '\"')
{
temp_str = get_string();
return Token(STRING, temp_str);
}
error();
}
////////////////////////////////////////////////////////////////////////////////////////////
// PARSER
////////////////////////////////////////////////////////////////////////////////////////////
class ASTNode
{
private:
Token token;
public:
std::vector< ASTNode* > child;
ASTNode(){};
ASTNode(Token _token)
{
token = _token;
}
~ASTNode(){};
void make_child(ASTNode _node)
{
ASTNode *temp = new ASTNode(_node._token());
temp->child = _node.child;
child.push_back(temp);
}
Token _token()
{
return token;
}
//Method to print AST, AST means level of node, root is at level zero
void show(int level)
{
std::cout<<"Token: level = "<<level<<" type = "<<(token._type())<<" value = "<<(token._value())<<std::endl;
for(auto it = child.begin(); it != child.end(); it++)
(*it)->show(level+1);
}
};
class Parser
{
private:
Lexer lexer;
Token current_token;
public:
Parser(){};
Parser(Lexer _lexer)
{
lexer = _lexer;
current_token = lexer.get_next_token();
}
~Parser(){};
// compare the current token type with the passed token
// type and if they match then "eat" the current token
// and assign the next token to the self.current_token,
// otherwise raise an exception.
void eat(std::string token_type);
void error()
{
std::cout<<"Parser Error:\n";
std::cout<<"current_token = "<<(current_token.str())<<"\n";
std::exit(0);
}
//Arithmatic expression parser/interpreter
//factor : (PLUS|MINUS)factor | INTEGER | LPAREN expr RPAREN
ASTNode factor();
//term : PLUS factor | MINUS factor | INTEGER | LPAREN exor RPAREN | variable
ASTNode term();
//expr : term((PLUS | MINUS) term)*
ASTNode expr();
//assignment_statement : variable ASSIGN expr
ASTNode assignment_statement();
//statement : assignment_statement | empty
ASTNode statement();
//variable : ID
ASTNode variable();
//statement_list : statement | statement ENDL statement_list
ASTNode statement_list();
//empty statement
// ASTNode empty();
//write() arguments list
// write : write LPAREN variable RPAREN
ASTNode write();
//read() takes only one argument, takes input from console and store it in that variable
//read : read LPAREN expr() RPAREN
ASTNode read();
//conditional operator : left OPERATOR right
ASTNode conditional();
//if : IF LPAREN Conditional RPAREN ( ( LBACKET statement_list RBRACKET ) | statement ) (EMPTY | ELSE ( ( LBACKET statement_list RBRACKET ) | statement ) )
ASTNode If();
//while : WHILE LPAREN conditional RPAREN ( ( LBACKET statement_list RBRACKET ) | statement )
ASTNode While();
ASTNode parse()
{
ASTNode node = statement_list();
return node;
}
};
void Parser::eat(std::string token_type)
{
// std::cout<<"Eat called\n";
if(current_token._type() == token_type)
current_token = lexer.get_next_token();
else
{
std::cout<<"Error when eat() called\n";
std::cout<<"current_token.type = "<<current_token._type()<<" token_type = "<<token_type<<"\n";
error();
}
}
ASTNode Parser::statement()
{
ASTNode node;
if(current_token._type() == VARIABLE)
{
node = assignment_statement();
eat(SEMI);
}
else if(current_token._type() == WRITE)
{
node = write();
eat(SEMI);
}
else if(current_token._type() == READ)
{
node = read();
eat(SEMI);
}
else if(current_token._type() == IF)
node = If();
else if(current_token._type() == WHILE)
node = While();
else error(); // some unknown kind of statement
return node;
}
ASTNode Parser::assignment_statement()
{
ASTNode left = variable();
eat(ASSIGN);
ASTNode right = expr();
ASTNode node(Token(ASSIGN, "="));
node.make_child(left);
node.make_child(right);
return node;
}
ASTNode Parser::variable()
{
ASTNode node(current_token);
eat(VARIABLE);
return node;
}
ASTNode Parser::statement_list()
{
ASTNode node(Token("STATEMENT_LIST", "STATEMENT_LIST"));
while(current_token._type() != "EOF" and current_token._type() != RBRACKET)
{
node.make_child(statement());
}
return node;
}
ASTNode Parser::factor()
{
if(current_token._type() == INTEGER)
{
ASTNode node(current_token);
eat(INTEGER);
return node;
}
else if(current_token._type() == LPAREN)
{
eat(LPAREN);
ASTNode node = expr();
eat(RPAREN);
return node;
}
else if(current_token._type() == PLUS)
{
ASTNode node(current_token);
eat(PLUS);
node.make_child(factor());
return node;
}
else if(current_token._type() == MINUS)
{
ASTNode node(current_token);
eat(MINUS);
node.make_child(factor());
return node;
}
else if(current_token._type() == VARIABLE)
{
ASTNode node(variable());
return node;
}
else if(current_token._type() == STRING)
{
ASTNode node(current_token);
eat(STRING);
return node;
}
else if(current_token._type() == ENDL)
{
ASTNode node(current_token);
eat(ENDL);
return node;
}
else error();
}
ASTNode Parser::term()
{
ASTNode node = factor();
while(true)
{
ASTNode temp;
if(current_token._type() == MUL)
{
eat(MUL);
temp = ASTNode(Token(MUL, "*"));
}
else if(current_token._type() == DIV)
{
eat(DIV);
temp = ASTNode(Token(DIV, "/"));
}
else if(current_token._type() == MOD)
{
eat(MOD);
temp = ASTNode(Token(MOD, "%"));
}
else break;
//child[1] holds value to be multiplied/divided from child[0]
temp.make_child(node);
temp.make_child(factor());
node = temp;
}
return node;
}
ASTNode Parser::expr()
{
ASTNode node = term();
while(true)
{
ASTNode temp;
if(current_token._type() == PLUS)
{
eat(PLUS);
temp = ASTNode(Token(PLUS, "+"));
}
else if(current_token._type() == MINUS)
{
eat(MINUS);
temp = ASTNode(Token(MINUS, "-"));
}
else break;
//child[1] holds value to be added/subtracted from child[0]
temp.make_child(node);
temp.make_child(term());
node = temp;
}
return node;
}
ASTNode Parser::write()
{
eat(WRITE);
eat(LPAREN);
ASTNode node(Token(WRITE, WRITE));
node.make_child(expr()); //child[0] holds expr() to be printed
eat(RPAREN);
return node;
}
ASTNode Parser::read()
{
eat(READ);
eat(LPAREN);
ASTNode node(Token(READ, READ));
node.make_child(variable()); //child[0] holds variable whose value is to be taken as input
eat(RPAREN);
return node;
}
ASTNode Parser::If()
{
//child[0] holds conditional statement, and child[1] holds statement_list to be executed if conditional returns true
//child[2] stores ELSE part
eat(IF);
eat(LPAREN);
ASTNode node(Token(IF, IF));
node.make_child(conditional());
eat(RPAREN);
if(current_token._type() == LBRACKET)
{
eat(LBRACKET);
node.make_child(statement_list());
eat(RBRACKET);
}
else node.make_child(statement());
//if ELSE part is present, then make it child, otherwise make empty statement as ELSE part
if(current_token._type() == ELSE)
{
eat(ELSE);
if(current_token._type() == LBRACKET)
{
eat(LBRACKET);
node.make_child(statement_list());
eat(RBRACKET);
}
else node.make_child(statement());
}
else node.make_child(Token(EMPTY, EMPTY));
return node;
}
ASTNode Parser::conditional()
{
//child[0] holds Left Hand Side of conditional
//child[1] holds Right Hand Side of conditional
ASTNode node;
ASTNode left = expr();
if(current_token._type() == EQUALITY)
{
node = Token(EQUALITY, EQUALITY);
eat(EQUALITY);
}
else if(current_token._type() == LESSTHAN)
{
node = Token(LESSTHAN, LESSTHAN);
eat(LESSTHAN);
}
else if(current_token._type() == MORETHAN)
{
node = Token(MORETHAN, MORETHAN);
eat(MORETHAN);
}
else error();
node.make_child(left);
node.make_child(expr());
return node;
}
ASTNode Parser::While()
{
//child[0] holds conditional statement, and child[1] holds statement_list to be executed while conditional returns true
eat(WHILE);
eat(LPAREN);
ASTNode node(Token(WHILE, WHILE));
node.make_child(conditional());
eat(RPAREN);
if(current_token._type() == LBRACKET)
{
eat(LBRACKET);
node.make_child(statement_list());
eat(RBRACKET);
}
else node.make_child(statement());
return node;
}
////////////////////////////////////////////////////////////////////////////////////////////
// INTERPRETER
////////////////////////////////////////////////////////////////////////////////////////////
class Interpreter
{
private:
Parser parser;
public:
Interpreter(Parser _parser)
{
parser = _parser;
}
~Interpreter(){};
Token visit(ASTNode node)
{
std::string result;
if(node._token()._type() == STATEMENT_LIST)
{
for(auto it = node.child.begin(); it != node.child.end(); it++)
{
Token token = visit(**it);
if(token._value() != "0")
error("Failed to interpret statement: type = "+token._type()+" value = "+token._value());
}
return (Token(INTEGER, "0"));
}
if(node._token()._type() == ASSIGN)
{
Token token = visit(*node.child[1]);
GLOBAL_SCOPE[node.child[0]->_token()._value()] = token._value();
VARIABLE_TYPES[node.child[0]->_token()._value()] = token._type();
return (Token(INTEGER, "0"));
}
if(node._token()._type() == VARIABLE)
{
auto it = GLOBAL_SCOPE.find(node._token()._value());
if(it == GLOBAL_SCOPE.end())
error("Garbage value in "+node._token()._value());
auto i = VARIABLE_TYPES.find(node._token()._value());
if(i == GLOBAL_SCOPE.end())
error("Error while determining datatype of "+node._token()._value());
if(i->second == INTEGER)
return Token(INTEGER, it->second);
else if(i->second == STRING)
return Token(STRING, it->second);
}
if(node._token()._type() == INTEGER)
{
return node._token();
}
if(node._token()._type() == STRING)
{
return node._token();
}
//Unary plus or binary plus
if(node._token()._type() == PLUS)
{
//Unary Plus: Applicable for both integer addition and string concatanation
if(node.child.size() == 1)
{
return visit(*(node.child[0]));
}
Token temp1, temp2;
temp1 = visit(*(node.child[0]));
temp2 = visit(*(node.child[1]));
//if any of child is string, then do simple string concatanation
if(temp1._type() == STRING or temp2._type() == STRING)
{
result = temp1._value() + temp2._value();
return Token(STRING, result);
}
//INTEGER ADDITION
else
{
std::string str1 = temp1._value();
std::string str2 = temp2._value();
//If both operands are integer only then return without decimals
if(str1.find('.') == std::string::npos and str2.find('.') == std::string::npos)
result = std::to_string(std::stoi(temp1._value()) + std::stoi(temp2._value()));
//if any of th operands is double type, then return with decimals
else result = std::to_string(std::stod(temp1._value()) + std::stod(temp2._value()));
return Token(INTEGER, result);
}
}
//Unary minus or binary minus
if(node._token()._type() == MINUS)
{
//Unary Minus
if(node.child.size() == 1)
{
Token temp = visit(*(node.child[0]));
std::string str = temp._value();
if(str.find('.') == std::string::npos)
result = std::to_string( - std::stoi(temp._value()));
else result = std::to_string( - std::stod(temp._value()));
return Token(INTEGER, result);
}
//Binary Minus
else
{
Token temp1, temp2;
temp1 = visit(*(node.child[0]));
temp2 = visit(*(node.child[1]));
std::string str1 = temp1._value();
std::string str2 = temp2._value();
//If both operands are integer only then return without decimals
if(str1.find('.') == std::string::npos and str2.find('.') == std::string::npos)
result = std::to_string(std::stoi(temp1._value()) - std::stoi(temp2._value()));
//if any of th operands is double type, then return wiht decimals
else result = std::to_string(std::stod(temp1._value()) - std::stod(temp2._value()));
return Token(INTEGER, result);
}
}
if(node._token()._type() == MUL)
{
Token temp1, temp2;
temp1 = visit(*(node.child[0]));
temp2 = visit(*(node.child[1]));
std::string str1 = temp1._value();
std::string str2 = temp2._value();
//If both operands are integer only then do integer multiplication and return without decimals
if(str1.find('.') == std::string::npos and str2.find('.') == std::string::npos)
result = std::to_string(std::stoi(temp1._value()) * std::stoi(temp2._value()));
//if any of th operands is double type, then return with decimal digits
else result = std::to_string(std::stod(temp1._value()) * std::stod(temp2._value()));
return Token(INTEGER, result);
}
if(node._token()._type() == DIV)
{
Token temp1, temp2;
temp1 = visit(*(node.child[0]));
temp2 = visit(*(node.child[1]));
std::string str1 = temp1._value();
std::string str2 = temp2._value();
//If both operands are integer only then do integer division and return quotient of division
if(str1.find('.') == std::string::npos and str2.find('.') == std::string::npos)
result = std::to_string(std::stoi(temp1._value()) / std::stoi(temp2._value()));
//if any of th operands is double type, do double division
else result = std::to_string(std::stod(temp1._value()) / std::stod(temp2._value()));
return Token(INTEGER, result);
}
if(node._token()._type() == MOD)
{
Token temp1, temp2;
temp1 = visit(*(node.child[0]));
temp2 = visit(*(node.child[1]));
result = std::to_string(std::stoi(temp1._value()) % std::stoi(temp2._value()));
return Token(INTEGER, result);
}
if(node._token()._type() == WRITE)
{
Token token = visit(*node.child[0]);
std::cout<<token._value();
return Token(INTEGER, "0");
}
if(node._token()._type() == READ)
{
std::string str, var;
int dec_count = 0; //to count number of dots '.' in input
var = node.child[0]->_token()._value(); //var stores name of variable
std::getline(std::cin, str); //str stores value of var
GLOBAL_SCOPE[var] = str;
//flag = true means input is string type, flag = false means input is integer type
bool flag = false;
for(auto it = str.begin(); it != str.end(); it++)
{
if((*it < 48 or *it > 57) and *it != '.' and *it != '-')
flag = true;
if(*it == '.')
dec_count++;
if(it != str.begin() and *it == '-') // -4 will be considered integer and 4-5 will be considered string
flag = true;
}
//One decimal is allowed in integer, two decimal points means STRING
if(dec_count > 1)
flag = true;
auto it = VARIABLE_TYPES.find(var);
//if input is a string, no matter what was earlier datatype, new datatype of var will be STRING
if(flag)
VARIABLE_TYPES[var] = STRING;
//input is a number and initially var was not used or has INTEGER datatype
else if(it == VARIABLE_TYPES.end() or it->second == INTEGER)
VARIABLE_TYPES[var] = INTEGER;
else VARIABLE_TYPES[var] = STRING;
return Token(INTEGER, "0");
}
if(node._token()._type() == IF)
{
Token token = visit(*node.child[0]);
Token temp;
if(token._value() == "1") //if conditional() returns 1(means TRUE) then execute statement_list, else do nothing
temp = visit(*node.child[1]);
else temp = visit(*node.child[2]); //else part
return temp;
}
if(node._token()._type() == EQUALITY)
{
Token token0 = visit(*node.child[0]);
Token token1 = visit(*node.child[1]);
if(token0._value() == token1._value() and token0._type() == token1._type())
return Token(INTEGER, "1");
else return Token(INTEGER, "0");
}
if(node._token()._type() == LESSTHAN)
{
Token token0 = visit(*node.child[0]);
Token token1 = visit(*node.child[1]);
if(token0._type() != token1._type())
{
error("Comparing different datatypes");
}