-
Notifications
You must be signed in to change notification settings - Fork 0
/
representation.py
160 lines (113 loc) · 3.59 KB
/
representation.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
import runtime
from excep import ReturnControl
class Program:
def __init__(self, statements):
self.statements = statements
def execute(self, env):
ret = None
for s in self.statements:
ret = s.execute(env)
return ret
def __str__(self):
return '\n'.join([str(s) for s in self.statements])
class Statement:
def __repr__(self):
return str(self)
class DefineStmt(Statement):
def __init__(self, var_id_literal, var_value_expr):
self.var_id_literal = var_id_literal
self.var_value_expr = var_value_expr
def execute(self, env):
env.set(self.var_id_literal, self.var_value_expr.evaluate(env))
def __str__(self):
return 'var %s = %s' % (self.var_id_literal, str(self.var_value_expr))
class WhileStmt(Statement):
def __init__(self, condition, statements):
self.condition = condition
self.statements = statements
def execute(self, env):
while self.condition.evaluate(env):
for s in self.statements:
s.execute(env)
class AssignStmt(Statement):
def __init__(self, var_id_literal, var_value_expr):
self.var_id_literal = var_id_literal
self.var_value_expr = var_value_expr
def execute(self, env):
env.set(self.var_id_literal, self.var_value_expr.evaluate(env))
class ReturnStmt(Statement):
def __init__(self, expr=None):
self.expr = expr
def execute(self, env):
raise ReturnControl(self.expr)
def __str__(self):
return 'return %s' % str(self.expr)
class IfStmt(Statement):
def __init__(self, cond_expr, statements, else_statements):
self.cond_expr = cond_expr
self.statements = statements
self.else_statements = else_statements
def execute(self, env):
cond = self.cond_expr.evaluate(env)
to_exec = self.statements if cond else self.else_statements
if to_exec is None:
return
for s in to_exec:
s.execute(env)
def __str__(self):
string = 'if (%s) {%s}' % (
str(self.cond_expr),
'; '.join([str(s) for s in self.statements]))
if self.else_statements is not None:
string += ' else {%s}' % '; '.join([str(s) for s in self.else_statements])
return string
class Expression:
def execute(self, env):
return self.evaluate(env)
def __repr__(self):
return str(self)
class FunctionCall(Expression):
def __init__(self, expr, arguments):
self.expr = expr
self.arguments = arguments
def evaluate(self, env):
function = self.expr.evaluate(env)
return runtime.apply(function, self.arguments, env)
def __str__(self):
return '(%s)(%s)' % (
str(self.expr),
', '.join([str(s) for s in self.arguments]))
class FunctionDecl(Expression):
def __init__(self, params, body):
self.params = params
self.body = body
def evaluate(self, env):
return runtime.function(self.params, self.body, env)
def __str__(self):
return 'function(%s){%s}' % (
', '.join([str(p) for p in self.params]),
'; '.join([str(s) for s in self.body]))
class BinaryOp(Expression):
def __init__(self, op, left, right):
self.op = op
self.left = left
self.right = right
def evaluate(self, env):
return runtime.apply(env.lookup(self.op), [self.left, self.right], env)
def __str__(self):
return '%s %s %s' % (
str(self.left), self.op , str(self.right))
class Identifier(Expression):
def __init__(self, id):
self.id = id
def evaluate(self, env):
return env.lookup(self.id)
def __str__(self):
return self.id
class Number(Expression):
def __init__(self, num):
self.num = num
def evaluate(self, env):
return self.num
def __str__(self):
return str(self.num)