-
Notifications
You must be signed in to change notification settings - Fork 0
/
execute.py
87 lines (63 loc) · 1.92 KB
/
execute.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
import solve
import operator
groups = {}
variables = {}
constraints = []
def flatten(L):
if len(L) < 2: return L
return [L[0]] + flatten(L[1])
def apply_func(func, param):
constraints.append((func, param))
def execute(ast):
if not isinstance(ast, tuple):
return ast
if ast[0] == 'Statements':
execute(ast[1])
return execute(ast[2])
if ast[0] == 'Statement':
return execute(ast[1])
if ast[0] == 'List':
return flatten(execute(ast[1]))
if ast[0] == 'ListItems':
return [execute(ast[1]), execute(ast[2])]
if ast[0] == 'ListItem':
return [execute(ast[1])]
if ast[0] == 'NullListItem':
return []
if ast[0] == 'Group':
groups[ast[1]] = execute(ast[2])
return groups[ast[1]]
if ast[0] == 'Point':
return (ast[1], ast[2])
if ast[0] == 'GroupListWithPoint':
return (execute(ast[1]), flatten(execute(ast[2])))
if ast[0] == 'GroupListWithoutPoint':
return flatten(execute(ast[1]))
if ast[0] == 'GroupItems':
return [execute(ast[1]), execute(ast[2])]
if ast[0] == 'GroupItem':
return (ast[1], ast[2])
if ast[0] == 'NullGroupItem':
return []
if ast[0] == 'NumToExpr':
return int(ast[1])
if ast[0] == 'Assign':
variables[ast[1]] = execute(ast[2])
return variables[ast[1]]
if ast[0] == 'IDToExpr':
return execute(ast[1])
if ast[0] == 'BinOp':
operator = ast[1]
operand1 = execute(ast[2])
operand2 = execute(ast[3])
return solve.BinOp(operator, operand1, operand2)
if ast[0] == 'FunctionCall':
return apply_func(ast[1], execute(ast[2]))
if ast[0] == 'Solve':
# print("Solving")
# print(groups)
# print(variables)
# print(constraints)
solve.solve(groups, variables, constraints)
return
print(f"Unexpected node {ast}")