-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresolver.py
More file actions
222 lines (167 loc) · 6.36 KB
/
Copy pathresolver.py
File metadata and controls
222 lines (167 loc) · 6.36 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# -*- coding: utf-8 -*-
from enum import Enum, unique
@unique
class FunctionType(Enum):
NONE = 0
FUNCTION = 1
METHOD = 2
LAMBDA = 3
@unique
class VariableState(Enum):
DECLARED = 0
DEFINED = 1
USED = 2
CORE = 3
class Resolver:
def __init__(self, interpreter, nebbdyr):
self.interpreter = interpreter
self.nebbdyr = nebbdyr
self.scopes = [{}]
for name in self.interpreter.globals.values:
self.scopes[0][name] = VariableState.CORE
self.current_function = FunctionType.NONE
def visit_block_stmt(self, stmt):
self.begin_scope()
self.resolve(stmt.statements)
self.end_scope()
def visit_class_stmt(self, stmt):
self.declare(stmt.name)
self.define(stmt.name)
for method in stmt.methods:
declaration = FunctionType.METHOD
self.resolve_function(method, declaration)
def visit_expression_stmt(self, stmt):
self.resolve(stmt.expression)
def visit_function_stmt(self, stmt):
self.declare(stmt.name)
self.define(stmt.name)
self.resolve_function(stmt, FunctionType.FUNCTION)
def visit_if_stmt(self, stmt):
self.resolve(stmt.condition)
self.resolve(stmt.then_branch)
if stmt.else_branch is not None:
self.resolve(stmt.else_branch)
def visit_print_stmt(self, stmt):
self.resolve(stmt.expression)
def visit_return_stmt(self, stmt):
if self.current_function not in (FunctionType.FUNCTION,
FunctionType.LAMBDA):
self.nebbdyr.error(stmt.keyword,
"Cannot return from top-level code.")
if stmt.value is not None:
self.resolve(stmt.value)
return None
def visit_while_stmt(self, stmt):
self.resolve(stmt.condition)
self.resolve(stmt.body)
def visit_continue_stmt(self, stmt):
return
def visit_break_stmt(self, stmt):
return
def visit_var_stmt(self, stmt):
self.declare(stmt.name)
if stmt.initializer is not None:
self.resolve(stmt.initializer)
self.define(stmt.name)
def visit_mut_stmt(self, stmt):
self.declare(stmt.name)
if stmt.initializer is not None:
self.resolve(stmt.initializer)
self.define(stmt.name)
def visit_unstable_stmt(self, stmt):
self.declare(stmt.name)
if stmt.initializer is not None:
self.resolve(stmt.initializer)
self.define(stmt.name)
def visit_assign_expr(self, expr):
self.resolve(expr.value)
self.resolve_local(expr, expr.name, False)
def visit_binary_expr(self, expr):
self.resolve(expr.left)
self.resolve(expr.right)
def visit_call_expr(self, expr):
self.resolve(expr.callee)
for argument in expr.arguments:
self.resolve(argument)
def visit_index_expr(self, expr):
self.resolve(expr.collection)
for index in expr.indicies:
self.resolve(index)
def visit_get_expr(self, expr):
self.resolve(expr.object)
def visit_grouping_expr(self, expr):
self.resolve(expr.expression)
def visit_literal_expr(self, expr):
return
def visit_logical_expr(self, expr):
self.resolve(expr.left)
self.resolve(expr.right)
def visit_set_expr(self, expr):
self.resolve(expr.value)
self.resolve(expr.object)
def visit_listconstructor_expr(self, expr):
self.resolve(expr.start)
if expr.next is not None:
self.resolve(expr.next)
self.resolve(expr.stop)
def visit_unary_expr(self, expr):
self.resolve(expr.right)
def visit_variable_expr(self, expr):
if (len(self.scopes) > 0 and
expr.name.lexeme in self.scopes[-1] and
self.scopes[-1][expr.name.lexeme] == VariableState.DECLARED):
self.nebbdyr.error(expr.name, "Cannot read local variable in its own initializer.")
self.resolve_local(expr, expr.name, True)
def visit_list_expr(self, expr):
self.resolve(expr.expression)
def visit_lambda_expr(self, expr):
self.resolve_function(expr, FunctionType.LAMBDA)
def resolve(self, statements):
if isinstance(statements, (list, tuple)):
for statement in statements:
self.resolve(statement)
else:
statements.accept(self)
def resolve_function(self, function, type):
enclosing_function = self.current_function
self.current_function = type
self.begin_scope()
for param in function.parameters:
self.declare(param)
self.define(param)
self.resolve(function.body)
self.end_scope()
self.current_function = enclosing_function
def begin_scope(self):
self.scopes.append(dict())
def end_scope(self):
scope = self.scopes.pop()
for name, state in scope.items():
if state == VariableState.DECLARED:
self.nebbdyr.error('', "Local variable '{}' is declared but not used.".format(name))
elif state == VariableState.DEFINED:
if self.current_function != FunctionType.LAMBDA:
self.nebbdyr.error('', "Local variable '{}' is defined but not used.".format(name))
def declare(self, name):
if not self.scopes:
return
scope = self.scopes[-1]
if name.lexeme in scope:
if scope[name.lexeme] == VariableState.CORE:
self.nebbdyr.error(name, "Cannot redefine core variable")
else:
self.nebbdyr.error(name,
"A variable with this name already declared in this scope.")
scope[name.lexeme] = VariableState.DECLARED
def define(self, name):
if not self.scopes:
return
self.scopes[-1][name.lexeme] = VariableState.DEFINED
def resolve_local(self, expr, name, is_read):
for i in range(len(self.scopes)-1, -1, -1):
if name.lexeme in self.scopes[i]:
self.interpreter.resolve(expr, len(self.scopes)-1-i)
if is_read and self.scopes[i][name.lexeme] != VariableState.CORE:
self.scopes[i][name.lexeme] = VariableState.USED
return
# Not found. Assume it is global