@@ -17,25 +17,77 @@ def __init__(self, value, children):
17
17
self .children = children # 0 = left child & 1 = right child
18
18
19
19
def Evaluate (self ):
20
+ res0 = self .children [0 ].Evaluate ()
21
+ res1 = self .children [1 ].Evaluate ()
22
+
20
23
if self .value == '+' :
21
- return self .children [0 ].Evaluate () + self .children [1 ].Evaluate ()
24
+ if (res0 [0 ] == res1 [0 ]):
25
+ return [res0 [0 ], res0 [1 ] + res1 [1 ]]
26
+ elif (res0 [0 ] == "STRING" or res1 [0 ] == "STRING" ):
27
+ raise NameError (f'Unable perform { res0 [0 ]} + { res1 [0 ]} ' )
28
+ return ['INT' , res0 [1 ] + res1 [1 ]]
29
+
22
30
elif self .value == '-' :
23
- return self .children [0 ].Evaluate () - self .children [1 ].Evaluate ()
31
+ if (res0 [0 ] == "STRING" or res1 [0 ] == "STRING" ):
32
+ raise NameError (f'Unable perform { res0 [0 ]} - { res1 [0 ]} ' )
33
+ return ['INT' , res0 [1 ] - res1 [1 ]]
34
+
24
35
elif self .value == '/' :
25
- return int (self .children [0 ].Evaluate () / self .children [1 ].Evaluate ())
36
+ if (res0 [0 ] == "STRING" or res1 [0 ] == "STRING" ):
37
+ raise NameError (f'Unable perform { res0 [0 ]} / { res1 [0 ]} ' )
38
+ return ['INT' , int (res0 [1 ] / res1 [1 ])]
39
+
26
40
elif self .value == '*' :
27
- return self .children [0 ].Evaluate () * self .children [1 ].Evaluate ()
41
+ if (res0 [0 ] == "STRING" or res1 [0 ] == "STRING" ):
42
+ if (res0 [0 ] == "BOOL" ):
43
+ if (res0 [1 ] == 1 ):
44
+ res0 [1 ] = "true"
45
+ else :
46
+ res0 [1 ] = "false"
47
+
48
+ if (res1 [0 ] == "BOOL" ):
49
+ if (res1 [1 ] == 1 ):
50
+ res1 [1 ] = "true"
51
+ else :
52
+ res1 [1 ] = "false"
53
+
54
+ return ["STRING" , str (res0 [1 ]) + str (res1 [1 ])]
55
+ else :
56
+ return ['INT' , res0 [1 ] * res1 [1 ]]
57
+
28
58
elif self .value == '||' :
29
- return self .children [0 ].Evaluate () or self .children [1 ].Evaluate ()
59
+ if (res0 [1 ] or res1 [1 ]):
60
+ return ['BOOL' , 1 ]
61
+ else :
62
+ return ['BOOL' , 0 ]
63
+
30
64
elif self .value == '&&' :
31
- return self .children [0 ].Evaluate () and self .children [1 ].Evaluate ()
65
+ if (res0 [0 ] == "STRING" or res1 [0 ] == "STRING" ):
66
+ raise NameError (f'Unable perform { res0 [0 ]} && { res1 [0 ]} ' )
67
+
68
+ if (res0 [1 ] and res1 [1 ]):
69
+ return ['BOOL' , 1 ]
70
+ else :
71
+ return ['BOOL' , 0 ]
72
+
32
73
elif self .value == '==' :
33
- return self .children [0 ].Evaluate () == self .children [1 ].Evaluate ()
74
+ if (res0 [1 ] == res1 [1 ]):
75
+ return ['BOOL' , 1 ]
76
+ else :
77
+ return ['BOOL' , 0 ]
78
+
34
79
elif self .value == '<' :
35
- return self .children [0 ].Evaluate () < self .children [1 ].Evaluate ()
80
+ if (res0 [1 ] < res1 [1 ]):
81
+ return ['BOOL' , 1 ]
82
+ else :
83
+ return ['BOOL' , 0 ]
84
+
36
85
elif self .value == '>' :
37
- return self .children [0 ].Evaluate () > self .children [1 ].Evaluate ()
38
-
86
+ if (res0 [1 ] > res1 [1 ]):
87
+ return ['BOOL' , 1 ]
88
+ else :
89
+ return ['BOOL' , 0 ]
90
+
39
91
40
92
class UnOp (Node ): # Single children
41
93
def __init__ (self , value ):
@@ -44,19 +96,45 @@ def __init__(self, value):
44
96
45
97
def Evaluate (self ):
46
98
if self .value == '+' :
47
- return self .children [0 ].Evaluate ()
99
+ return [ 'INT' , self .children [0 ].Evaluate ()[ 1 ]]
48
100
elif self .value == '-' :
49
- return - self .children [0 ].Evaluate ()
101
+ return [ 'INT' , - self .children [0 ].Evaluate ()[ 1 ]]
50
102
elif self .value == '!' :
51
- return not (self .children [0 ].Evaluate ())
103
+ if not (self .children [0 ].Evaluate ()[1 ]):
104
+ return ['BOOL' , 1 ]
105
+ else :
106
+ return ['BOOL' , 0 ]
107
+
52
108
53
109
class IntVal (Node ):
54
110
def __init__ (self , value ):
55
111
self .value = value
56
112
self .children = None
57
113
58
114
def Evaluate (self ):
59
- return self .value
115
+ return ["INT" , self .value ]
116
+
117
+
118
+ class BoolVal (Node ):
119
+ def __init__ (self , value ):
120
+ self .value = value
121
+ self .children = None
122
+
123
+ def Evaluate (self ):
124
+ if (self .value == "true" ):
125
+ return ["BOOL" , 1 ]
126
+ if (self .value == "false" ):
127
+ return ["BOOL" , 0 ]
128
+
129
+
130
+ class StringVal (Node ):
131
+ def __init__ (self , value ):
132
+ self .value = value
133
+ self .children = None
134
+
135
+ def Evaluate (self ):
136
+ return ["STRING" , self .value ]
137
+
60
138
61
139
class NoOp (Node ):
62
140
def __init__ (self ):
@@ -65,19 +143,31 @@ def __init__(self):
65
143
def Evaluate (self ):
66
144
pass
67
145
146
+
68
147
class Identifier (Node ):
69
148
def __init__ (self , value ):
70
149
self .value = value
71
150
72
151
def Evaluate (self ):
73
152
return table .getter (self .value )
74
153
154
+
75
155
class Assignment (Node ): # Two children
76
- def __init__ (self , children ):
77
- self .children = children # 0 = Identifier & 1 = Expression
156
+ def __init__ (self , children , value ):
157
+ self .children = children # 0 = Identifier & 1 = Expression/Type
158
+ self .value = value
78
159
79
160
def Evaluate (self ):
80
- table .setter (self .children [0 ], self .children [1 ].Evaluate ())
161
+ if (self .value == "::" ):
162
+ table .setter (self .children [0 ], self .children [1 ], None )
163
+
164
+ elif (self .value == "=" ):
165
+ res = self .children [1 ].Evaluate ()
166
+ if (res [0 ] == table .getter (self .children [0 ])[0 ]):
167
+ table .setter (self .children [0 ], res [0 ], res [1 ])
168
+ else :
169
+ raise NameError (f"Types for variable { self .children [0 ]} don't match" )
170
+
81
171
82
172
class Statement (Node ):
83
173
def __init__ (self ):
@@ -87,39 +177,50 @@ def Evaluate(self):
87
177
for i in range (len (self .children )):
88
178
self .children [i ].Evaluate ()
89
179
180
+
90
181
class Print (Node ): # Single children
91
182
def __init__ (self , children ):
92
183
self .children = [children ] # 0 = child
93
184
94
185
def Evaluate (self ):
95
- print (self .children [0 ].Evaluate ())
186
+ res = self .children [0 ].Evaluate ()
187
+ if (res [0 ] == "BOOL" ):
188
+ if (res [1 ] == 1 ):
189
+ print (True )
190
+ elif (res [1 ] == 0 ):
191
+ print (False )
192
+ else :
193
+ print (self .children [0 ].Evaluate ()[1 ])
194
+
96
195
97
196
class Read (Node ):
98
197
def __init__ (self ):
99
198
self .children = None
100
199
101
200
def Evaluate (self ):
102
201
self .value = int (input ())
103
- return self .value
202
+ return ['INT' , self .value ]
203
+
104
204
105
205
class While (Node ):
106
206
def __init__ (self , children ):
107
207
self .children = children # 0 = RelEx & 1 = Block
108
208
109
209
def Evaluate (self ):
110
- while (self .children [0 ].Evaluate ()):
210
+ while (self .children [0 ].Evaluate ()[ 1 ] ):
111
211
self .children [1 ].Evaluate ()
112
212
213
+
113
214
class If (Node ):
114
215
def __init__ (self , children ):
115
216
self .children = children
116
217
117
218
def Evaluate (self ):
118
219
if (self .children [2 ] is None ):
119
- if (self .children [0 ].Evaluate ()):
220
+ if (self .children [0 ].Evaluate ()[ 1 ] ):
120
221
self .children [1 ].Evaluate ()
121
222
else :
122
- if (self .children [0 ].Evaluate ()):
223
+ if (self .children [0 ].Evaluate ()[ 1 ] ):
123
224
self .children [1 ].Evaluate ()
124
225
else :
125
226
self .children [2 ].Evaluate ()
0 commit comments