-
Notifications
You must be signed in to change notification settings - Fork 1
/
Expression.py
291 lines (240 loc) · 9.03 KB
/
Expression.py
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
from NonTerminal import *
from Object import *
import Engine
from Control import statement_list
def primary_expression(ast, active_record):
if len(ast) == 2:
if ast[1] == "this":
return None, active_record.this
else:
return Engine.engine[ast[1][0]](ast[1], active_record)
else:
return None, expression_no_in(ast[2], active_record)
# pe = ast[1]
# if isinstance(pe, list):
# return Engine.engine[pe[0]](pe, active_record)
# else:
# return active_record.this
def identifier(ast, active_record):
def find_identifier(identifier, active_record):
if active_record is None:
return Engine.glb, identifier
if identifier in active_record:
return active_record, identifier
else:
return find_identifier(identifier, active_record.outFunction)
return find_identifier(ast[1], active_record)
def array_literal(ast, active_record):
obj = StObject()
element_list(ast[2], active_record, obj)
return None, obj
def element_list(ast, active_record, obj):
i = 0
ud = True
for child in ast:
if isinstance(child, list) and child[0] == AssignmentExpressionNoIn:
obj[i] = assignment_expression_no_in(child, active_record)
ud = False
elif child == ',':
if ud:
obj[i] = UNDEFINED
i += 1
ud = True
def object_literal(ast, active_record):
obj = StObject()
if len(ast) == 4:
property_name_and_value_list(ast[2], active_record, obj)
return None, obj
def property_name_and_value_list(ast, active_record, obj):
for child in ast:
if isinstance(child, list) and child[0] == PropertyNameAndValue:
property_name_and_value(child, active_record, obj)
def property_name_and_value(ast, active_record, obj):
obj[property_name(ast[1], active_record)] = assignment_expression_no_in(ast[3], active_record)
def property_name(ast, active_record):
if isinstance(ast[1], list):
return ast[1][1]
else:
return ast[1]
def member_expression(ast, active_record):
if len(ast) == 3:
owner_of_owner, key = member_expression(ast[1], active_record)
if isinstance(owner_of_owner, dict):
owner = owner_of_owner[key]
else:
owner = key
return member_expression_part(ast[2], active_record, owner)
else:
return Engine.engine[ast[1][0]](ast[1], active_record)
def allocation_expression(ast, active_record):
obj = StObject()
owner, key = member_expression(ast[2], active_record)
if owner is None:
constructor = key
else:
if key in owner:
constructor = owner[key]
else:
raise Exception("'" + str(key) + "' is not defined.")
if not isinstance(constructor, StFunction):
raise Exception(str(constructor) + " is not callable.")
new_ar = StActiveRecord()
new_ar.this = obj
new_ar.outFunction = constructor.outFunction
arguments(ast[3], active_record, new_ar, constructor.argument_list)
function_body(constructor.ast, new_ar)
return None, obj
def member_expression_part(ast, active_record, owner):
if isinstance(owner, dict):
if ast[1] == ".":
key = ast[2][1]
else:
key = expression_no_in(ast[2], active_record)
if not (isinstance(key, str) or isinstance(key, int) or isinstance(key, float)):
raise Exception("Only number or string can be a key")
return owner, key
else:
raise Exception(str(owner) + " has no member.")
def call_expression(ast, active_record):
if ast[2][0] == Arguments:
owner, key = Engine.engine[ast[1][0]](ast[1], active_record)
if owner is None:
function = key
elif key in owner:
function = owner[key]
else:
raise Exception("'" + str(key) + "' is not defined.")
if not isinstance(function, StFunction):
raise Exception(str(function) + " is not callable.")
new_ar = StActiveRecord()
if owner is None:
new_ar.this = active_record.this
elif isinstance(owner, StActiveRecord):
new_ar.this = active_record.this
elif isinstance(owner, StObject):
new_ar.this = owner
else:
raise Exception("function's owner is wrong.")
new_ar.outFunction = function.outFunction
arguments(ast[2], active_record, new_ar, function.argument_list)
return None, function_body(function.ast, new_ar)
else:
owner, ret = call_expression(ast[1], active_record)
return member_expression_part(ast[2], active_record, ret)
#
# def call_expression_part(ast, active_record):
# Engine.traverse(ast, active_record)
def arguments(ast, active_record, new_ar, argument_names):
if len(ast) == 3:
new_ar["arguments"] = StObject()
else:
argument_list(ast[2], active_record, new_ar, argument_names)
def argument_list(ast, active_record, new_ar, argument_names):
arguments_value = StObject()
i = 0
for child in ast:
if isinstance(child, list) and child[0] == AssignmentExpressionNoIn:
arg = assignment_expression_no_in(child, active_record)
arguments_value[i] = arg
if i < len(argument_names):
new_ar[argument_names[i]] = arg
i += 1
new_ar["arguments"] = arguments_value
def right_hand_side_expression(ast, active_record):
owner, key = Engine.engine[ast[1][0]](ast[1], active_record)
if isinstance(owner, dict):
if key in owner:
return owner[key]
else:
raise Exception("'" + str(key) + "' is not defined")
else:
return key
def left_hand_side_expression(ast, active_record):
owner, key = Engine.engine[ast[1][0]](ast[1], active_record)
if len(ast) == 3:
if isinstance(owner, dict):
owner = owner[key]
else:
owner = key
owner, key = member_expression_part(ast[2], active_record, owner)
if isinstance(owner, dict):
return owner, key
else:
raise Exception(str(key) + " can't be assignment")
def assignment_expression_no_in(ast, active_record):
if len(ast) == 2:
return Engine.engine[ast[1][0]](ast[1], active_record)
else:
owner, key = left_hand_side_expression(ast[1], active_record)
operator = assignment_operator(ast[2], active_record)
right_value = assignment_expression_no_in(ast[3], active_record)
if operator == "=":
owner[key] = right_value
elif operator == "+=":
owner[key] += right_value
elif operator == "-=":
owner[key] -= right_value
elif operator == "*=":
owner[key] *= right_value
elif operator == "/=":
owner[key] /= right_value
elif operator == "%=":
owner[key] %= right_value
elif operator == "|=":
owner[key] |= right_value
elif operator == "&=":
owner[key] &= right_value
elif operator == "^=":
owner[key] ^= right_value
else:
raise Exception("Unsupport operator " + operator)
return owner[key]
# if len(ast) > 2:
# left = Engine.engine[ast[1][0]](ast[1], active_record)
# right = Engine.engine[ast[3][0]](ast[3], active_record)
# if isinstance(right, StRef):
# left.obj = right.obj
# else:
# left.obj = right
# return left.obj
# else:
# return Engine.engine[ast[1][0]](ast[1], active_record)
def assignment_operator(ast, active_record):
return ast[1]
def expression_no_in(ast, active_record):
return assignment_expression_no_in(ast[1], active_record)
#
# def function_declaration(ast, active_record):
# Engine.traverse(ast, active_record)
def function_expression(ast, active_record):
func_proto = StFunction()
func_proto.outFunction = active_record
for child in ast:
if isinstance(child, list) and child[0] == Identifier:
func_proto.name = child[1]
active_record[child[1]] = func_proto
if isinstance(child, list) and child[0] == FormalParameterList:
func_proto.argument_list = formal_parameter_list(child, active_record)
if isinstance(child, list) and child[0] == FunctionBody:
func_proto.ast = child
return None, func_proto
def formal_parameter_list(ast, active_record):
ret = []
for x in ast:
if isinstance(x, list):
ret.append(x[1])
return ret
def function_body(ast, active_record):
if len(ast) == 3:
return UNDEFINED
else:
ret = statement_list(ast[2], active_record)
if ret is None:
ret = UNDEFINED
return ret
def variable_statement(ast, active_record):
var = ast[2][1]
if len(ast) <= 4:
active_record[var] = UNDEFINED
else:
active_record[var] = assignment_expression_no_in(ast[4], active_record)