-
Notifications
You must be signed in to change notification settings - Fork 4
/
schemas.mpl
285 lines (246 loc) · 7.11 KB
/
schemas.mpl
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
# Copyright (C) 2021 Matway Burkow
#
# This repository and all its contents belong to Matway Burkow (referred here and below as "the owner").
# The content is for demonstration purposes only.
# It is forbidden to use the content or any part of it for any purpose without explicit permission from the owner.
# By contributing to the repository, contributors acknowledge that ownership of their work transfers to the owner.
"Array" use
"String" use
"Variant" use
"algorithm" use
"control" use
"Var" use
makeVariableSchema: [
refToVar: processor: ;;
var: refToVar getVar;
varSchema: VariableSchema;
var.data.getTag (
VarImport [
VariableSchemaTags.FUNCTION_SCHEMA @varSchema.@data.setTag
functionSchema: VariableSchemaTags.FUNCTION_SCHEMA @varSchema.@data.get;
functionId: VarImport var.data.get;
node: functionId processor.blocks.at.get;
signature: node.csignature;
signature.inputs.size @functionSchema.@inputSchemaIds.resize
signature.inputs.size [
i signature.inputs.at getVar.mplSchemaId i @functionSchema.@inputSchemaIds.at set
] times
signature.outputs.size @functionSchema.@outputSchemaIds.resize
signature.outputs.size [
i signature.outputs.at getVar.mplSchemaId i @functionSchema.@outputSchemaIds.at set
] times
signature.variadic new @functionSchema.!variadic
signature.convention new @functionSchema.!convention
]
VarRef [
VariableSchemaTags.REF_SCHEMA @varSchema.@data.setTag
refSchema: VariableSchemaTags.REF_SCHEMA @varSchema.@data.get;
ref: VarRef var.data.get.refToVar;
pointee: ref getVar;
ref.mutable @refSchema.!mutable
pointee.mplSchemaId new @refSchema.!pointeeSchemaId
]
VarStruct [
VariableSchemaTags.STRUCT_SCHEMA @varSchema.@data.setTag
structSchema: VariableSchemaTags.STRUCT_SCHEMA @varSchema.@data.get;
struct: VarStruct var.data.get.get;
struct.fields.size @structSchema.@data.resize
struct.fields.size [
field: i struct.fields.at;
fieldSchema: FieldSchema;
field.refToVar getVar.mplSchemaId @fieldSchema.@valueSchemaId set
field.nameInfo new @fieldSchema.!nameInfo
@fieldSchema i @structSchema.@data.at set
] times
]
[
VariableSchemaTags.BUILTIN_TYPE_SCHEMA @varSchema.@data.setTag
builtinTypeSchema: VariableSchemaTags.BUILTIN_TYPE_SCHEMA @varSchema.@data.get;
var.data.getTag @builtinTypeSchema.@tag set
]
) case
refToVar isVirtual [
schemaId: @varSchema @processor getVariableSchemaId;
VariableSchemaTags.VIRTUAL_VALUE_SCHEMA @varSchema.@data.setTag
virtualValueSchema: VariableSchemaTags.VIRTUAL_VALUE_SCHEMA @varSchema.@data.get;
schemaId new @virtualValueSchema.!schemaId
refToVar getVirtualValue @virtualValueSchema.!virtualValue
] when
@varSchema
];
getVariableSchemaId: [
processor:;
varSchema:;
findResult: varSchema processor.schemaTable.find;
findResult.success [
findResult.value new
] [
schemaId: processor.schemaBuffer.size;
varSchema schemaId @processor.@schemaTable.insert
@varSchema @processor.@schemaBuffer.append
schemaId new
] if
];
VariableSchema: [{
VARIABLE_SCHEMA: ();
irTypeId: -1;
dbgTypeId: -1;
dbgTypeDeclarationId: -1;
nilVar: RefToVar;
data: (
BuiltinTypeSchema
FunctionSchema
RefSchema
StructSchema
VirtualValueSchema
) Variant;
equal: [
other:;
data.getTag other.data.getTag = ~ [FALSE] [
data.getTag (
VariableSchemaTags fieldCount [
i new i new [
tag:;
tag data.get
tag other.data.get =
] bind
] times
[
"invalid tag in VariableSchema" failProc
FALSE
]
) case
] if
];
hash: [
variableSchema: data;
seed: 0n32;
@seed variableSchema.getTag hashCombine
variableSchema [value:; @seed value hashSchema] visit
@seed
];
}];
VariableSchemaTags: (
"BUILTIN_TYPE_SCHEMA"
"FUNCTION_SCHEMA"
"REF_SCHEMA"
"STRUCT_SCHEMA"
"VIRTUAL_VALUE_SCHEMA"
) Int32 enum;
BuiltinTypeSchema: [{
BUILTIN_TYPE_SCHEMA: ();
tag: Int32;
equal: [other:; tag other.tag =];
}];
RefSchema: [{
REF_SCHEMA: ();
pointeeSchemaId: Int32;
mutable: Cond;
equal: [other:; pointeeSchemaId other.pointeeSchemaId = [mutable other.mutable =] &&];
}];
FieldSchema: [{
FIELD_SCHEMA: ();
nameInfo: Int32;
valueSchemaId: Int32;
equal: [other:; nameInfo other.nameInfo = [valueSchemaId other.valueSchemaId =] &&];
}];
FunctionSchema: [{
FUNCTION_SCHEMA: ();
inputSchemaIds: Int32 Array;
outputSchemaIds: Int32 Array;
convention: String;
variadic: Cond;
equal: [
other:;
convention other.convention = [
variadic other.variadic = [
inputSchemaIds other.inputSchemaIds = [
outputSchemaIds other.outputSchemaIds =
] &&
] &&
] &&
];
}];
VirtualValueSchema: [{
VIRTUAL_VALUE_SCHEMA: ();
schemaId: Int32;
virtualValue: String;
equal: [other:; schemaId other.schemaId = [virtualValue other.virtualValue =] &&];
}];
StructSchema: [{
STRUCT_SCHEMA: ();
data: FieldSchema Array;
equal: [other:; data other.data =];
}];
twoWith: [
predicate:;
x:y:;;
@x predicate
@y predicate and
];
hashSchema: ["FIELD_SCHEMA" has] [
fieldSchema:;
seed:;
@seed fieldSchema.nameInfo hashCombine
@seed fieldSchema.valueSchemaId hashCombine
] pfunc;
hashSchema: ["REF_SCHEMA" has] [
refSchema:;
seed:;
@seed refSchema.pointeeSchemaId hashCombine
@seed refSchema.mutable hashCombine
] pfunc;
hashSchema: ["FUNCTION_SCHEMA" has] [
functionSchema:;
seed:;
functionSchema.inputSchemaIds [
value:;
@seed value hashCombine
] each
functionSchema.outputSchemaIds [
value:;
@seed value hashCombine
] each
@seed functionSchema.convention.hash hashCombine
@seed functionSchema.variadic hashCombine
] pfunc;
hashSchema: ["VIRTUAL_VALUE_SCHEMA" has] [
virtualValueSchema:;
seed:;
@seed virtualValueSchema.schemaId hashCombine
@seed virtualValueSchema.virtualValue.hash hashCombine
] pfunc;
hashSchema: ["STRUCT_SCHEMA" has] [
structSchema: .data;
seed:;
structSchema [
value:;
@seed value hashSchema
] each
] pfunc;
hashSchema: ["BUILTIN_TYPE_SCHEMA" has] [.tag Nat32 cast hashCombine] pfunc;
visit: [
variant:callback:;;
variant.getTag (
variant.typeList fieldCount [
i new i new [@variant.get callback] bind
] times
["invalid tag in variant" failProc]
) case
];
hashCombine: [
seed:value:;;
value hashValue 0x9e3779b9n32 + seed 6n32 lshift + seed 2n32 rshift + @seed set
];
hashValue: [Int8 same] [Nat32 cast] pfunc;
hashValue: [Int16 same] [Nat32 cast] pfunc;
hashValue: [Int32 same] [Nat32 cast] pfunc;
hashValue: [Int64 same] [Nat64 cast Nat32 cast] pfunc;
hashValue: [Intx same] [Nat64 cast Nat32 cast] pfunc;
hashValue: [Nat8 same] [Nat32 cast] pfunc;
hashValue: [Nat16 same] [Nat32 cast] pfunc;
hashValue: [Nat32 same] [Nat32 cast] pfunc;
hashValue: [Nat32 same] [Nat32 cast] pfunc;
hashValue: [Nat64 same] [Nat32 cast] pfunc;
hashValue: [Natx same] [Nat32 cast] pfunc;
hashValue: [Cond same] [[1n32] [0n32] if] pfunc;