-
Notifications
You must be signed in to change notification settings - Fork 2
/
evaluator.grace
385 lines (295 loc) · 12.4 KB
/
evaluator.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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
import "common-ast" as jm
import "combinator-collections" as c
use c.abbreviations
import "errors" as errors
use errors.exports
import "utility" as utility
use utility.exports
import "loader" as loader
var ng is public //evil evil dependency inversion
//TODO dynamic typechecks on arguments - and results
//TODO where clauses...
//TODO types!
//TODO block matching
//TODO exceptions
//TODO nother f**king look at alias/excludes
//do aliases first; check for errors
//TODO should make exclude retun an "abstract" candidate
//TODO check candidate consolidation?
//TODO alias clauses annotations change privacy?
// (need to fix kernan parser)
//TODO add Kernan primitives to let us link through to incoming source code
//TODO add Kernan support for block generic arguments
//TODO imported names should go into an extra surrounding scope
//TODO currently they go into the current scope
//TOOD this is bad because they can be inherited
//TODO although also good because can be local
//TODO refactor AST, redesign class names
//TODO refactor progn out of runtime into jast -
//TODO AST returnType -> returnTypeAnnotation (all types here are annotations?)
//TODO add sequence and statementsequence into the common AST
//TODO add "provenacne" to methods, e.g. if they came from a class or type decln
//TODO correct canonical names of of assignment methods/requests (wash your dog first)
//method jdebug(block) {block.apply}
method jdebug(block) { }
method DEBUG(block) {block.apply}
method debugPrint(string) { }
class jevalFamily {
inherit jm.jastFamily
alias jNodeAt(_) = nodeAt(_)
alias jStringLiteralNode(_) at(_) = stringLiteralNode(_) at(_)
alias jNumberLiteralNode(_) at(_) = numberLiteralNode(_) at(_)
alias jInterfaceNode(_) at(_) = interfaceNode(_) at(_)
alias jExplicitRequestNode(_,_,_,_) at(_) = explicitRequestNode(_,_,_,_) at(_)
alias jImplicitRequestNode(_,_,_) at(_) = implicitRequestNode(_,_,_) at(_)
alias jDefDeclarationNode(_,_,_,_) at(_) = defDeclarationNode(_,_,_,_) at(_)
alias jVarDeclarationNode(_,_,_,_) at(_) = varDeclarationNode(_,_,_,_) at(_)
alias jMethodNode(_,_,_,_) at(_) = methodNode(_,_,_,_) at(_)
alias jBlockNode(_,_) at(_) = blockNode(_,_) at(_)
alias jReturnNode(_) at(_) = returnNode(_) at (_)
alias jObjectConstructorNode(_,_) at(_) = objectConstructorNode(_,_) at(_)
alias jModuleNode(_,_) at(_) = moduleNode(_,_) at(_)
alias jInheritNode(_,_,_,_) at(_) = inheritNode(_,_,_,_) at(_)
alias jImportNode(_,_,_) at(_) = importNode(_,_,_) at(_)
method asStringPrefix { "jeval." }
var nodeCounter := 0
class nodeAt( source ) -> Node {
inherit jNodeAt(source)
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 jStringLiteralNode( value' ) at( source )
method eval(ctxt) { ng.ngString( value ) }
}
class numberLiteralNode(
value' : String)
at ( source ) -> Parameter {
inherit jNumberLiteralNode( value' ) at( source )
method eval(ctxt) { ng.ngNumber( value ) }
}
class interfaceNode(
signatures' : Sequence[[Signature]])
at ( source ) -> Parameter {
inherit jInterfaceNode(signatures') at( source )
method eval(ctxt ) { ng.ngInterface( self, ctxt ) }
}
class blockNode(
parameters' : Sequence[[Parameter]],
body' : Sequence[[Statement]])
at ( source ) -> Parameter {
inherit jBlockNode( parameters', body' ) at( source )
method eval(ctxt) { ng.ngBlock(self,ctxt) }
}
class defDeclarationNode(
name' : String,
typeAnnotation' : Expression,
annotations' : Sequence[[Expression]],
value' : Expression)
at ( source ) -> Parameter {
inherit jDefDeclarationNode(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) {
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 jMethodNode(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 jExplicitRequestNode(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)
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 jImplicitRequestNode(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 jReturnNode( 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 jObjectConstructorNode(body',origin') at(source)
method eval(ctxt) {
def ret = ng.objectContext(body,ctxt)
//SHIT below here is solely to handle brands
//doing this with andalso just didn't work - no idea why
if ("missing" == origin) then {return ret}
if (origin.get_Origin.isNull) then {return ret}
if (!origin.get_Origin.KJXHasAnnotations) then {return ret}
//if ( ("missing" != origin).andAlso
// {(!origin.get_Origin.isNull).andAlso
// {origin.get_Origin.KJXHasAnnotations}}) then {
def annotName = self.origin.KJXOrigin.KJXAnnotationOne.KJXName
//def argCtxt = ctxt.withoutCreatio
//def creatio = argCtxt.creatio
//def annotAttribute = ctxt.lookupInternal(annotName)
//def annotObject = annotAttribute.invoke(ctxt) args(list) types(list) creatio(creatio)
def annotImplicit = implicitRequestNode(annotName,empty,empty) at(source)
def brandObject = annotImplicit.eval(ctxt.withoutCreatio)
if (brandObject.lookupExternal("retainedAnnotation").isMissing)
then { print "NOT A BRAND" }
else { ret.declareName(brandObject) lambda { error: "FUCKED" } }
//the above else clause
//inserts the objectContext of the brand object
//in as a **method name** on the object being branded
//usually method names are strings :-)
// }
ret
}
}
class moduleNode(
moduleDialect' : String,
body' : Sequence[[ObjectStatement]] )
at ( source ) -> Parameter {
inherit jModuleNode(moduleDialect', body') at(source)
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 jInheritNode(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 jImportNode(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 = jevalFamily
//translator.commonASTFactory := singleton