-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmatch.mml
255 lines (222 loc) · 5.38 KB
/
match.mml
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
use (
. "lists"
"functions"
"ints"
"floats"
)
fn (
token() token
none() none
integer() integer
floating() floating
stringType() stringType
boolean() boolean
errorType() errorType
)
export fn (
any() any
function() function
channel() channel
)
export fn type(t) {
switch t {
case int:
return integer
case float:
return floating
case string:
return stringType
case bool:
return boolean
case error:
return errorType
default:
return t
}
}
fn complexType(name) {token: token, type: name}
let (
intRangeType complexType("int-range")
floatRangeType complexType("float-range")
)
fn (
isRange(ofType, min, max) ofType(min) && ofType(max) && min <= max
isNaturalRange(min, max) isRange(isInt, min, max) && min >= 0
)
fn defineRange(ofType, validate, min, max)
validate(min, max) ?
{ofType..., min: min, max: max} :
none
let (
intRange defineRange(intRangeType, isRange(isInt))
floatRange defineRange(floatRangeType, isRange(isFloat))
stringRangeType complexType("string")
stringRange defineRange(stringRangeType, isNaturalRange)
listType complexType("list")
)
fn listRange(item, min, max) {defineRange(listType, isNaturalRange, min, max)..., item: item}
export fn (
listOf(item) listRange(type(item), 0, ints.max)
structOf(s) is({}, s) ? s : none
)
export fn range(match, min, max) {
let m type(match)
switch {
case m == integer:
return intRange(min, max)
case m == floating:
return floatRange(min, max)
case m == stringType:
return stringRange(min, max)
case complexTypeEq(listType, m):
return listRange(m.item, min, max)
default:
return none
}
}
let (
unionType complexType("union")
intersectType complexType("intersection")
predicateType complexType("predicate")
)
export fn (
or(...matches) {unionType..., matches: map(type, matches)}
and(...matches) {intersectType..., matches: map(type, matches)}
predicate(p) {predicateType..., predicate: p}
predicates(...p) and(map(predicate, p)...)
)
fn isSimpleType(t) some(functions.bind(functions.eq, t), [
integer
floating
stringType
boolean
function
errorType
channel
])
fn isComplexType(t)
isStruct(t) &&
has("token", t) && t.token == token &&
has("type", t) && isString(t.type)
fn isType(t) isSimpleType(t) || isComplexType(t)
fn complexTypeEq(type, value)
isComplexType(type) &&
isComplexType(value) &&
type.type == value.type
let primitives {
int: {
checkValue: isInt
type: integer
rangeType: intRangeType
rangeValue: functions.identity
}
float: {
checkValue: isFloat
type: floating
rangeType: floatRangeType
rangeValue: functions.identity
}
string: {
checkValue: isString
type: stringType
rangeType: stringRangeType
rangeValue: len
}
}
fn matchPrimitive(def, match, value) {
switch {
case !def.checkValue(value):
return false
case match == def.type:
return true
case complexTypeEq(def.rangeType, match):
let rv def.rangeValue(value)
return rv >= match.min && rv <= match.max
default:
return false
}
}
let (
matchInt matchPrimitive(primitives.int)
matchFloat matchPrimitive(primitives.float)
matchString matchPrimitive(primitives.string)
)
fn matchToList(match, value) {
if len(value) < len(match) {
return false
}
for i in :len(match) {
if !is(match[i], value[i]) {
return false
}
}
return true
}
fn matchToListType(match, value)
len(value) >= match.min &&
len(value) <= match.max &&
(match.item == any || every(is(match.item), value))
fn matchList(match, value) {
switch {
case !isList(value):
return false
case isList(match):
return matchToList(match, value)
case complexTypeEq(listType, match):
return matchToListType(match, value)
default:
return false
}
}
fn matchStruct(match, value)
isStruct(value) &&
every(fn (key) has(key, value) && is(match[key], value[key]), keys(match))
fn (
matchUnion(match, value) some(fn (m) is(m, value), match.matches)
matchIntersection(match, value) every(fn (m) is(m, value), match.matches)
)
fn matchOne(match, value) {
switch {
case match == none:
return false
case match == any:
return true
case complexTypeEq(predicateType, match):
return match.predicate(value)
case isType(value) && value != function:
return false
case match == value:
return true
case match == integer || complexTypeEq(intRangeType, match):
return matchInt(match, value)
case match == floating || complexTypeEq(floatRangeType, match):
return matchFloat(match, value)
case match == stringType || complexTypeEq(stringRangeType, match):
return matchString(match, value)
case match == boolean:
return isBool(value)
case match == function:
return isFunction(value)
case match == channel:
return isChannel(value)
case match == errorType:
return isError(value)
case isList(match) || complexTypeEq(listType, match):
return matchList(match, value)
case complexTypeEq(unionType, match):
return matchUnion(match, value)
case complexTypeEq(intersectType, match):
return matchIntersection(match, value)
case isStruct(match):
return matchStruct(match, value)
default:
return false
}
}
export fn (
rangeMin(match, min) range(match, min, isInt(min) ? ints.max : float.max)
listLength(l) range(listOf(any), l, l)
not(m) predicate(fn (v) !is(m, v))
)
export let natural rangeMin(int, 0)
export fn is(match, ...values) len(values) == 0 ? functions.bind(is, match) : every(matchOne(match), values)