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
|
# Copyright (c) 2015 Egor Tensin <Egor.Tensin@gmail.com>
# This file is part of the "Simple interpreter" project.
# For details, see https://github.com/egor-tensin/simple-interpreter.
# Distributed under the MIT License.
class ProgramNode:
def __init__(self, stmt_list):
self._stmt_list = stmt_list
def execute(self):
for stmt in self._stmt_list:
stmt.execute()
_varmap = { }
class CompoundStatementNode:
def __init__(self, stmt_list):
self._stmt_list = stmt_list
def execute(self):
for stmt in self._stmt_list:
stmt.execute()
class EmptyStatementNode:
def execute(self):
pass
class AssignmentNode:
def __init__(self, identifier, arithm_expr):
self._identifier = identifier
self._arithm_expr = arithm_expr
def execute(self):
_varmap[str(self._identifier)] = self._arithm_expr.execute()
return None
class PrintStatementNode:
def __init__(self, arithm_expr):
self._arithm_expr = arithm_expr
def execute(self):
print(self._arithm_expr.execute())
return None
class IdentifierNode:
def __init__(self, identifier):
self._identifier = identifier
def execute(self):
return _varmap[str(self._identifier)]
class AdditionOpNode:
def __init__(self, left, right):
self._left = left
self._right = right
def execute(self):
return self._left.execute() + self._right.execute()
class SubtractionOpNode:
def __init__(self, left, right):
self._left = left
self._right = right
def execute(self):
return self._left.execute() + self._right.execute()
class MultiplicationOpNode:
def __init__(self, left, right):
self._left = left
self._right = right
def execute(self):
return self._left.execute() * self._right.execute()
class DivisionOpNode:
def __init__(self, left, right):
self._left = left
self._right = right
def execute(self):
return self._left.execute() / self._right.execute()
class IntegerNumberNode:
def __init__(self, n):
self._n = n
def execute(self):
return int(self._n)
class FloatingPointNumberNode:
def __init__(self, n):
self._n = n
def execute(self):
return float(self._n)
class IfStatementNode:
def __init__(self, cond, body):
self._cond = cond
self._body = body
def execute(self):
if self._cond.execute():
return self._body.execute()
class TrueNode:
def execute(self):
return True
class FalseNode:
def execute(self):
return False
class AndOpNode:
def __init__(self, left, right):
self._left = left
self._right = right
def execute(self):
return self._left.execute() and self._right.execute()
class OrOpNode:
def __init__(self, left, right):
self._left = left
self._right = right
def execute(self):
return self._left.execute() or self._right.execute()
class EqualsOpNode:
def __init__(self, left, right):
self._left = left
self._right = right
def execute(self):
return self._left.execute() == self._right.execute()
class NotEqualsOpNode:
def __init__(self, left, right):
self._left = left
self._right = right
def execute(self):
return self._left.execute() != self._right.execute()
|