-
Notifications
You must be signed in to change notification settings - Fork 0
/
symbol.py
218 lines (187 loc) · 7.21 KB
/
symbol.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
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
from vm import *
class SymbolException(Exception):pass
class Scope(dict):pass
class SymbolTable(object):
def __init__(self, generator):
self.generator = generator
# we mark the first scope as Global Scope
self.globalScope = Scope()
self.globalScope.proto = generator.proto
self.current = self.globalScope
self.scopes = []
self.consts = generator.consts
def push(self, proto=None):
scope = Scope()
if proto:
scope.proto = proto
# a new proto, reset index to 0
self.index = 0
scope.index = self.index
else:
scope.proto = self.current.proto
self.index += 1
scope.index = self.index
self.scopes.append(self.current)
self.current = scope
self.proto = self.current.proto
def current_scope(self):
return self.current
def pop(self):
assert not (self.current is self.globalScope)
old = self.current
self.current = self.scopes.pop()
self.proto = self.current.proto
return old
def lookup(self, symbol):
if symbol in self.current:
return self.current[symbol], self.current
else:
for scope in reversed(self.scopes):
if symbol in scope:
return scope[symbol], scope
return None
def add(self, symbol, info=None):
if symbol in self.current:
exp = SymbolException("symbol '%s' is already defined in current scope.", symbol)
exp.scopes = self.scopes
exp.scope = self.current
raise exp
else:
if info:
self.current[symbol] = info
return info
if self.current is self.globalScope:
index = self.consts.addLiteral(symbol)
# add symbol in global scope:
self.current[symbol] = index
return "global", index
else:
info = self.proto.iLocalvars
self.current[symbol] = { "local":info }
self.proto.iLocalvars += 1
self.proto.adjustMaxLocalvars()
return "local", info
def _findSymbol(self, sym):
ret = self.lookup(sym)
if ret == None:
index = self.consts.addLiteral(sym)
return ("global", index)
info, scope = ret
if scope is self.globalScope:
index = self.consts.addLiteral(sym)
return ("global", index)
if scope.proto is self.proto:
if "varg" in info:
return ("varg", -1)
# 'sym' is a local symbol.
else:
return ("local", info["local"])
else:
# 'sym' is a upvar symbol already.
if "upvar" in info:
return ("upvar", info["upvar"])
# 'sym ' is a local and will become a upvar.
scopes = []
scopes.append(self.current)
for s in reversed(self.scopes):
scopes.append(s)
if s is scope:
break
# get protos for scopes.
protos = []
for s in scopes:
if len(protos) == 0 or (s.proto is not protos[-1]):
protos.append(s.proto)
# remove the proto where symbol is a local.
top_proto = protos.pop()
protos.reverse()
# add upvar in the proto
if "local" in info:
protos[0].upvars.append(("local", info["local"]))
elif "varg" in info:
protos[0].upvars.append(("varg", -1))
pre = protos[0]
for proto in protos[1:]:
proto.upvars.append(("up", len(pre.upvars) - 1))
pre = proto
# add new symbol in scopes list.
for s in scopes[:-1]:
# not for scope will symbol should be a local symbol
if s.proto is not top_proto:
assert sym not in s
s[sym] = { "upvar" : len(s.proto.upvars) - 1 }
return ("upvar", self.current[sym]["upvar"])
def genLoadSymbol(self, sym):
type, info = self._findSymbol(sym)
if type == "global":
return self.generator.gen((OpLoadGlobal, info))
elif type == "local":
return self.generator.gen((OpLoadLocal, info))
elif type == "varg":
return self.generator.gen((OpLoadVarg, info))
elif type == "upvar":
return self.generator.gen((OpLoadUpvar, info))
else:
assert 0
def genLoadSymbol(self, sym):
type, info = self._findSymbol(sym)
if type == "global":
return self.generator.gen((OpSetGlobal, info))
elif type == "local":
return self.generator.gen((OpSetLocal, info))
elif type == "varg":
return self.generator.gen((OpSetVarg, info))
elif type == "upvar":
return self.generator.gen((OpSetUpvar, info))
else:
assert 0
def genLoadSymbol(self, sym):
ret = self.lookup(sym)
if ret == None:
index = self.consts.addLiteral(sym)
return self.generator.gen((OpLoadGlobal, index))
info, scope = ret
if scope is self.globalScope:
index = self.consts.addLiteral(sym)
return self.generator.gen((OpLoadGlobal, index))
if scope.proto is self.proto:
if "varg" in info:
return self.generator.gen((OpLoadVarg, -1))
# 'sym' is a local symbol.
else:
return self.generator.gen((OpLoadLocal, info["local"]))
else:
# 'sym' is a upvar symbol already.
if "upvar" in info:
return self.generator.gen((OpLocalUpvar, info["upvar"]))
# 'sym ' is a local and will become a upvar.
scopes = []
scopes.append(self.current)
for s in reversed(self.scopes):
scopes.append(s)
if s is scope:
break
# get protos for scopes.
protos = []
for s in scopes:
if len(protos) == 0 or (s.proto is not protos[-1]):
protos.append(s.proto)
# remove the proto where symbol is a local.
top_proto = protos.pop()
protos.reverse()
# add upvar in the proto
if "local" in info:
protos[0].upvars.append(("local", info["local"], scope.index))
elif "varg" in info:
protos[0].upvars.append(("varg", None, None))
pre = protos[0]
for proto in protos[1:]:
proto.upvars.append(("up", len(pre.upvars) - 1, None))
pre = proto
# add new symbol in scopes list.
for s in scopes[:-1]:
# not for scope will symbol should be a local symbol
if s.proto is not top_proto:
assert sym not in s
s[sym] = { "upvar" : len(s.proto.upvars) - 1 }
return self.generator.gen((OpLoadUpvar, self.current[sym]["upvar"]))