forked from kanaka/mal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstep3_env.wam
281 lines (230 loc) · 8.13 KB
/
step3_env.wam
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
(module $step3_env
(global $repl_env (mut i32) (i32.const 0))
;; READ
(func $READ (param $str i32) (result i32)
($read_str $str)
)
;; EVAL
(func $EVAL_AST (param $ast i32 $env i32) (result i32)
(LET $res 0 $val2 0 $val3 0 $type 0 $found 0
$ret 0 $empty 0 $current 0)
(if (global.get $error_type) (return 0))
(local.set $type ($TYPE $ast))
;;($PR_VALUE ">>> EVAL_AST ast: '%s'\n" $ast)
;;; switch(type)
(block $done
(block $default (block (block
(br_table 2 2 2 2 2 0 1 1 1 2 2 2 2 2 2 2 $type))
;; symbol
;; found/res returned as hi 32/lo 32 of i64
(local.set $res ($ENV_GET $env $ast))
(br $done))
;; list, vector, hashmap
;; MAP_LOOP_START
(local.set $res ($MAP_LOOP_START $type))
;; push MAP_LOOP stack
;;; empty = current = ret = res
(local.set $ret $res)
(local.set $current $res)
(local.set $empty $res)
(block $done
(loop $loop
;; check if we are done evaluating the source sequence
(br_if $done (i32.eq ($VAL0 $ast) 0))
(if (i32.eq $type (global.get $HASHMAP_T))
(then
(local.set $res ($EVAL ($MEM_VAL2_ptr $ast) $env)))
(else
(local.set $res ($EVAL ($MEM_VAL1_ptr $ast) $env))))
(local.set $val2 $res)
;; if error, release the unattached element
(if (global.get $error_type)
(then
($RELEASE $res)
(local.set $res 0)
(br $done)))
;; for hash-maps, copy the key (inc ref since we are going
;; to release it below)
(if (i32.eq $type (global.get $HASHMAP_T))
(then
(local.set $val3 $val2)
(local.set $val2 ($MEM_VAL1_ptr $ast))
(drop ($INC_REF $val2))))
;; MAP_LOOP_UPDATE
(local.set $res ($MAP_LOOP_UPDATE $type $empty $current $val2 $val3))
(if (i32.le_u $current (global.get $EMPTY_HASHMAP))
;; if first element, set return to new element
(local.set $ret $res))
;; update current to point to new element
(local.set $current $res)
(local.set $ast ($MEM_VAL0_ptr $ast))
(br $loop)
)
)
;; MAP_LOOP_DONE
(local.set $res $ret)
;; EVAL_AST_RETURN: nothing to do
(br $done))
;; default
(local.set $res ($INC_REF $ast))
)
$res
)
(type $fnT (func (param i32) (result i32)))
(table funcref
(elem
$add $subtract $multiply $divide))
(func $MAL_GET_A1 (param $ast i32) (result i32)
($MEM_VAL1_ptr ($MEM_VAL0_ptr $ast)))
(func $MAL_GET_A2 (param $ast i32) (result i32)
($MEM_VAL1_ptr ($MEM_VAL0_ptr ($MEM_VAL0_ptr $ast))))
(func $MAL_GET_A3 (param $ast i32) (result i32)
($MEM_VAL1_ptr ($MEM_VAL0_ptr ($MEM_VAL0_ptr ($MEM_VAL0_ptr $ast)))))
(func $EVAL (param $ast i32 $env i32) (result i32)
(LET $res 0
$ftype 0 $f_args 0 $f 0 $args 0
$a0 0 $a0sym 0 $a1 0 $a2 0
$let_env 0)
(local.set $f_args 0)
(local.set $f 0)
(local.set $args 0)
(if (global.get $error_type) (return 0))
;;($PR_VALUE ">>> EVAL ast: '%s'\n" $ast)
(if (i32.ne ($TYPE $ast) (global.get $LIST_T))
(return ($EVAL_AST $ast $env)))
;; APPLY_LIST
(if ($EMPTY_Q $ast)
(return ($INC_REF $ast)))
(local.set $a0 ($MEM_VAL1_ptr $ast))
(local.set $a0sym "")
(if (i32.eq ($TYPE $a0) (global.get $SYMBOL_T))
(local.set $a0sym ($to_String $a0)))
(if (i32.eqz ($strcmp "def!" $a0sym))
(then
(local.set $a1 ($MAL_GET_A1 $ast))
(local.set $a2 ($MAL_GET_A2 $ast))
(local.set $res ($EVAL $a2 $env))
(if (global.get $error_type) (return $res))
;; set a1 in env to a2
(local.set $res ($ENV_SET $env $a1 $res)))
(else (if (i32.eqz ($strcmp "let*" $a0sym))
(then
(local.set $a1 ($MAL_GET_A1 $ast))
(local.set $a2 ($MAL_GET_A2 $ast))
;; create new environment with outer as current environment
(local.set $let_env ($ENV_NEW $env))
(block $done
(loop $loop
(br_if $done (i32.eqz ($VAL0 $a1)))
;; eval current A1 odd element
(local.set $res ($EVAL ($MEM_VAL1_ptr ($MEM_VAL0_ptr $a1))
$let_env))
(br_if $done (global.get $error_type))
;; set key/value in the let environment
(local.set $res ($ENV_SET $let_env ($MEM_VAL1_ptr $a1) $res))
;; release our use, ENV_SET took ownership
($RELEASE $res)
;; skip to the next pair of a1 elements
(local.set $a1 ($MEM_VAL0_ptr ($MEM_VAL0_ptr $a1)))
(br $loop)
)
)
(local.set $res ($EVAL $a2 $let_env))
;; EVAL_RETURN
($RELEASE $let_env))
(else
;; EVAL_INVOKE
(local.set $res ($EVAL_AST $ast $env))
(local.set $f_args $res)
;; if error, return f/args for release by caller
(if (global.get $error_type)
(return $f_args))
(local.set $args ($MEM_VAL0_ptr $f_args)) ;; rest
(local.set $f ($MEM_VAL1_ptr $f_args)) ;; value
(local.set $ftype ($TYPE $f))
(if (i32.eq $ftype (global.get $FUNCTION_T))
(then
(local.set $res (call_indirect (type $fnT) $args ($VAL0 $f))))
(else
($THROW_STR_1 "apply of non-function type: %d\n" $ftype)
(local.set $res 0)))
($RELEASE $f_args)))))
$res
)
;; PRINT
(func $PRINT (param $ast i32) (result i32)
($pr_str $ast 1)
)
;; REPL
(func $REP (param $line i32 $env i32) (result i32)
(LET $mv1 0 $mv2 0 $ms 0)
(block $done
(local.set $mv1 ($READ $line))
(br_if $done (global.get $error_type))
(local.set $mv2 ($EVAL $mv1 $env))
(br_if $done (global.get $error_type))
;; ($PR_MEMORY -1 -1)
(local.set $ms ($PRINT $mv2))
)
;; release memory from MAL_READ and EVAL
($RELEASE $mv2)
($RELEASE $mv1)
$ms
)
(func $add (param $args i32) (result i32)
($INTEGER
(i32.add ($VAL0 ($MEM_VAL1_ptr $args))
($VAL0 ($MEM_VAL1_ptr ($MEM_VAL0_ptr $args))))))
(func $subtract (param $args i32) (result i32)
($INTEGER
(i32.sub ($VAL0 ($MEM_VAL1_ptr $args))
($VAL0 ($MEM_VAL1_ptr ($MEM_VAL0_ptr $args))))))
(func $multiply (param $args i32) (result i32)
($INTEGER
(i32.mul ($VAL0 ($MEM_VAL1_ptr $args))
($VAL0 ($MEM_VAL1_ptr ($MEM_VAL0_ptr $args))))))
(func $divide (param $args i32) (result i32)
($INTEGER
(i32.div_s ($VAL0 ($MEM_VAL1_ptr $args))
($VAL0 ($MEM_VAL1_ptr ($MEM_VAL0_ptr $args))))))
(func $pr_memory (param $args i32) (result i32)
($PR_MEMORY -1 -1)
($INC_REF (global.get $NIL)))
(func $main (param $argc i32 $argv i32) (result i32)
(LET $line (STATIC_ARRAY 201)
$res 0 $repl_env 0)
;; DEBUG
;; ($printf_1 "memoryBase: 0x%x\n" (global.get $memoryBase))
;; ($printf_1 "heap_start: 0x%x\n" (global.get $heap_start))
;; ($printf_1 "heap_end: 0x%x\n" (global.get $heap_end))
;; ($printf_1 "mem: 0x%x\n" (global.get $mem))
;; ($printf_1 "string_mem: %d\n" (global.get $string_mem))
(global.set $repl_env ($ENV_NEW (global.get $NIL)))
(local.set $repl_env (global.get $repl_env))
(drop ($ENV_SET_S $repl_env "+" ($FUNCTION 0)))
(drop ($ENV_SET_S $repl_env "-" ($FUNCTION 1)))
(drop ($ENV_SET_S $repl_env "*" ($FUNCTION 2)))
(drop ($ENV_SET_S $repl_env "/" ($FUNCTION 3)))
;;($PR_MEMORY -1 -1)
;; Start REPL
(block $repl_done
(loop $repl_loop
(br_if $repl_done (i32.eqz ($readline "user> " $line)))
(br_if $repl_loop (i32.eq (i32.load8_u $line) 0))
(local.set $res ($REP $line $repl_env))
(if (global.get $error_type)
(then
($printf_1 "Error: %s\n" (global.get $error_str))
(global.set $error_type 0))
(else
($printf_1 "%s\n" ($to_String $res))))
($RELEASE $res)
;;($PR_MEMORY_SUMMARY_SMALL)
(br $repl_loop)
)
)
($print "\n")
;;($PR_MEMORY -1 -1)
0
)
)