-
Notifications
You must be signed in to change notification settings - Fork 2
/
attributes.grace
334 lines (298 loc) · 12.9 KB
/
attributes.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
import "combinator-collections" as c
use c.abbreviations
import "errors" as errors
use errors.exports
trait abstractAttributes {
method attributeDef(origin) asType(typeAnnotation) properties(properties) inContext(ctxt) is abstract { }
method attributeVar(origin) asType(typeAnnotation) properties(properties) inContext(ctxt) is abstract { }
method attributeMethod(methodNode) properties(properties) inContext(ctxt) is abstract { }
method attributeBlockMatchMethod(blockNode) inContext(ctxt) is abstract { }
method attributeBlockApplyMethod(blockNode) inContext(ctxt) is abstract { }
method attributeValue(value') inContext(ctxt) is abstract { }
method attributeMissing(name) inContext(ctxt) is abstract { }
method attributeAmbiguous(name) between(possibilities) inContext(ctxt) is abstract { }
method attributeLambda(lambda) inContext(ctxt) is abstract { }
method attributeLambda2(lambda) inContext(ctxt) is abstract { }
method attributeWrapper(subject) privacy(shouldBePublic) is abstract { }
method Attribute is abstract { } //abstract type :-)
}
class attributesFamily {
method ngUninitialised is abstract { }
method ngDone is abstract { }
method ngImplicitUnknown is abstract { }
method progn(_) is abstract { }
method ngBoolean(_) is abstract { }
type O = Unknown
type Context = Unknown
type Attribute = {
invoke(this: O) args(args: Sequence[[O]]) types(typeArgs: Sequence[[O]]) creatio(creatio) -> O
isPublic -> Boolean
isAbstract -> Boolean
isOverride -> Boolean
isMissing -> Boolean //usually false. TRUE if lookup failed!!
asPublic(Boolean) -> Attribute
context -> Context
}
class attributeDef(origin) asType(typeAnnotation) properties(properties) inContext(ctxt) {
use utility.annotationsTrait(properties)
use changePrivacyAnnotations
assert {!properties.isAbstract} because "A field can't be abstract"
var boxValue := ngUninitialised
method initialValue:= (initialValue) {
if (boxValue != ngUninitialised) then { error "can't initialise initailsed box" }
check(initialValue) isType(typeAnnotation) inContext(ctxt)
boxValue := initialValue
}
method invoke(this) args(args) types(typeArgs) creatio(_) {
assert {args.size == 0}
assert {typeArgs.size == 0}
value
}
method value {
if (ngUninitialised == boxValue)
then { error "can't access uninitailsed box" }
boxValue }
method asString {"attributeDef: {origin} = {boxValue}"}
method context { ctxt }
}
class attributeVar(origin) asType(typeAnnotation) properties(properties) inContext(ctxt) {
inherit attributeDef(origin) asType(typeAnnotation) properties(properties.getter) inContext(ctxt)
alias varInitialValue:=(_) = initialValue:=(_)
method initialValue:=(initialValue) {
if (initialValue != ngUninitialised)
then { varInitialValue:=(initialValue) }
}
def setter is public = object {
use utility.annotationsTrait(properties.setter)
use changePrivacyAnnotations
method invoke(this) args(args) types(typeArgs) creatio(creatio) {
assert {args.size == 1}
assert {typeArgs.size == 0}
def newValue = args.at(1)
check(newValue) isType(typeAnnotation) inContext(ctxt)
boxValue:= newValue
ngDone
}
method context { ctxt }
method asString {"attributeVar (setter): {origin} := {boxValue}"}
}
method asString {"attributeVar (getter): {origin} := {boxValue}"}
method context { ctxt }
}
//an attribute method..
class attributeMethod(methodNode) properties(properties) inContext(ctxt) {
use utility.annotationsTrait(properties)
use changePrivacyAnnotations
method invoke(this) args(args) types(typeArgs) creatio(creatio) {
//print "invoke {methodNode.signature.name} on #{this.dbg} creatio:{creatio.isCreatio}"
//should this not have a creation??
def typetxt = ctxt.subcontextNamed(methodNode.signature.name ++ "(types)")
def typeParams = methodNode.signature.typeParameters.asList
if (typeArgs.size == typeParams.size)
then {
for (typeParams.indices) do { i ->
typetxt.declareName(typeParams.at(i).name) value(typeArgs.at(i)) } }
elseif {typeArgs.size == 0}
then {
for (typeParams.indices) do { i ->
typetxt.declareName(typeParams.at(i).name) value(ngImplicitUnknown) } }
else {error "generic arg mismatch"}
def returnType = methodNode.signature.returnType
def subtxt = typetxt.subcontextNamed(methodNode.signature.name)
def params = methodNode.signature.parameters.asList
if (args.size != params.size) then {error "arg mismatch"}
for (params.indices) do { i ->
def par = params.at(i)
def arg = args.at(i)
check(arg) isType(par.typeAnnotation) inContext(typetxt)
subtxt.declareName(par.name) value(arg)
}
subtxt.addLocal(CREATIO) value(creatio)
subtxt.addLocal(RETURNBLOCK)
slot (attributeLambda {rv, _ ->
check(rv) isType(returnType) inContext(typetxt)
return rv} inContext(subtxt))
subtxt.addLocal(RETURNCREATIO) value (creatio)
def prognBody = progn(methodNode.body)
prognBody.build(subtxt)
def rv = prognBody.eval(subtxt)
check(rv) isType(returnType) inContext(typetxt)
rv
}
method context { ctxt }
method asString {"attributeMethod: {methodNode.signature.name} #{ctxt.dbg}"}
}
class attributeBlockMatchMethod(blockNode) inContext(ctxt) {
use utility.publicAnnotations
use changePrivacyAnnotations
method invoke(this) args(args) types(typeArgs) creatio(creatio) {
def params = blockNode.parameters.asList
def prognBody = progn(blockNode.body)
if (args.size != params.size) then {error "arg mismatch"}
for (params.indices) do { i ->
def par = params.at(i)
def arg = args.at(i)
if (!test(arg) isType(par.typeAnnotation) inContext(ctxt))
then {return ngBoolean(false)}
}
return ngBoolean(true)
}
method context { ctxt }
method asString {"attributeBlockMethod"}
}
class attributeBlockApplyMethod(blockNode) inContext(ctxt) {
use utility.publicAnnotations
use changePrivacyAnnotations
method invoke(this) args(args) types(typeArgs) creatio(creatio) {
def params = blockNode.parameters.asList
def prognBody = progn(blockNode.body)
def subtxt = ctxt.subcontext
if (args.size != params.size) then {error "arg mismatch"}
for (params.indices) do { i ->
def par = params.at(i)
def arg = args.at(i)
check(arg) isType(par.typeAnnotation) inContext(ctxt)
subtxt.declareName(par.name) value(arg)
}
subtxt.addLocal(CREATIO) value(creatio)
prognBody.build(subtxt)
prognBody.eval(subtxt)
}
method context { ctxt }
method asString {"attributeBlockMethod"}
}
//a ng value bound to a name in a context. already initialised!
class attributeValue(value') inContext(ctxt) {
use utility.confidentialAnnotations
use changePrivacyAnnotations
method invoke(this) args(args) types(typeArgs) creatio(creatio) {
assert {(args.size == 0) && (typeArgs.size == 0) && (!creatio.isCreatio)}
value
}
method context { ctxt }
method value { value' }
method asString {"attributeValue: {value}"}
}
//potentially every obejct could be attribute, so we don't need this.
//too confusing to put in now.
//what lookup retuns when it doesn't find anything.
class attributeMissing(name) inContext(ctxt) {
//print "MISSING {name} {ctxt}"
use utility.publicAnnotations
use changePrivacyAnnotations
method isMissing { true }
method invoke(this) args(args) types(typeArgs) creatio(creatio) {
error "{name} is missing from {ctxt}"
}
method context { ctxt }
method asString {"attributeMissing: {name} at {ctxt}"}
}
//what lookup retuns when it's abiguous
class attributeAmbiguous(name) between(possibilities) inContext(ctxt) {
inherit attributeMissing(name) inContext(ctxt)
method invoke(this) args(args) types(typeArgs) creatio(creatio) {
error "{name} is ambiguous at {ctxt} between {possibilities}"
}
method context { ctxt }
method asString {"inocableAmbiguous {name} in {ctxt}"}
}
//old style attribute that wraps a lambda block;
//blocks takes arguments plus creatio. BUT NO CONTEXT
//use for primitives but otherwise avoid
class attributeLambda(lambda) inContext(ctxt) {
use utility.publicAnnotations
use changePrivacyAnnotations
method invoke(this) args(args) types(typeArgs) creatio(creatio) {
applyVarargs(lambda,args,creatio)
}
//apply the block to the LIST of arguments from the interpreter
//args are already evaluated
method applyVarargs(block,args,creatio) {
def a = args.asList
match (args.size)
case { 0 -> block.apply(creatio)}
case { 1 -> block.apply(a.at(1),creatio)}
case { 2 -> block.apply(a.at(1),a.at(2),creatio)}
case { 3 -> block.apply(a.at(1),a.at(2),a.at(3),creatio)}
case { 4 -> block.apply(a.at(1),a.at(2),a.at(3),a.at(4),creatio)}
case { 5 -> block.apply(a.at(1),a.at(2),a.at(3),a.at(4),a.at(5),creatio)}
case { _ -> error "CANT BE BOTHERED TO APPLY MORE VARARGS" }
}
method context { ctxt }
method asString { "attributeLambda {lambda}" }
}
//new style attribute that wraps a lambda block;
//blocks takes arguments plus creatio and context.
//should use this one rather than the other one
class attributeLambda2(lambda) inContext(ctxt) {
use utility.publicAnnotations
use changePrivacyAnnotations
method invoke(this) args(args) types(typeArgs) creatio(creatio) {
applyVarargs2(lambda,args,creatio)
}
//apply the block to the LIST of arguments from the interpreter
//args are already evaluated
method applyVarargs2(block,args,creatio) {
def a = args.asList
match (args.size)
case { 0 -> block.apply(creatio,ctxt)}
case { 1 -> block.apply(a.at(1),creatio,ctxt)}
case { 2 -> block.apply(a.at(1),a.at(2),creatio,ctxt)}
case { 3 -> block.apply(a.at(1),a.at(2),a.at(3),creatio,ctxt)}
case { 4 -> block.apply(a.at(1),a.at(2),a.at(3),a.at(4),creatio,ctxt)}
case { 5 -> block.apply(a.at(1),a.at(2),a.at(3),a.at(4),a.at(5),creatio,ctxt)}
case { _ -> error "CANT BE BOTHERED TO APPLY MORE VARARGS" }
}
method context { ctxt }
method asString { "attributeLambda {lambda}" }
}
//dynamic type check.
//Takes a typeExpression and a context, not a type object
// because it's mostly used to check against declared types
method test(obj) isType(typeExpression) inContext(ctxt) {
def typeObject = typeExpression.eval(ctxt.withoutCreatio)
//if (!typeObject.match(obj))
// then { error "type check failed: {obj} isnt {typeObject} from {typeExpression}" }
def argCtxt = ctxt.withoutCreatio
def creatio = argCtxt.creatio
def matchAttribute = typeObject.lookupExternal("match(_)")
def matchResult = matchAttribute.invoke(ctxt) args(list(obj)) types(empty) creatio(creatio)
return matchResult.value
}
method check(obj) isType(typeExpression) inContext(ctxt) {
if (!test(obj) isType(typeExpression) inContext(ctxt))
then {
def typeObject = typeExpression.eval(ctxt.withoutCreatio)
print "type check failed: {obj} isnt {typeObject} from {typeExpression}" }
obj
}
//behaviour to change privacy annotations
trait changePrivacyAnnotations {
method isPublic is abstract { }
method asPublic(shouldBePublic : Boolean) {
if (isPublic == shouldBePublic)
then {self}
else {attributeWrapper(self) privacy(shouldBePublic)}
}
}
//proxy to change an attribute's privacy
class attributeWrapper(subject) privacy(shouldBePublic) {
assert {subject.isPublic != shouldBePublic} //or else shouldn't be here
method isPublic { shouldBePublic }
method asPublic(shouldBePublic : Boolean) {
if (isPublic == shouldBePublic)
then {self}
elseif {subject.isPublic == shouldBePublic}
then {subject}
else { error "asPublic(_): should never happen - excluded middle" }
}
method isOverride { subject.isOverride }
method isAbstract { subject.isAbstract }
method isMissing { subject.isMissing }
method invoke(this) args(args) types(typeArgs) creatio(creatio) {
subject.invoke(this) args(args) types(typeArgs) creatio(creatio) }
method context { subject.context }
method asString {"attributeWrapper {subject}"}
}
method context { ctxt }
}