-
Notifications
You must be signed in to change notification settings - Fork 22
/
pattern+typeBundle.grace
195 lines (175 loc) · 6.31 KB
/
pattern+typeBundle.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
dialect "none"
import "equalityBundle" as equalityBundle
import "collections" as coll
import "intrinsic" as intrinsic
// This module _defines_ the & and | operations on types. Hence, we
// _cannot_ define or use here any types made with & or |.
def ic = intrinsic.controlStructures
use intrinsic.annotations
trait open {
use equalityBundle.open
trait BasePattern {
method &(o) {
AndPattern(self, o)
}
method |(o) {
OrPattern(self, o)
}
method prefix ¬ {
NotPattern(self)
}
method isType { false }
method setTypeName(_) is confidential {
// This method exists so that if compiled code tries to setTypeName
// on a pattern, we will get a nicer error than NoSuchMethod.
// It is confidetial so that it does not show up in the Pattern type.
intrinsic.constants.TypeError.raise "{self} is a Pattern, but not a Type"
}
}
trait AndPattern(p1, p2) {
use BasePattern
method matches(obj) {
if (p1.matches(obj)) then { p2.matches(obj) } else { false }
}
}
trait OrPattern(p1, p2) {
use BasePattern
method matches(o) {
if (p1.matches(o)) then { true } else { p2.matches(o) }
}
}
trait NotPattern(p) {
use BasePattern
method matches(o) {
p.matches(o).not
}
}
trait BaseType {
use identityEquality
method &(o) { TypeIntersection(self, o) }
method |(o) { TypeVariant(self, o) }
method -(o) { TypeExclusion(self, o) }
method prefix ¬ { NotPattern(self) }
method asString { "type {self.name}" }
method setName(nu) is confidential {
self.name := nu
self // for chaining
}
method setTypeName(nu) is confidential {
// requested from compiled code when a named type is declared
self.name := nu
self // for chaining
}
method name:=(nu) is required
method name is required
method matchHook(obj) is required // does the actual matching
method matches(obj) {
// this caches the result of matchHook, under the assumption
// that two objects with the same classUid will have the same type
native "js" code ‹
if (! this.matchCache) this.matchCache = [];
let key = var_obj.classUid;
result = this.matchCache[key];
if (result) return result;
result = selfRequest(this, "matchHook(1)", [1], var_obj);
this.matchCache[key] = result;
return result;
›
}
method isNone { false }
method isType { true }
method typeParameterNames {
native "js" code ‹
if (! this.typeParamNames) {
return new GraceSequence([]);
}
return new GraceSequence(this.typeParamNames.map(
nm => new GraceString(nm)));
›
}
method interfaces is required
method isInterface { false }
}
method TypeIntersection (t1, t2) {
if (t2.isType.not) then { return t2 & t1 } // double-dispatch to Pattern t2
if (t2.isNone) then {return t2} // if t1.isNone, we won't come here
AndType (t1, t2)
}
class AndType (t1, t2) {
// Objects of this class are used to represent Self & <an interface> and
// Unknown & <an interface>, becuase apb doesn't yet know what else to do.
// The & of two interfaces generates another "Interface-built-in", and
// not an instance of this class.
use AndPattern (t1, t2)
alias matchHook(_) = matches(_)
exclude &(_)
exclude |(_)
exclude matches(_)
exclude isType
exclude setTypeName(_)
exclude prefix ¬
use BaseType
// t1 can't be a GraceInterface (from gracelib) — it must be an
// exclusion
var name is readable := "{t1.name} & {t2.name}"
def interfaces is readable = coll.sequence.withAll( // because map is lazy
t1.interfaces.map { each -> each & t2 }
)
method asDebugString {
var result := ""
interfaces.do { each -> result := result ++ each.name }
separatedBy { result := result ++ " | " }
result
}
}
method TypeVariant (t1, t2) {
if (t2.isType.not) then { return t2 | t1 } // double-dispatch to Pattern t2
if (t1.isNone) then {return t2}
if (t2.isNone) then {return t1}
BarType(t1, t2)
}
class BarType(t1, t2) {
use OrPattern (t1, t2)
alias matchHook(_) = matches(_)
exclude &(_)
exclude |(_)
exclude matches(_)
exclude isType
exclude setTypeName(_)
exclude prefix ¬
use BaseType
var name is readable := { // not a once method, because
// compiled code uses name:=(_) to set the name
def t1Name = if (t1.name.startsWith "(") then { t1.name }
elseif { t1.name.contains "&" } then { "({t1.name})" }
else { t1.name }
def t2Name = if (t2.name.startsWith "(") then { t2.name }
elseif { t2.name.contains "&" } then { "({t2.name})" }
else { t2.name }
"{t1Name} | {t2Name}"
}.apply
once method interfaces {
def result = t1.interfaces >> coll.list
t2.interfaces.do { each ->
if (! result.contains(each)) then {
result.addLast(each)
}
}
result >> coll.sequence
}
method isInterface { false }
}
class TypeExclusion (t1, t2) {
use BaseType
var name is readable := "{t1} - {t2}"
if (t2.isInterface.not) then {
intrinsic.constants.TypeError.raise
"right-hand argument to `-` operator is not an interface"
}
once method interfaces {
coll.sequence.withAll( // because map is lazy
t1.interfaces.map { each -> each - t2 }
)
}
}
}