3 (global $repl_env (mut i32) (i32.const 0))
6 (func $READ (param $str i32) (result i32)
11 (func $is_pair (param $ast i32) (result i32)
12 (LET $type ($TYPE $ast))
13 (AND (OR (i32.eq $type (global.get $LIST_T))
14 (i32.eq $type (global.get $VECTOR_T)))
15 (i32.ne ($VAL0 $ast) 0))
18 (func $QUASIQUOTE (param $ast i32) (result i32)
19 (LET $res 0 $sym 0 $second 0 $third 0)
20 (if (i32.eqz ($is_pair $ast)) ;; QQ_QUOTE
22 (local.set $sym ($STRING (global.get $SYMBOL_T) "quote"))
24 (local.set $res ($LIST2 $sym $ast))
27 (local.set $res ($MEM_VAL1_ptr $ast))
28 (if (AND (i32.eq ($TYPE $res) (global.get $SYMBOL_T))
29 (i32.eqz ($strcmp "unquote" ($to_String $res))))
32 (local.set $res ($INC_REF ($MEM_VAL1_ptr ($MEM_VAL0_ptr $ast)))))
33 (else (if (AND ($is_pair $res)
34 (i32.eq ($TYPE ($MEM_VAL1_ptr $res))
35 (global.get $SYMBOL_T))
36 (i32.eqz ($strcmp "splice-unquote"
37 ($to_String ($MEM_VAL1_ptr $res)))))
39 ;; ['concat, ast[0][1], quasiquote(ast[1..])]
40 (local.set $sym ($STRING (global.get $SYMBOL_T) "concat"))
42 ($MEM_VAL1_ptr ($MEM_VAL0_ptr ($MEM_VAL1_ptr $ast))))
43 (local.set $third ($QUASIQUOTE ($MEM_VAL0_ptr $ast)))
44 (local.set $res ($LIST3 $sym $second $third))
45 ;; release inner quasiquoted since outer list take ownership
49 ;; ['cons, quasiquote(ast[0]), quasiquote(ast[1..])]
50 (local.set $sym ($STRING (global.get $SYMBOL_T) "cons"))
51 (local.set $second ($QUASIQUOTE ($MEM_VAL1_ptr $ast)))
52 (local.set $third ($QUASIQUOTE ($MEM_VAL0_ptr $ast)))
53 (local.set $res ($LIST3 $sym $second $third))
54 ;; release inner quasiquoted since outer list takes ownership
61 (func $EVAL_AST (param $ast i32 $env i32 $skiplast i32) (result i32)
62 (LET $res 0 $val2 0 $val3 0 $type 0 $found 0
63 $ret 0 $empty 0 $current 0)
65 (if (global.get $error_type) (return 0))
66 (local.set $type ($TYPE $ast))
68 ;;($PR_VALUE ">>> EVAL_AST ast: '%s'\n" $ast)
72 (block $default (block (block
73 (br_table 2 2 2 2 2 0 1 1 1 2 2 2 2 2 2 2 $type))
75 ;; found/res returned as hi 32/lo 32 of i64
76 (local.set $res ($ENV_GET $env $ast))
78 ;; list, vector, hashmap
80 (local.set $res ($MAP_LOOP_START $type))
81 ;; push MAP_LOOP stack
82 ;;; empty = current = ret = res
84 (local.set $current $res)
85 (local.set $empty $res)
89 ;; check if we are done evaluating the source sequence
90 (br_if $done (i32.eq ($VAL0 $ast) 0))
93 (br_if $done (i32.eqz ($VAL0 ($MEM_VAL0_ptr $ast)))))
95 (if (i32.eq $type (global.get $HASHMAP_T))
97 (local.set $res ($EVAL ($MEM_VAL2_ptr $ast) $env)))
99 (local.set $res ($EVAL ($MEM_VAL1_ptr $ast) $env))))
100 (local.set $val2 $res)
102 ;; if error, release the unattached element
103 (if (global.get $error_type)
109 ;; for hash-maps, copy the key (inc ref since we are going
110 ;; to release it below)
111 (if (i32.eq $type (global.get $HASHMAP_T))
113 (local.set $val3 $val2)
114 (local.set $val2 ($MEM_VAL1_ptr $ast))
115 (drop ($INC_REF $val2))))
118 (local.set $res ($MAP_LOOP_UPDATE $type $empty $current $val2 $val3))
119 (if (i32.le_u $current (global.get $EMPTY_HASHMAP))
120 ;; if first element, set return to new element
121 (local.set $ret $res))
122 ;; update current to point to new element
123 (local.set $current $res)
125 (local.set $ast ($MEM_VAL0_ptr $ast))
131 (local.set $res $ret)
132 ;; EVAL_AST_RETURN: nothing to do
135 (local.set $res ($INC_REF $ast))
141 (func $MAL_GET_A1 (param $ast i32) (result i32)
142 ($MEM_VAL1_ptr ($MEM_VAL0_ptr $ast)))
143 (func $MAL_GET_A2 (param $ast i32) (result i32)
144 ($MEM_VAL1_ptr ($MEM_VAL0_ptr ($MEM_VAL0_ptr $ast))))
145 (func $MAL_GET_A3 (param $ast i32) (result i32)
146 ($MEM_VAL1_ptr ($MEM_VAL0_ptr ($MEM_VAL0_ptr ($MEM_VAL0_ptr $ast)))))
148 (func $EVAL (param $orig_ast i32 $orig_env i32) (result i32)
151 $prev_ast 0 $prev_env 0 $res 0 $el 0
152 $ftype 0 $f_args 0 $f 0 $args 0
153 $a0 0 $a0sym 0 $a1 0 $a2 0)
158 (local.set $f_args 0)
162 (if (global.get $error_type)
167 ;;($PR_VALUE ">>> EVAL ast: '%s'\n" $ast)
169 (if (i32.ne ($TYPE $ast) (global.get $LIST_T))
171 (local.set $res ($EVAL_AST $ast $env 0))
177 (local.set $res ($INC_REF $ast))
180 (local.set $a0 ($MEM_VAL1_ptr $ast))
181 (local.set $a0sym "")
182 (if (i32.eq ($TYPE $a0) (global.get $SYMBOL_T))
183 (local.set $a0sym ($to_String $a0)))
185 (if (i32.eqz ($strcmp "def!" $a0sym))
187 (local.set $a1 ($MAL_GET_A1 $ast))
188 (local.set $a2 ($MAL_GET_A2 $ast))
189 (local.set $res ($EVAL $a2 $env))
190 (br_if $EVAL_return (global.get $error_type))
192 ;; set a1 in env to a2
193 (local.set $res ($ENV_SET $env $a1 $res))
195 (else (if (i32.eqz ($strcmp "let*" $a0sym))
197 (local.set $a1 ($MAL_GET_A1 $ast))
198 (local.set $a2 ($MAL_GET_A2 $ast))
200 ;; create new environment with outer as current environment
201 (local.set $prev_env $env) ;; save env for later release
202 (local.set $env ($ENV_NEW $env))
206 (br_if $done (i32.eqz ($VAL0 $a1)))
207 ;; eval current A1 odd element
208 (local.set $res ($EVAL ($MEM_VAL1_ptr ($MEM_VAL0_ptr $a1)) $env))
210 (br_if $done (global.get $error_type))
212 ;; set key/value in the let environment
213 (local.set $res ($ENV_SET $env ($MEM_VAL1_ptr $a1) $res))
214 ;; release our use, ENV_SET took ownership
217 ;; skip to the next pair of a1 elements
218 (local.set $a1 ($MEM_VAL0_ptr ($MEM_VAL0_ptr $a1)))
223 ;; release previous environment if not the current EVAL env
224 (if (i32.ne $prev_env $orig_env)
227 (local.set $prev_env 0)))
231 (else (if (i32.eqz ($strcmp "do" $a0sym))
233 ;; EVAL the rest through second to last
234 (local.set $el ($EVAL_AST ($MEM_VAL0_ptr $ast) $env 1))
235 (local.set $ast ($LAST $ast))
236 ($RELEASE $ast) ;; we already own it via ast
239 (else (if (i32.eqz ($strcmp "quote" $a0sym))
241 (local.set $res ($INC_REF ($MEM_VAL1_ptr ($MEM_VAL0_ptr $ast))))
243 (else (if (i32.eqz ($strcmp "quasiquote" $a0sym))
245 (local.set $ast ($QUASIQUOTE ($MEM_VAL1_ptr ($MEM_VAL0_ptr $ast))))
247 ;; if we have already been here via TCO, release previous ast
248 (if $prev_ast ($RELEASE $prev_ast))
249 (local.set $prev_ast $ast)
251 (else (if (i32.eqz ($strcmp "if" $a0sym))
253 (local.set $a1 ($MAL_GET_A1 $ast))
254 (local.set $res ($EVAL $a1 $env))
256 (if (global.get $error_type)
258 (else (if (OR (i32.eq $res (global.get $NIL))
259 (i32.eq $res (global.get $FALSE)))
262 ;; if no false case (A3), return nil
263 (if (i32.lt_u ($COUNT $ast) 4)
265 (local.set $res ($INC_REF (global.get $NIL)))
268 (local.set $ast ($MAL_GET_A3 $ast)))))
271 (local.set $ast ($MAL_GET_A2 $ast))))))
273 (else (if (i32.eqz ($strcmp "fn*" $a0sym))
275 (local.set $a1 ($MAL_GET_A1 $ast))
276 (local.set $a2 ($MAL_GET_A2 $ast))
277 (local.set $res ($MALFUNC $a2 $a1 $env))
281 (local.set $res ($EVAL_AST $ast $env 0))
282 (local.set $f_args $res)
284 ;; if error, return f/args for release by caller
285 (if (global.get $error_type)
287 (local.set $res $f_args)
290 (local.set $args ($MEM_VAL0_ptr $f_args)) ;; rest
291 (local.set $f ($MEM_VAL1_ptr $f_args)) ;; value
293 (local.set $ftype ($TYPE $f))
294 (if (i32.eq $ftype (global.get $FUNCTION_T))
296 (if (i32.eq ($VAL0 $f) 0) ;; eval
298 (local.set $res ($EVAL ($MEM_VAL1_ptr $args)
299 (global.get $repl_env))))
301 (local.set $res (call_indirect (type $fnT) $args ($VAL0 $f)))))
305 (else (if (i32.eq $ftype (global.get $MALFUNC_T))
307 ;; save the current environment for release
308 (local.set $prev_env $env)
309 ;; create new environment using env and params stored in function
310 (local.set $env ($ENV_NEW_BINDS ($MEM_VAL2_ptr $f)
311 ($MEM_VAL1_ptr $f) $args))
313 ;; release previous environment if not the current EVAL env
314 ;; because our new env refers to it and we no longer need to
315 ;; track it (since we are TCO recurring)
316 (if (i32.ne $prev_env $orig_env)
319 (local.set $prev_env 0)))
321 ;; claim the AST before releasing the list containing it
322 (local.set $ast ($MEM_VAL0_ptr $f))
323 (drop ($INC_REF $ast))
325 ;; if we have already been here via TCO, release previous
328 (if $prev_ast ($RELEASE $prev_ast))
329 (local.set $prev_ast $ast)
336 ($THROW_STR_1 "apply of non-function type: %d\n" $ftype)
339 (br $EVAL_return)))))))))))))))))))
342 ) ;; end of EVAL_return
345 (if (i32.ne $env $orig_env) ($RELEASE $env))
346 (if $prev_ast ($RELEASE $prev_ast))
352 (func $PRINT (param $ast i32) (result i32)
357 (func $RE (param $line i32 $env i32) (result i32)
360 (local.set $mv1 ($READ $line))
361 (br_if $done (global.get $error_type))
363 (local.set $res ($EVAL $mv1 $env))
366 ;; release memory from MAL_READ
371 (func $REP (param $line i32 $env i32) (result i32)
374 (local.set $mv2 ($RE $line $env))
375 (br_if $done (global.get $error_type))
377 ;; ($PR_MEMORY -1 -1)
378 (local.set $ms ($PRINT $mv2))
381 ;; release memory from RE
386 (func $main (param $argc i32 $argv i32) (result i32)
387 (LET $line (STATIC_ARRAY 201)
388 $res 0 $repl_env 0 $ms 0
389 ;; argument processing
390 $i 0 $ret 0 $empty 0 $current 0 $val2 0)
393 ;; ($printf_1 "argc: 0x%x\n" $argc)
394 ;; ($printf_1 "memoryBase: 0x%x\n" (global.get $memoryBase))
395 ;; ($printf_1 "heap_start: 0x%x\n" (global.get $heap_start))
396 ;; ($printf_1 "heap_end: 0x%x\n" (global.get $heap_end))
397 ;; ($printf_1 "mem: 0x%x\n" (global.get $mem))
398 ;; ($printf_1 "string_mem: %d\n" (global.get $string_mem))
400 (global.set $repl_env ($ENV_NEW (global.get $NIL)))
401 (local.set $repl_env (global.get $repl_env))
403 ;; core.EXT: defined in wasm
404 ($add_core_ns $repl_env)
405 (drop ($ENV_SET_S $repl_env "eval" ($FUNCTION 0)))
407 ($checkpoint_user_memory)
409 ;; core.mal: defined using the language itself
410 ($RELEASE ($RE "(def! not (fn* (a) (if a false true)))" $repl_env))
411 ($RELEASE ($RE "(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \"\nnil)\")))))" $repl_env))
414 ;; Command line arguments
415 (local.set $res ($MAP_LOOP_START (global.get $LIST_T)))
416 ;; push MAP_LOP stack
417 ;; empty = current = ret = res
418 (local.set $ret $res)
419 (local.set $current $res)
420 (local.set $empty $res)
425 (br_if $done (i32.ge_u $i $argc))
427 (local.set $val2 ($STRING (global.get $STRING_T)
428 (i32.load (i32.add $argv (i32.mul $i 4)))))
431 (local.set $res ($MAP_LOOP_UPDATE
432 (global.get $LIST_T) $empty $current $val2 0))
433 (if (i32.le_u $current (global.get $EMPTY_HASHMAP))
434 ;; if first element, set return to new element
435 (local.set $ret $res))
436 ;; update current to point to new element
437 (local.set $current $res)
439 (local.set $i (i32.add $i 1))
443 (drop ($ENV_SET_S $repl_env "*ARGV*" $ret))
448 (if (i32.gt_u $argc 1)
450 (drop ($ENV_SET_S $repl_env
451 "*FILE*" ($STRING (global.get $STRING_T)
452 (i32.load (i32.add $argv 4)))))
453 ($RELEASE ($RE "(load-file *FILE*)" $repl_env))
454 (if (global.get $error_type)
456 ($printf_1 "Error: %s\n" (global.get $error_str))
464 (br_if $repl_done (i32.eqz ($readline "user> " $line)))
465 (br_if $repl_loop (i32.eq (i32.load8_u $line) 0))
466 (local.set $res ($REP $line $repl_env))
467 (if (global.get $error_type)
469 (if (i32.eq 2 (global.get $error_type))
471 (local.set $ms ($pr_str (global.get $error_val) 1))
472 ($printf_1 "Error: %s\n" ($to_String $ms))
474 ($RELEASE (global.get $error_val)))
476 ($printf_1 "Error: %s\n" (global.get $error_str))))
477 (global.set $error_type 0))
479 ($printf_1 "%s\n" ($to_String $res))))
481 ;;($PR_MEMORY_SUMMARY_SMALL)