-
Notifications
You must be signed in to change notification settings - Fork 1
/
eval.py
165 lines (147 loc) · 6.48 KB
/
eval.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
from typing import Dict
class Interpreter:
def __init__(self):
self.environment = {}
def evaluate(self, node):
if isinstance(node, ProgramNode):
return self.evaluate_program_node(node)
elif isinstance(node, VariableDeclarationNode):
return self.evaluate_variable_declaration_node(node)
elif isinstance(node, MutableDeclarationNode):
return self.evaluate_mutable_declaration_node(node)
elif isinstance(node, AssignmentNode):
return self.evaluate_assignment_node(node)
elif isinstance(node, BinaryOperatorNode):
return self.evaluate_binary_operator_node(node)
elif isinstance(node, UnaryOperatorNode):
return self.evaluate_unary_operator_node(node)
elif isinstance(node, FunctionDeclarationNode):
return self.evaluate_function_declaration_node(node)
elif isinstance(node, FunctionCallNode):
return self.evaluate_function_call_node(node)
elif isinstance(node, IdentifierNode):
return self.evaluate_identifier_node(node)
elif isinstance(node, LiteralNode):
return self.evaluate_literal_node(node)
elif isinstance(node, IfStatementNode):
return self.evaluate_if_statement_node(node)
elif isinstance(node, ForLoopNode):
return self.evaluate_for_loop_node(node)
elif isinstance(node, WhileLoopNode):
return self.evaluate_while_loop_node(node)
elif isinstance(node, ReturnStatementNode):
return self.evaluate_return_statement_node(node)
elif isinstance(node, ExpressionStatementNode):
return self.evaluate_expression_statement_node(node)
elif isinstance(node, BlockNode):
return self.evaluate_block_node(node)
else:
raise Exception(f"Invalid node type: {type(node)}")
def evaluate_program_node(self, node):
result = None
for statement in node.statements:
result = self.evaluate(statement)
return result
def evaluate_variable_declaration_node(self, node):
value = self.evaluate(node.expression)
self.environment[node.identifier] = value
return value
def evaluate_mutable_declaration_node(self, node):
value = self.evaluate(node.expression)
self.environment[node.identifier] = value
return value
def evaluate_assignment_node(self, node):
value = self.evaluate(node.expression)
self.environment[node.identifier] = value
return value
def evaluate_binary_operator_node(self, node):
left = self.evaluate(node.left)
right = self.evaluate(node.right)
if node.operator.type == TokenType.PLUS:
return left + right
elif node.operator.type == TokenType.MINUS:
return left - right
elif node.operator.type == TokenType.MULTIPLY:
return left * right
elif node.operator.type == TokenType.DIVIDE:
if right == 0:
raise Exception("Division by zero")
return left / right
elif node.operator.type == TokenType.MODULO:
return left % right
elif node.operator.type == TokenType.EQUALS:
return left == right
elif node.operator.type == TokenType.NOT_EQUALS:
return left != right
elif node.operator.type == TokenType.LESS_THAN:
return left < right
elif node.operator.type == TokenType.LESS_THAN_OR_EQUALS:
return left <= right
elif node.operator.type == TokenType.GREATER_THAN:
return left > right
elif node.operator.type == TokenType.GREATER_THAN_OR_EQUALS:
return left >= right
elif node.operator.type == TokenType.AND:
return left and right
elif node.operator.type == TokenType.OR:
return left or right
else:
raise Exception(f"Invalid operator: {node.operator.type}")
def evaluate_unary_operator_node(self, node):
operand = self.evaluate(node.operand)
if node.operator.type == TokenType.MINUS:
return -operand
elif node.operator.type == TokenType.NOT:
return not operand
else:
raise Exception(f"Invalid operator: {node.operator.type}")
def evaluate_function_declaration_node(self, node):
self.environment[node.identifier] = node
return node
def evaluate_function_call_node(self, node):
function_node = self.evaluate(node.function)
if not isinstance(function_node, FunctionDeclarationNode):
raise Exception(f"Invalid function call: {node.function}")
if len(function_node.parameters) != len(node.arguments):
raise Exception(f"Invalid number of arguments for function call: {node.function}")
local_environment = {}
for parameter, argument in zip(function_node.parameters, node.arguments):
local_environment[parameter] = self.evaluate(argument)
previous_environment = self.environment
self.environment = local_environment
result = self.evaluate(function_node.body)
self.environment = previous_environment
return result
def evaluate_identifier_node(self, node):
if node.value not in self.environment:
raise Exception(f"Undefined variable: {node.value}")
return self.environment[node.value]
def evaluate_literal_node(self, node):
return node.value
def evaluate_if_statement_node(self, node):
condition = self.evaluate(node.condition)
if condition:
return self.evaluate(node.body)
else:
return self.evaluate(node.else_body)
def evaluate_for_loop_node(self, node):
iterable = self.evaluate(node.iterable)
result = None
for item in iterable:
self.environment[node.identifier] = item
result = self.evaluate(node.body)
return result
def evaluate_while_loop_node(self, node):
result = None
while self.evaluate(node.condition):
result = self.evaluate(node.body)
return result
def evaluate_return_statement_node(self, node):
return self.evaluate(node.expression)
def evaluate_expression_statement_node(self, node):
return self.evaluate(node.expression)
def evaluate_block_node(self, node):
result = None
for statement in node.statements:
result = self.evaluate(statement)
return result