-
Notifications
You must be signed in to change notification settings - Fork 2
/
typechecker.grace
350 lines (278 loc) · 11 KB
/
typechecker.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
import "evaluator" as e
import "combinator-collections" as c
use c.abbreviations
import "errors" as errors
use errors.exports
import "type-model" as runtime
import "utility" as utility
use utility.exports
import "loader" as loader
import "subtyping" as subtyping
//method jdebug(block) {block.apply}
method jdebug(block) { }
method DEBUG(block) {block.apply}
method debugPrint(string) { }
var ng is public //evil evil dep injection. should be some kind of import
class jcheckFamily {
inherit e.jevalFamily
alias eNodeAt(_) = nodeAt(_)
alias eStringLiteralNode(_) at(_) = stringLiteralNode(_) at(_)
alias eNumberLiteralNode(_) at(_) = numberLiteralNode(_) at(_)
alias eInterfaceNode(_) at(_) = interfaceNode(_) at(_)
alias eExplicitRequestNode(_,_,_,_) at(_) = explicitRequestNode(_,_,_,_) at(_)
alias eImplicitRequestNode(_,_,_) at(_) = implicitRequestNode(_,_,_) at(_)
alias eDefDeclarationNode(_,_,_,_) at(_) = defDeclarationNode(_,_,_,_) at(_)
alias eVarDeclarationNode(_,_,_,_) at(_) = varDeclarationNode(_,_,_,_) at(_)
alias eMethodNode(_,_,_,_) at(_) = methodNode(_,_,_,_) at(_)
alias eBlockNode(_,_) at(_) = blockNode(_,_) at(_)
alias eReturnNode(_) at(_) = returnNode(_) at (_)
alias eObjectConstructorNode(_,_) at(_) = objectConstructorNode(_,_) at(_)
alias eModuleNode(_,_) at(_) = moduleNode(_,_) at(_)
alias eInheritNode(_,_,_,_) at(_) = inheritNode(_,_,_,_) at(_)
alias eImportNode(_,_,_) at(_) = importNode(_,_,_) at(_)
method asStringPrefix { "jcheck." }
var nodeCounter := 0
class nodeAt( source ) -> Node {
inherit eNodeAt(source)
def asStringPrefix = "jcheck."
def nodeID is public = nodeCounter
nodeCounter := nodeCounter + 1
//the core of the tree-walking interpreter
//
//eval, well, evaluates stuff
//build called by "Two phase" Contexts, e.g. object constuctors, methods
//first phase is build, second is eval.
//
//only "declarations" that build attributes into contexts
// should implement build
// declarations should add themsevles to the context in build
// declarations should initialise themselves in eval
//
//expressions should ignore build, and eval themselves on eval
method build(ctxt) -> ng.NGO { ng.ngBuild } //NOOP
method eval(ctxt) -> ng.NGO { error "can't eval {self}" }
}
class stringLiteralNode(
value' : String)
at ( source ) -> Parameter {
inherit eStringLiteralNode( value' ) at( source )
method eval(ctxt) { (ctxt.lookupLexical "stringLiteral").value }
}
class numberLiteralNode(
value' : String)
at ( source ) -> Parameter {
inherit eNumberLiteralNode( value' ) at( source )
method eval(ctxt) { (ctxt.lookupLexical "numberLiteral").value }
}
class interfaceNode(
signatures' : Sequence[[Signature]])
at ( source ) -> Parameter {
inherit eInterfaceNode(signatures') at( source )
method eval(ctxt ) { ng.ngTypeType( subtyping.objectType( ng.ngInterface( self, ctxt ) ) ) }
}
class blockNode(
parameters' : Sequence[[Parameter]],
body' : Sequence[[Statement]])
at ( source ) -> Parameter {
inherit eBlockNode( parameters', body' ) at( source )
method eval(ctxt) {
//seems to come from block attributes??
//oops. somehow have to
def params = parameters.asList
def prognBody = ng.progn(body)
def subtxt = ctxt.subcontext
//TODO - haven't built any parameters!!!!
prognBody.build(subtxt)
prognBody.eval(subtxt)
def ret = ng.ngType(
subtyping.blockType(
ng.ngBlock(self,ctxt),
parameters,
subtyping.unknownObjectType, //should be returnType
ctxt))
ret
}
}
class defDeclarationNode(
name' : String,
typeAnnotation' : Expression,
annotations' : Sequence[[Expression]],
value' : Expression)
at ( source ) -> Parameter {
inherit eDefDeclarationNode(name', typeAnnotation', annotations', value')
at( source )
method build(ctxt) {
def annots = safeFuckingMap { a -> a.eval(ctxt) } over(annotations)
def properties = utility.processAnnotations(annots,false)
ctxt.declareDef(name) asType(typeAnnotation) properties(properties)
}
method eval(ctxt) {
def tat = typeAnnotation.eval(ctxt)
def vt = value.eval(ctxt)
//print "about to sTC:{tat}\nAGAINST:{vt}"
if (!tat.staticTypeCheck(vt)) then {
print "TYPE ERROR in def: {vt} does not have type {tat}"
}
ctxt.getLocal(name).initialValue:= value.eval(ctxt)
ng.ngDone
}
}
class varDeclarationNode(
name' : String,
typeAnnotation' : Expression,
annotations' : Sequence[[Expression]],
value' : Expression)
at ( source ) -> Parameter {
inherit defDeclarationNode(name', typeAnnotation', annotations', value')
at( source )
method build(ctxt) {
def annots = safeFuckingMap { a -> a.eval(ctxt) } over(annotations)
def properties = utility.processVarAnnotations(annots)
ctxt.declareVar(name) asType(typeAnnotation) properties(properties)
}
}
class methodNode(
signature' : Signature,
body' : Sequence[[Statement]],
annotations' : Sequence[[Expression]],
kind' : String)
at( source ) -> Method {
inherit eMethodNode(signature', body', annotations', kind') at(source)
method build(ctxt) {
//doesn't work with brands
// def annots = safeFuckingMap { a -> a.eval(ctxt) } over(annotations)
def annots = list
def properties = utility.processAnnotations(annots,true)
ctxt.declareName(signature.name)
attribute (ng.attributeMethod(self) properties(properties) inContext(ctxt) )
ng.ngDone
}
method eval(_) {
ng.ngDone }
}
class explicitRequestNode(
receiver' : Expression,
name' : String,
typeArguments' : Sequence[[Expression]],
arguments' : Sequence[[Expression]])
at ( source ) -> Parameter {
inherit eExplicitRequestNode(receiver', name', typeArguments', arguments')
at( source )
method eval(ctxt) {
print "{name} {arguments.size}"
def creatio = ctxt.creatio
def argCtxt = ctxt.withoutCreatio
def rcvr = receiver.eval(argCtxt)
def types = safeFuckingMap { ta -> ta.eval(argCtxt) } over(typeArguments)
def args = safeFuckingMap { a -> a.eval(argCtxt) } over(arguments)
print "RCVR {rcvr}"
def methodBody = rcvr.lookupExternal(name)
if (rcvr != rcvr.whole) then {
print "about to crash"
print "requesting {name}"
print "RCVR"
print (rcvr)
print "RCVR WHOLE"
print (rcvr.whole) }
assert {rcvr.isWhole}
def mySelf = ctxt.getInternal("self").value
def isSpecialRequest = mySelf.isInside(rcvr)
if (! (methodBody.isPublic || isSpecialRequest) )
then {error "External request for confidential attribute {name}"}
//if (!methodBody.isPublic) then {error "External request for confidential attribute {name}"}
//def isSpecialRequest =
// ImplicitRequestNodeBrand.match(receiver).andAlso {
// (receiver.name == "self") || (receiver.name == "outer") }
def rv = methodBody.invoke(rcvr) args(args) types(types) creatio(creatio)
rv
}
}
class implicitRequestNode(
name' : String,
typeArguments' : Sequence[[Expression]],
arguments' : Sequence[[Expression]])
at ( source ) -> Parameter {
inherit eImplicitRequestNode(name', typeArguments', arguments')
at( source )
method eval(ctxt) {
def creatio = ctxt.creatio
def argCtxt = ctxt.withoutCreatio
def types = safeFuckingMap { ta -> ta.eval(argCtxt) } over(typeArguments)
def args = safeFuckingMap { a -> a.eval(argCtxt) } over(arguments)
def methodBody = ctxt.lookupInternal(name)
def rv = methodBody.invoke(ctxt) args(args) types(types) creatio(creatio)
rv
}
}
class returnNode(
value' : Expression)
at ( source ) {
inherit eReturnNode( value' ) at( source )
method eval(ctxt) {
def returnCreatio = ctxt.getInternal(RETURNCREATIO).value
def returnBlock = ctxt.getInternal(RETURNBLOCK)
returnBlock.invoke(ctxt)
args(list( value.eval(ctxt) ))
types(empty)
creatio(returnCreatio)
}
}
class objectConstructorNode(
body' : Sequence[[ObjectStatement]],
origin' : Unknown)
at ( source ) -> Parameter {
inherit eObjectConstructorNode(body',origin') at(source)
method eval(ctxt) {
ng.objectContext(body,ctxt)
// def ret = ng.ngType(
// subtyping.objectConstructorType(
// ng.objectContext(body,ctxt), body, ctxt ) )
// ret
}
}
class moduleNode(
moduleDialect' : String,
body' : Sequence[[ObjectStatement]] )
at ( source ) -> Parameter {
inherit eModuleNode(moduleDialect', body') at(source)
print "making TC module size {body.size} dialect{moduleDialect}"
method eval(ctxt) {
def dialectModuleObject = loader.loadModule(moduleDialect)
ng.moduleObject(body,dialectModuleObject) }
}
//consider renaming as "parentNode"
class inheritNode(
kind' : String,
request' : Request,
excludes' : List[[String]],
aliases' : Dictionary[[String,String ]])
at ( source ) -> Parameter {
inherit eInheritNode(kind', request', excludes', aliases') at ( source )
def parentID is public = "{PARENT}:{nodeID}"
method build(ctxt) {ctxt.addParent(self)}
method eval(ctxt) {
def parentalPartObject = ctxt.getLocal(parentID)
parentalPartObject.initialize
ng.ngDone
}
}
class importNode(
path' : String,
name' : String,
typeAnnotation' : Expression)
at ( source ) -> Node {
inherit eImportNode(path',name',typeAnnotation') at ( source )
method build(ctxt) {
ctxt.declareDef(name) asType(typeAnnotation) properties(utility.confidentialAnnotations)
}
method eval(ctxt) {
def importedModule = loader.loadModule(path)
ctxt.getLocal(name).initialValue:=importedModule
ng.ngDone
}
}
method context { ng.context }
method lexicalContext(c) { ng.lexicalContext(c) }
method moduleObject(b,c) { ng.moduleObject(b, c) }
}
def singleton is public = jcheckFamily