-
Notifications
You must be signed in to change notification settings - Fork 1
/
Interpreter.py
168 lines (142 loc) · 5.04 KB
/
Interpreter.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
import ast
import SymbolTable
from Memory import *
from Exceptions import *
from visitor import *
import sys
import numpy
sys.setrecursionlimit(10000)
class Interpreter(object):
def __init__(self):
self.stack = MemoryStack()
@on('node')
def visit(self, node):
pass
@when(ast.BinOp)
def visit(self, node):
opmap = dict({('+', lambda left, right: left + right),
('-', lambda left, right: left - right),
('*', lambda left, right: left * right),
('/', lambda left, right: left / right),
('>', lambda left, right: left > right),
('<', lambda left, right: left < right),
('DOTADD', lambda left, right: left + right),
('DOTSUB', lambda left, right: left - right),
('DOTMUL', lambda left, right: numpy.multiply(left, right)),
('DOTDIV', lambda left, right: numpy.divide(left, right)),
('EQ', lambda left, right: left == right),
('LTE', lambda left, right: left <= right),
('GTE', lambda left, right: left >= right),
('NE', lambda left, right: left != right)})
r1 = node.left.accept(self)
r2 = node.right.accept(self)
return opmap[node.op](r1, r2)
@when(ast.UnOp)
def visit(self, node):
opmap = dict({('-', lambda val: -val),
('!', lambda val: not val),
("'", lambda val: numpy.transpose(val))
})
r = node.right.accept(self)
return opmap[node.op](r)
@when(ast.AssignInstruction)
def visit(self, node):
opmap = dict({
('-=', lambda left, right: left - right),
('*=', lambda left, right: left * right),
('/=', lambda left, right: left / right),
('=', lambda left, right: right),
('+=', lambda left, right: left + right)
})
r1 = node.left.accept(self)
r2 = node.right.accept(self)
res = opmap[node.operand](r1, r2)
if not self.stack.set(node.left.id, res):
self.stack.insert(node.left.id, res)
pass
# simplistic while loop interpretation
@when(ast.WhileLoop)
def visit(self, node):
r = None
while node.condition.accept(self):
try:
r = node.instructions.accept(self)
except BreakException:
break
except ContinueException:
pass
return r
@when(ast.Range)
def visit(self, node):
return range(node.start.accept(self), node.end.accept(self))
@when(ast.ForLoop)
def visit(self, node):
self.stack.insert(node.variable.id, node.varFrom.accept(self))
rng = range(node.varFrom.accept(self), node.varTo.accept(self))
for i in rng:
self.stack.set(node.variable.id, i)
try:
r = node.instructions.accept(self)
except BreakException:
break
except ContinueException:
pass
return r
@when(ast.IfElse)
def visit(self, node):
if node.condition.accept(self):
r = node.instTrue.accept(self)
elif not node.condition.accept(self) and node.instFalse:
r = node.instFalse.accept(self)
return r
@when(ast.PrintInstruction)
def visit(self, node):
print node.value.accept(self)
@when(ast.PrintExpression)
def visit(self, node):
return str(node.expression.accept(self))
@when(ast.PrintExpressions)
def visit(self, node):
return str(node.expression.accept(self)) + str(node.expressions.accept(self))
@when(ast.BreakInstruction)
def visit(self, node):
raise BreakException
@when(ast.ContinueInstruction)
def visit(self, node):
raise ContinueException
@when(ast.ReturnInstruction)
def visit(self, node):
raise ReturnValueException(node.value.accept(self))
@when(ast.Instr)
def visit(self, node):
instruction = node.instruction.accept(self)
instructions = node.instructions.accept(self)
@when(ast.Number)
def visit(self, node):
return node.value
@when(ast.Variable)
def visit(self, node):
return self.stack.get(node.id)
@when(ast.Array)
def visit(self, node):
return node.range.accept(self)
@when(ast.Row)
def visit(self, node):
return node.nums
@when(ast.Matrix)
def visit(self, node):
rs = []
for row in node.rows:
rs.append(row.accept(self))
return numpy.array(rs)
@when(ast.SingleMatrixRef)
def visit(self, node):
idx = node.idx.accept(self)
matrix = self.stack.get(node.id)
return matrix[idx]
@when(ast.DoubleMatrixRef)
def visit(self, node):
idx1 = node.idx.accept(self)
idx2 = node.idx2.accept(self)
matrix = self.stack.get(node.id)
return matrix[idx1][idx2]