This repository has been archived by the owner on Feb 20, 2024. It is now read-only.
/
scope.sk
204 lines (173 loc) · 4.7 KB
/
scope.sk
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
namespace Skew {
enum ScopeKind {
FUNCTION
LOCAL
OBJECT
VARIABLE
}
enum ScopeSearch {
NORMAL
ALSO_CHECK_FOR_SETTER
}
class Scope {
var parent Scope
var used StringMap<VariableSymbol> = null
var _enclosingFunctionOrLambda FunctionScope = null
var _enclosingFunction FunctionScope = null
var _enclosingLoop LocalScope = null
def kind ScopeKind
def _find(name string) Symbol
# Need to check for a setter at the same time as for a normal symbol
# because the one in the closer scope must be picked. If both are in
# the same scope, pick the setter.
def find(name string, search ScopeSearch) Symbol {
var symbol Symbol = null
for scope = self; scope != null && symbol == null; scope = scope.parent {
if search == .ALSO_CHECK_FOR_SETTER { symbol = scope._find(name + "=") }
if symbol == null { symbol = scope._find(name) }
}
return symbol
}
def asObjectScope ObjectScope {
assert(kind == .OBJECT)
return self as ObjectScope
}
def asFunctionScope FunctionScope {
assert(kind == .FUNCTION)
return self as FunctionScope
}
def asVariableScope VariableScope {
assert(kind == .VARIABLE)
return self as VariableScope
}
def asLocalScope LocalScope {
assert(kind == .LOCAL)
return self as LocalScope
}
def findEnclosingFunctionOrLambda FunctionScope {
if _enclosingFunctionOrLambda != null {
return _enclosingFunctionOrLambda
}
var scope = self
while scope != null {
if scope.kind == .FUNCTION {
_enclosingFunctionOrLambda = scope.asFunctionScope
return _enclosingFunctionOrLambda
}
scope = scope.parent
}
return null
}
def findEnclosingFunction FunctionScope {
if _enclosingFunction != null {
return _enclosingFunction
}
var scope Scope = findEnclosingFunctionOrLambda
while scope != null {
if scope.kind == .FUNCTION && scope.asFunctionScope.symbol.kind != .FUNCTION_LOCAL {
_enclosingFunction = scope.asFunctionScope
return _enclosingFunction
}
scope = scope.parent
}
return null
}
def findEnclosingLoop LocalScope {
if _enclosingLoop != null {
return _enclosingLoop
}
var scope = self
while scope != null && scope.kind == .LOCAL {
if scope.asLocalScope.type == .LOOP {
_enclosingLoop = scope.asLocalScope
return _enclosingLoop
}
scope = scope.parent
}
return null
}
def generateName(prefix string) string {
var count = 0
var name = prefix
while isNameUsed(name) {
name = prefix + (++count).toString
}
reserveName(name, null)
return name
}
def reserveName(name string, symbol VariableSymbol) {
if used == null {
used = {}
}
if !(name in used) {
used[name] = symbol
}
}
def isNameUsed(name string) bool {
return find(name, .NORMAL) != null || used != null && name in used
}
}
class ObjectScope : Scope {
var symbol ObjectSymbol
over kind ScopeKind {
return .OBJECT
}
over _find(name string) Symbol {
return symbol.members.get(name, null)
}
}
class FunctionScope : Scope {
var symbol FunctionSymbol
var parameters StringMap<ParameterSymbol> = {}
over kind ScopeKind {
return .FUNCTION
}
over _find(name string) Symbol {
return parameters.get(name, null)
}
}
class VariableScope : Scope {
var symbol VariableSymbol
over kind ScopeKind {
return .VARIABLE
}
over _find(name string) Symbol {
return null
}
}
enum LocalType {
LOOP
NORMAL
}
class LocalScope : Scope {
var locals StringMap<VariableSymbol> = {}
var type LocalType
over kind ScopeKind {
return .LOCAL
}
over _find(name string) Symbol {
return locals.get(name, null)
}
def define(symbol VariableSymbol, log Log) {
symbol.scope = self
# Check for duplicates
var other = locals.get(symbol.name, null)
if other != null {
log.semanticErrorDuplicateSymbol(symbol.range, symbol.name, other.range)
return
}
# Check for shadowing
var scope = parent
while scope.kind == .LOCAL {
var local = scope.asLocalScope.locals.get(symbol.name, null)
if local != null {
log.semanticErrorShadowedSymbol(symbol.range, symbol.name, local.range)
return
}
scope = scope.parent
}
scope.reserveName(symbol.name, symbol)
locals[symbol.name] = symbol
}
}
}