forked from kanaka/mal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes.awk
218 lines (210 loc) · 4.5 KB
/
types.awk
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
# string"
# symbol '
# keyword :
# number +
# nil #
# true #
# false #
# list (
# vector [
# hash {
# atom ?
# builtin function &
# builtin function with meta %
# user defined function $
function types_allocate()
{
types_heap[types_heap_index]["ref"] = 1
return types_heap_index++
}
function types_addref(ast)
{
if (ast ~ /^[([{$%?]/) {
++types_heap[substr(ast, 2)]["ref"]
}
return ast
}
function types_release(ast, idx, ref, i, len)
{
switch (ast) {
case /^[([]/:
idx = substr(ast, 2)
ref = --types_heap[idx]["ref"]
if (ref <= 0) {
if (ref < 0) {
print "ref count error:" ast ", " ref
}
len = types_heap[idx]["len"]
for (i = 0; i < len; ++i) {
types_release(types_heap[idx][i])
}
types_release(types_heap[idx]["meta"])
delete types_heap[idx]
}
return
case /^\{/:
idx = substr(ast, 2)
ref = --types_heap[idx]["ref"]
if (ref <= 0) {
if (ref < 0) {
print "ref count error:" ast ", " ref
}
for (i in types_heap[idx]) {
if (i ~ /^[":]/) {
types_release(types_heap[idx][i])
}
}
types_release(types_heap[idx]["meta"])
delete types_heap[idx]
}
return
case /^\$/:
idx = substr(ast, 2)
ref = --types_heap[idx]["ref"]
if (ref <= 0) {
if (ref < 0) {
print "ref count error:" ast ", " ref
}
types_release(types_heap[idx]["params"])
types_release(types_heap[idx]["body"])
types_release(types_heap[idx]["meta"])
env_release(types_heap[idx]["env"])
delete types_heap[idx]
}
return
case /^%/:
idx = substr(ast, 2)
ref = --types_heap[idx]["ref"]
if (ref <= 0) {
if (ref < 0) {
print "ref count error:" ast ", " ref
}
types_release(types_heap[idx]["meta"])
delete types_heap[idx]
}
return
case /^\?/:
idx = substr(ast, 2)
ref = --types_heap[idx]["ref"]
if (ref <= 0) {
if (ref < 0) {
print "ref count error:" ast ", " ref
}
types_release(types_heap[idx]["obj"])
delete types_heap[idx]
}
}
}
function types_check(val, idx, len, i)
{
if (val !~ /^[([{?%$]/) {
return
}
idx = substr(val, 2)
if (!(idx in types_heap)) {
print "dangling reference " val
return
}
if (types_heap[idx]["checked"]++) {
return
}
#types_heap[idx]["checked"] = 1
switch (val) {
case /^[([]/:
if (!("len" in types_heap[idx])) {
print "length not found in " val
return
}
len = types_heap[idx]["len"]
for (i = 0; i < len; ++i) {
if (!(i in types_heap[idx])) {
print "sequence corrupted in " val " of " i
} else {
types_check(types_heap[idx][i])
}
}
types_check(types_heap[idx]["meta"])
return
case /^\{/:
for (i in types_heap[idx]) {
if (i != "ref") {
types_check(types_heap[idx][i])
}
}
return
case /^\?/:
if (!("obj" in types_heap[idx])) {
print "atom corrupted in " val
} else {
types_check(types_heap[idx]["obj"])
}
types_check(types_heap[idx]["meta"])
return
case /^%/:
if (!("func" in types_heap[idx])) {
print "function corrupted in " val
} else {
types_check(types_heap[idx]["func"])
}
types_check(types_heap[idx]["meta"])
return
case /^\$/:
if (!("body" in types_heap[idx])) {
print "function body corrupted in " val
} else {
types_check(types_heap[idx]["body"])
}
if (!("params" in types_heap[idx])) {
print "function params corrupted in " val
} else {
types_check(types_heap[idx]["params"])
}
if (!("env" in types_heap[idx])) {
print "function env corrupted in " val
} else {
env_check(types_heap[idx]["env"])
}
types_check(types_heap[idx]["meta"])
return
default:
print "unknown type " val
return
}
}
function types_dump(i, j)
{
for (i = 0; i < types_heap_index; i++) {
if (i in types_heap) {
if (isarray(types_heap[i])) {
if (!("checked" in types_heap[i]) || types_heap[i]["checked"] != types_heap[i]["ref"]) {
for (j in types_heap[i]) {
print " types_heap[" i "][" j "] = " types_heap[i][j]
}
}
} else {
print " types_heap[" i "] = " types_heap[i]
}
}
}
}
function types_typename(str)
{
switch (str) {
case /^"/: return "string"
case /^'/: return "symbol"
case /^:/: return "keyword"
case /^\+/: return "number"
case /^#nil$/: return "nil"
case /^#true$/: return "true"
case /^#false$/: return "false"
case /^\(/: return "list"
case /^\[/: return "vector"
case /^\{/: return "hash"
case /^\?/: return "atom"
case /^[&%]/: return "builtin function"
case /^\$/: return "user defined function"
}
}
BEGIN {
types_heap_index = 0
}