-
Notifications
You must be signed in to change notification settings - Fork 14
/
Variant.mpl
210 lines (185 loc) · 4.1 KB
/
Variant.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
# Copyright (C) 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.
"algorithm.=" use
"control.=" use
"control.Int32" use
"control.Ref" use
"control.assert" use
"control.pfunc" use
"control.swap" use
"control.when" use
Variant: [{
virtual VARIANT: ();
virtual typeList: Ref;
virtual maxSize: [
result: 1 static;
i: 0 static;
[
i typeList fieldCount < [
curSize: i @typeList @ storageSize Int32 cast;
curSize result > [
curSize @result set
] when
i 1 + @i set
TRUE static
] [
FALSE static
] if
] loop
result
] call;
virtual maxAlignment: [
result: 1 static;
i: 0 static;
[
i @typeList fieldCount < [
curSize: i @typeList @ alignment Int32 cast;
curSize result > [
curSize @result set
] when
i 1 + @i set
TRUE static
] [
FALSE static
] if
] loop
result
] call;
typeTag: 0;
memory: [
maxAlignment 1 static = [0n8 dynamic] [
maxAlignment 2 static = [0n16 dynamic] [
maxAlignment 4 static = [0n32 dynamic] [
maxAlignment 8 static = [0n64 dynamic] [
().ALIGNMENT_HAS_INCORRECT_VALUE
] if
] if
] if
] if
] call;
filler: 0n8 maxSize maxAlignment - array dynamic;
rawInit: [
i: 0 static;
fc: typeList fieldCount;
[
i fc < [
i typeTag = [
i get manuallyInitVariable
] when
i 1 + @i set
TRUE static
] [
FALSE static
] if
] loop
];
rawDestroy: [
i: 0 static;
[
i typeList fieldCount < [
i typeTag = [
i get manuallyDestroyVariable
] when
i 1 + @i set
TRUE static
] [
FALSE static
] if
] loop
];
getTag: [typeTag new];
setTag: [
index: new;
index typeTag = ~ [
rawDestroy
index @typeTag set
rawInit
] when
];
getUnchecked: [memory storageAddress swap @typeList @ addressToReference];
get: [
index: new;
[index typeTag =] "Wrong tag in Tagged Union!" assert
index getUnchecked
];
assign: [
index:;
index setTag
index get set
];
visitInternal: [
visitBranches: visitIndex:;;
visitIndex visitBranches fieldCount = [] [
visitIndex visitBranches fieldCount 1 - = [visitIndex visitBranches @ call] [
visitIndex visitBranches @ typeTag = [visitIndex visitBranches @ get visitIndex 1 + visitBranches @ call] [
visitBranches visitIndex 2 + visitInternal
] if
] uif
] uif
];
visit: [0 visitInternal];
# Loop is kept to allow equality check of dynamic Variants
equal: [
other:;
typeList other.typeList same ~ ["Variants' supported types differ" raiseStaticError] when
result: FALSE;
typeTag other.typeTag = [
i: 0 static;
[
i typeTag = [i getUnchecked i other.getUnchecked = !result] when
i 1 + !i
i typeList fieldCount <
] loop
] when
result
];
INIT: [
0 @typeTag set
rawInit
];
ASSIGN: [
other:;
other.typeTag setTag
i: 0 static;
[
i @typeList fieldCount < [
i typeTag = [
i other.getUnchecked
i getUnchecked set
] when
i 1 + @i set
TRUE static
] [
FALSE static
] if
] loop
];
DIE: [
rawDestroy
];
}
result:;
@result manuallyInitVariable
@result
];
getHeapUsedSize: ["VARIANT" has] [
arg:;
i: 0 static;
result: 0nx;
[
i arg.@typeList fieldCount < [
i arg.typeTag = [
result i arg.get getHeapUsedSize + @result set
] when
i 1 + @i set
TRUE static
] [
FALSE static
] if
] loop
result
] pfunc;