forked from Matway/mpl-sl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Variant.mpl
executable file
·160 lines (141 loc) · 2.65 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
"Variant" module
"control" includeModule
Variant: [{
virtual VARIANT: ();
schema typeList:;
virtual maxSize: [
result: 1 static;
i: 0 static;
[
i typeList fieldCount < [
curSize: i typeList @ storageSize 0ix cast 0 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 0ix cast 0 cast;
curSize result > [
curSize @result set
] when
i 1 + @i set
TRUE static
] [
FALSE static
] if
] loop
result
] call;
typeTag: 0 dynamic;
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 copy];
setTag: [
index: copy dynamic;
index typeTag = not [
rawDestroy
index @typeTag set
rawInit
] when
];
get: [
index: copy;
[index typeTag =] "Wrong tag in Tagged Union!" assert
@memory storageAddress index @typeList @ addressToReference
];
INIT: [
0 @typeTag set
rawInit
];
ASSIGN: [
other:;
other.typeTag setTag
i: 0 static;
[
i typeList fieldCount < [
i typeTag = [
i other.get
i get 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;