forked from gracelang/minigrace
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbasic.grace
364 lines (351 loc) · 14.8 KB
/
basic.grace
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
dialect "standard"
// A bundle of basic definitions used in many parts of the minigrace compiler
//
// AstNode: the type of a parse node cum AST node
// Position — the type of a point in the source code
// Range — the type of a range of the source code
// Scope — the type of a symbol table of declared identifiers
// Variable — the type of symbol table entry describing a declared variable
// noPosition — a null source-code position
// emptyRange — an empty source code range
// nullNode:AstNode — a "null" for AST nodes
// class fakeSymbolTable — generates a new placeholder symbol table
// method canonicalName(n) — converts a neumeric name to canonical form
// method numericName(c) — converts a canonical name to numeric form
trait open {
// defines positions, and ranges, in the source
type Position = interface {
line → Number
column → Number
> (other) → Boolean
≥ (other) → Boolean
== (other) → Boolean
< (other) → Boolean
≤ (other) → Boolean
}
type Range = interface {
start → Position
end → Position
> (other) → Boolean
≥ (other) → Boolean
== (other) → Boolean
< (other) → Boolean
≤ (other) → Boolean
rangeString → String
rangeLongString → String
lineRangeString → String
}
type Scope = Object & interface {
outerScope → Scope
node → AstNode
uid → String
allNames → Sequence⟦String⟧
in(anotherScope) → SelfType
// sets the outerScope for this scope, and returns self
node(nu:AstNode) → SelfType
// sets the node for this scope, and returns self
lookup (name) ifAbsent (aBlock)
// Return the variable corresponding to name, which may or may not be
// defined in this scope, or in one of the lexically surrounding scopes.
// If it is not defined, return the value of executing aBlock.
lookup (name)
// Return the variable corresponding to name, which must be
// defined in this scope, or in one of the lexically surrounding scopes.
lookupLocally (name) ifAbsent (aBlock) ifPresent (pBlock)
// Look up variable corresponding to name, which may or may not be defined
// in this scope. If it is not defined, return the result of executing
// aBlock; otherwise, return the result of applying pBlock to the variable.
isFresh → Boolean
// answer true if this scope is one that can be used or inherited
attributeScopeOf (aName:String) in (nd:AstNode) → Scope
asString → String
lookupLocallyOrReused (name) ifAbsent (aBlock)
// Return the variable corresponding to name, which may or may not be defined in this scope,
// or in one of the scopes that it reuses.
// If name is not defined, return the value of executing aBlock.
lookupLocallyOrReused (name)
copy(other)
areReusedNamesCompleted → Boolean
currentObjectScope → SelfType
reusedNames → Dictionary
isModuleScope → Boolean
reuses (name) → Boolean
// Is name defined by a scope that this scope reuses?
lookup (name) ifAbsent (aBlock) ifPresent (pBlock)
// applies pBlock to the variable corresponding to name, if it is defined
// in this scope, or in one of the lexically surrounding scopes. If it
// is not defined, return the value of executing aBlock
== (other) → Boolean
lookupLexically (name)
redeclarationError (aMessage) variable (aVariable) → None
// raises a NamingError
add (aVariable) withName (aName) → Variable
structuralError (aMessage) variable (aVariable) → None
// raises a NamingError
allNamesAndValuesDo (aBlock) → Done
defines (name) → Boolean
// Is name defined in this scope, or in one of the lexically surrounding scopes?
redefine (aVariable) withName (aName) → Variable
hash → Number
isTrait → Boolean
isTheEmptyScope → Boolean
isDialectScope → Boolean
localAndReusedNamesAndValuesDo (aBlock) → Done
lookupReused (name) ifAbsent (aBlock)
lookupReused (name) ifAbsent (aBlock) ifPresent (pBlock)
lookupLexically (name) ifAbsent (aBlock)
definesLocallyOrReuses (name) → Boolean
dialectError (aString) → None
// raises a CompilationError
localNamesAndValuesDo (aBlock) → Done
removeReused (aName) ifAbsent (aBlock) → Done
lookupLocallyOrOutwards (name) ifAbsent (aBlock) → Variable
add (aVariable) → Variable
localNames → Dictionary
lookupLocally (name) ifAbsent (aBlock) → Variable
reusedNamesAndValuesDo (aBlock) → Done
definesLocally (name) → Boolean
// Is name defined in this scope (ignoring surrounding scopes and required methods)
doesNotDefineLocally (name) → Boolean
lookupLocallyOrReused (name) ifAbsent (aBlock) ifPresent (pBlock) → Variable
isObjectScope → Boolean
variety → String
clear → Done
withSurroundingScopesDo (b) → Done
// do b in this scope and all surrounding scopes.
}
type Variable = interface {
canBeOriginOfSuperExpression → Boolean
canonicalName → String
declaredName → String
definingParseNode → AstNode
definingScope → Scope
isAnnotatedConfidential → Boolean
isAnnotatedPublic → Boolean
isAnnotatedReadable → Boolean
isAnnotatedWith (anAnnotationName) → Boolean
isAnnotatedWritable → Boolean
isAssignable → Boolean
isAvailableForReuse → Boolean
isConcrete → Boolean
isConfidential → Boolean
isExplicitMethod → Boolean
isIfThenElse → Boolean
isImport → Boolean
isMatchCase → Boolean
isMethod → Boolean
isMethodOrType → Boolean
isOnceMethod → Boolean
isPublic → Boolean
isPublicByDefault → Boolean
isReadable → Boolean
isSpecialControlStructure → Boolean
isTryCatch → Boolean
isType → Boolean
isTypeParameter → Boolean
isWritable → Boolean
kind → String
lineRangeString → String
moduleName → String
range → Range
rangeLongString → String
rangeString → Boolean
startPosition → Position
stopPosition → Position
}
class line (l:Number) column (c:Number) → Position {
use equality
def line is public = l
def column is public = c
method > (other:Position) → Boolean {
if (line > other.line) then { return true }
if (line < other.line) then { return false }
(column > other.column)
}
method ≥ (other:Position) → Boolean {
if (line > other.line) then { return true }
if (line < other.line) then { return false }
(column ≥ other.column)
}
method == (other:Position) → Boolean {
(line == other.line) && (column == other.column)
}
method hash → Number {
hashCombine(line.hash, column.hash)
}
method ≤ (other:Position) → Boolean {
(other > self).not
}
method < (other:Position) → Boolean {
(other ≥ self).not
}
method asString { "{line}:{column}" }
method addColumn(n) { line (line) column (column+n) }
method addLine(n) { line (line+n) column (column) }
}
class start (s:Position) end (e:Position) → Range {
use equality
def start is public = s
def end is public = e
method asString { rangeString }
method == (other) { (start == other.start) && (end == other.end) }
method < (other) { (start < other.start) }
method ≤ (other) { (start ≤ other.start) }
method > (other) { (start > other.start) }
method ≥ (other) { (start ≥ other.start) }
method hash → Number { hashCombine(start.hash, end.hash) }
method rangeLongString {
// returns a range string such as "line 17 column 5" ,
// "line 17 columns 5 to 25", or "line 17 column 5 to line 22 column 10"
if ((start == end) || (end == noPosition)) then {
"line {start.line} column {start.column}"
} elseif { end.line == start.line } then {
"line {start.line}, columns {start.column}-{end.column}"
} else {
"line {start.line} column {start.column} to line " ++
"{end.line} column {end.column}"
}
}
method line { start.line }
method column { start.column }
method arrow { ("-" * (start.column-1)) ++ ("^" * (end.column - start.column + 1)) }
// so that we can behave as an error object
method sugg { [] }
// so that we can behave as an error object
method rangeString {
// returns a range string such as "17:5" , "17:5-25" or "17:5–22:10"
if ((start == end) || (end == noPosition)) then {
start.asString
} elseif { end.line == start.line } then {
"{start}-{end.column}"
} else {
"{start}-{end}"
}
}
method lineRangeString {
// returns a line range such as "line 17", or "lines 17–21"
if ((start.line == end.line) || (end == noPosition)) then {
"line {start.line}"
} else {
"lines {start.line}-{end.line}"
}
}
}
once method noPosition { line 0 column 0 }
once method emptyRange { start (noPosition) end (noPosition) }
type AstNode = interface {
isDeclaredByParent → Boolean
// for a declaration node or identifier, true when the thing being
// declared is decared in the outerscope, rather than in scope
kind → String
// Used for pseudo-instanceof tests, and for printing
register → String
// Used in the code generator to name the resulting object
line → Number
// The source line the node came from; the first line is 1
line:=(ln:Number)
column → Number
// the first column is 1
column:=(lp:Number)
scope → Scope
// The symbol table for names defined in this node and its sub-nodes
pretty(n:Number) → String
// Pretty-print-string of node at depth n
comments → AstNode
// Comments associated with this node
range → Range
// The source range represented by this node
start → Position
// The start of the source range represented by this node
end → Position
// The end of the source range represented by this node
}
once class nullNode → AstNode {
use identityEquality
def kind is public = "null"
def register is public = "null"
def line is public = 0
def column is public = 0
def scope is public = fakeSymbolTable
method decType {
self // can't be Unknown, because ast depends on this module
}
method nameString { asString }
method isMarkerDeclaration { false }
method hasAnnotation(_) { false }
method isDeclaredByParent { false }
method isMember { false }
method isMethod { false }
method isDialect { false }
method isModule { false }
method isExecutable { false }
method isCall { false }
method isComment { false }
method isClass { false } // is a method that returns a fresh object
method inClass { false } // object in a syntactic class definiton
method isTrait { false } // is a method that returns a trait object
method inTrait { false } // object in a syntactic trait definition
method isAssignment { false }
method isReturn { false }
method isSelf { false }
method isSuper { false }
method isBuiltIn { false }
method isOuter { false }
method isSelfOrOuter { false }
method isBlock { false }
method isObject { false }
method isIdentifier { false }
method isImport { false }
method isTypeDec { false }
method isExternal { false }
method isFresh { false }
method isConstant { false }
method isSequenceConstructor { false }
method start { line (line) column (column) }
method childrenDo(anAction:Procedure1) { done }
method childrenMap(f:Function1) { [] }
method newAccept(aVisitor) {
aVisitor.preVisit(self)
aVisitor.postVisit(self) result(aVisitor.newVisitNull(self))
}
method toGrace(depth) {
"// null"
}
method range { emptyRange }
method endCol { 0 }
method annotations { [] }
method end → Position { line (0) column (0) }
method asString { "the nullNode" }
method isNull { true }
method accept(visitor) from (ac) { }
method map(blk) ancestors(ac) { self }
method shallowCopy { self }
method column:=(_) { ProgrammingError.raise "Can't change column of nullNode" }
method comments { ProgrammingError.raise "Can't get comments from nullNode" }
method line:=(_) { ProgrammingError.raise "Can't change line of nullNode" }
method pretty(n) { toGrace(n) }
}
class fakeSymbolTable is public {
use identityEquality
var node is public // will be initialized when this node
// is placed in an AstNode using scope:=(_).
// Can't make it nullNode now, because nullNode
// initializes its scope to fakeSymbolTable
method asString { "the fakeSymbolTable" }
method isFresh { false }
method varsAreMethods { true }
method variety { "fake" }
method areReusedNamesCompleted { true }
method localNames { [] }
}
method canonicalName(n) {
// converts n, a minigrace numeric method name, to the canonical name
native "js" code ‹return new GraceString(canonicalMethodName(var_n._value));›
}
method numericName(c) {
// converts c, a Grace canonical method name, to minigrace's numeric name
native "js" code ‹return new GraceString(numericMethodName(var_c._value));›
}
once method CompilerError { ProgrammingError.refine "CompilerError" }
once method GatheringError { CompilerError.refine "GatheringError" }
}