-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprimitives.grace
215 lines (173 loc) · 6.31 KB
/
primitives.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
import "subtyping" as subtyping
trait abstractPrimitives {
method ngPrimitive is abstract { }
method ngNumber( value' ) is abstract { }
method ngString( value' ) is abstract { }
method ngBoolean( value' ) is abstract { }
method ngInterface( value', ctxt ) is abstract { }
method ngBlock(blockNode,ctxt) is abstract { }
method ngBuiltinAnnotation(description' : String) is abstract { }
method ngDone is abstract { }
method ngImplicitDone is abstract { }
method ngBuild is abstract { }
method ngUninitialised is abstract { }
method ngUnknown is abstract { }
method ngImplicitUnknown is abstract { }
method ngNotCreatio is abstract { }
}
class primitivesFamily {
method context is abstract { }
method lexicalContext(_) is abstract { }
method attributeBlockApplyMethod(_) inContext(_) is abstract { }
method attributeBlockMatchMethod(_) inContext(_) is abstract { }
/////////////////////////////////////////////////////////////
////
//// primitivies
////
/////////////////////////////////////////////////////////////
class ngPrimitive {
inherit context
method lookupInheritance(name) { lookupLocal(name) } //primitives only have local slots
method whole { self } //HMM. should clarify actual interface
method asString { "ngPrimitive (should be abstract)"}
declareName "asString" lambda { creatio ->
def rv = ngString(asString)
rv }
}
class ngNumber( value' ) {
inherit ngPrimitive
method kind {"ngNumber"}
method value {value'}
method asString { "ngNumber: {value}"}
declareName "+(_)" lambda { other, creatio ->
def rv = ngNumber(value' + other.value)
rv }
}
class ngString( value' ) {
inherit ngPrimitive
method kind {"ngString"}
method value {value'}
method asString { "ngString: {value}"}
declareName "++(_)" lambda { other, creatio ->
def rv = ngString(value' ++ other.value)
rv }
}
class ngBoolean( value' ) {
inherit ngPrimitive
method kind {"ngBoolean: {value}"}
method value {value'}
method asString { "ngBoolean: {value}"}
// needs some methods which will need the context -- or will it??
declareName "prefix!" lambda { _ ->
def rv = ngBoolean(! value)
rv }
declareName "&&(_)" lambda { other, _ ->
def rv = ngBoolean(value && other.value)
rv }
declareName "ifTrue(_)" lambda2 { block, creatio, ctxt ->
if (value) then {
def argCtxt = ctxt.withoutCreatio
def creatio = argCtxt.creatio
def matchAttribute = block.lookupExternal("apply")
def matchResult = matchAttribute.invoke(ctxt) args(empty) types(empty) creatio(creatio)
matchResult
} else {ngDone}
}
}
class ngInterface( value', ctxt ) {
//cheating, just points to ast node - and context
inherit ngPrimitive
method kind {"ngInterface"}
method value {value'}
method context { ctxt }
method asString {
def sigs = safeFuckingMap { sig -> sig.name } over (value.signatures)
"ngInterface: #{dbg} n{value.nodeID} {sigs}"}
method match(other) {//assumes other is an NGO
for (value.signatures) do { sig ->
if (other.lookupExternal(sig.name).isMissing) then { return false }
}
true
}
method isSubtypeOf(other) {
subtyping.check(self) isSubtypeOf(other)
}
method isTypeEquals(other) {
subtyping.check(self) isTypeEquals(other)
}
declareName "match(_)" lambda { other, creatio ->
ngBoolean(match(other)) }
declareName "<:(_)" lambda { other, creatio ->
ngBoolean(isSubtypeOf(other)) }
}
class ngBlock(blockNode,ctxt) {
inherit lexicalContext(ctxt)
method asString { "\{a ngBlock\} #{dbg}" }
method kind {"ngBlock"}
def params = blockNode.parameters.asList
def suffix = match (params.size)
case { 0 -> "" }
case { 1 -> "(_)" }
case { 2 -> "(_,_)" }
case { 3 -> "(_,_,_)" }
case { 4 -> "(_,_,_,_)" }
case { 5 -> "(_,_,_,_,_)" }
case { _ -> error "CANT BE BOTHERED TO APPLY MORE VARARGS" }
declareName("apply" ++ suffix)
attribute (attributeBlockApplyMethod(blockNode) inContext(ctxt))
declareName("match" ++ suffix)
attribute (attributeBlockMatchMethod(blockNode) inContext(ctxt))
declareName "asString" lambda { creatio ->
def rv = ngString(asString)
rv }
}
/////////////////////////////////////////////////////////////
////
//// singletons
////
/////////////////////////////////////////////////////////////
def ngDone is public = object {
inherit ngPrimitive
method kind{"ngDone"}
method asString { "ngDone"}
}
def ngImplicitDone is public = object {
inherit ngPrimitive
method kind{"ngImplicitDone"}
method asString { "ngImplicitDone"}
}
def ngBuild is public = object {
inherit ngPrimitive
method kind {"ngBuild"}
method asString { "ngBuild"} //result returned from build. always an error.
}
def ngUninitialised is public = object {
inherit ngPrimitive
method kind {"ngUninit"}
method asString { "ngUninitialised" } //also an error if accessed
}
def ngUnknown is public = object {
inherit ngInterface(ngUninitialised,context)
method kind {"ngUnknown"}
method asString { "ngUnknown" }
method match(other) {true}
}
def ngImplicitUnknown is public = object {
inherit ngInterface(ngUninitialised,context)
method match(other) {true}
method kind {"ngImplicitUnknown"}
method asString { "ngImplicitUnknown" }
}
def ngNotCreatio is public = object {
inherit ngPrimitive
method kind {"ngNotCreatio"}
method asString { "ngNotCreatio" }
method isCreatio { false }
}
class ngBuiltinAnnotation(description' : String) {
inherit ngPrimitive
method kind {"ngBuiltinAnnotation"}
method asString { "ngBuiltinAnnotation(\"{description}\")" }
method description { description' }
}
}