3 (global $repl_env (mut i32) (i32.const 0))
6 (func $READ (param $str i32) (result i32)
13 (func $QUASIQUOTE (param $ast i32) (result i32)
14 (LET $type ($TYPE $ast) $res 0 $sym 0 $second 0)
16 ;; symbol or map -> ('quote ast)
17 (if (OR (i32.eq $type (global.get $SYMBOL_T))
18 (i32.eq $type (global.get $HASHMAP_T)))
20 (local.set $sym ($STRING (global.get $SYMBOL_T) "quote"))
21 (local.set $res ($LIST2 $sym $ast))
25 ;; [xs..] -> ('vec (processed like a list))
26 (if (i32.eq $type (global.get $VECTOR_T)) (then
27 (local.set $sym ($STRING (global.get $SYMBOL_T) "vec"))
28 (local.set $second ($qq_foldr $ast))
29 (local.set $res ($LIST2 $sym $second))
34 ;; If ast is not affected by eval, return it unchanged.
35 (if (i32.ne $type (global.get $LIST_T)) (then
36 (return ($INC_REF $ast))))
39 (local.set $second ($qq_unquote $ast "unquote"))
41 (return ($INC_REF $second))))
43 ;; ast is a normal list, iterate on its elements
44 (return ($qq_foldr $ast)))
46 ;; Helper for quasiquote.
47 ;; If the given list ast contains at least two elements and starts
48 ;; with the given symbol, return the second element. Else return 0.
49 (func $qq_unquote (param $ast i32) (param $sym i32) (result i32)
51 (if ($VAL0 $ast) (then
52 (local.set $car ($MEM_VAL1_ptr $ast))
53 (if (i32.eq ($TYPE $car) (global.get $SYMBOL_T)) (then
54 (if (i32.eqz ($strcmp ($to_String $car) $sym)) (then
55 (local.set $cdr ($MEM_VAL0_ptr $ast))
56 (if ($VAL0 $cdr) (then
57 (return ($MEM_VAL1_ptr $cdr))))))))))
60 ;; Iteration on sequences for quasiquote (right reduce/fold).
61 (func $qq_foldr (param $xs i32) (result i32)
63 (return ($qq_loop ($MEM_VAL1_ptr $xs) ($qq_foldr ($MEM_VAL0_ptr $xs)))))
65 (return ($INC_REF (global.get $EMPTY_LIST))))))
67 ;; Transition function for quasiquote right fold/reduce.
68 (func $qq_loop (param $elt i32) (param $acc i32) (result i32)
69 (LET $sym 0 $second 0 $res 0)
71 ;; If elt is ('splice-unquote x) -> ('concat, x, acc)
72 (if (i32.eq ($TYPE $elt) (global.get $LIST_T)) (then
73 (local.set $second ($qq_unquote $elt "splice-unquote"))
75 (local.set $sym ($STRING (global.get $SYMBOL_T) "concat"))
76 (local.set $res ($LIST3 $sym $second $acc))
77 ;; release inner quasiquoted since outer list takes ownership
81 ;; normal elt -> ('cons, (quasiquoted x), acc)
82 (local.set $sym ($STRING (global.get $SYMBOL_T) "cons"))
83 (local.set $second ($QUASIQUOTE $elt))
84 (local.set $res ($LIST3 $sym $second $acc))
85 ;; release inner quasiquoted since outer list takes ownership
91 (global $mac_stack (mut i32) (i32.const 0))
92 (global $mac_stack_top (mut i32) (i32.const -1))
94 (func $MACROEXPAND (param $orig_ast i32 $env i32) (result i32)
98 (global.set $mac_stack (STATIC_ARRAY 2048)) ;; 512 * 4, TODO: move to init
102 (OR (i32.ne ($TYPE $ast) (global.get $LIST_T)) ;; a list
103 (i32.eqz ($VAL0 $ast)) ;; non-empty
104 (i32.ne ($TYPE ($MEM_VAL1_ptr $ast)) ;; leading symbol
105 (global.get $SYMBOL_T))))
106 (local.set $mac_env ($ENV_FIND $env ($MEM_VAL1_ptr $ast)))
107 (local.set $mac (i32.wrap_i64 (i64.shr_u $mac_env (i64.const 32))))
108 (br_if $done (OR (i32.eqz (i32.wrap_i64 $mac_env)) ;; defined in env
109 (i32.ne ($TYPE $mac) ;; a macro
110 (global.get $MACRO_T))))
112 (local.set $ast ($APPLY $mac ($MEM_VAL0_ptr $ast)))
114 ;; if ast is not the first ast that was passed in, then add it
115 ;; to the pending release list.
116 (if (i32.ne $ast $orig_ast)
118 (global.set $mac_stack_top
119 (i32.add (global.get $mac_stack_top) 1))
120 (if (i32.ge_s (i32.mul (global.get $mac_stack_top) 4) 2048) ;; 512 * 4
121 ($fatal 7 "Exhausted mac_stack!\n"))
123 (global.get $mac_stack)
124 (i32.mul (global.get $mac_stack_top) 4))
126 (br_if $done (global.get $error_type))
134 (func $EVAL_AST (param $ast i32 $env i32 $skiplast i32) (result i32)
135 (LET $res 0 $val2 0 $val3 0 $type 0 $found 0
136 $ret 0 $empty 0 $current 0)
138 (if (global.get $error_type) (return 0))
139 (local.set $type ($TYPE $ast))
141 ;;($PR_VALUE ">>> EVAL_AST ast: '%s'\n" $ast)
145 (block $default (block (block
146 (br_table 2 2 2 2 2 0 1 1 1 2 2 2 2 2 2 2 $type))
148 ;; found/res returned as hi 32/lo 32 of i64
149 (local.set $res ($ENV_GET $env $ast))
151 ;; list, vector, hashmap
153 (local.set $res ($MAP_LOOP_START $type))
154 ;; push MAP_LOOP stack
155 ;;; empty = current = ret = res
156 (local.set $ret $res)
157 (local.set $current $res)
158 (local.set $empty $res)
162 ;; check if we are done evaluating the source sequence
163 (br_if $done (i32.eq ($VAL0 $ast) 0))
166 (br_if $done (i32.eqz ($VAL0 ($MEM_VAL0_ptr $ast)))))
168 (if (i32.eq $type (global.get $HASHMAP_T))
170 (local.set $res ($EVAL ($MEM_VAL2_ptr $ast) $env)))
172 (local.set $res ($EVAL ($MEM_VAL1_ptr $ast) $env))))
173 (local.set $val2 $res)
175 ;; if error, release the unattached element
176 (if (global.get $error_type)
182 ;; for hash-maps, copy the key (inc ref since we are going
183 ;; to release it below)
184 (if (i32.eq $type (global.get $HASHMAP_T))
186 (local.set $val3 $val2)
187 (local.set $val2 ($MEM_VAL1_ptr $ast))
188 (drop ($INC_REF $val2))))
191 (local.set $res ($MAP_LOOP_UPDATE $type $empty $current $val2 $val3))
192 (if (i32.le_u $current (global.get $EMPTY_HASHMAP))
193 ;; if first element, set return to new element
194 (local.set $ret $res))
195 ;; update current to point to new element
196 (local.set $current $res)
198 (local.set $ast ($MEM_VAL0_ptr $ast))
204 (local.set $res $ret)
205 ;; EVAL_AST_RETURN: nothing to do
208 (local.set $res ($INC_REF $ast))
214 (func $MAL_GET_A1 (param $ast i32) (result i32)
215 ($MEM_VAL1_ptr ($MEM_VAL0_ptr $ast)))
216 (func $MAL_GET_A2 (param $ast i32) (result i32)
217 ($MEM_VAL1_ptr ($MEM_VAL0_ptr ($MEM_VAL0_ptr $ast))))
218 (func $MAL_GET_A3 (param $ast i32) (result i32)
219 ($MEM_VAL1_ptr ($MEM_VAL0_ptr ($MEM_VAL0_ptr ($MEM_VAL0_ptr $ast)))))
221 (func $EVAL (param $orig_ast i32 $orig_env i32) (result i32)
224 $orig_mac_stack_top (global.get $mac_stack_top)
225 $prev_ast 0 $prev_env 0 $res 0 $el 0
226 $ftype 0 $f_args 0 $f 0 $args 0
227 $a0 0 $a0sym 0 $a1 0 $a2 0
233 (local.set $f_args 0)
237 (if (global.get $error_type)
242 ;;($PR_VALUE ">>> EVAL ast: '%s'\n" $ast)
244 (if (i32.ne ($TYPE $ast) (global.get $LIST_T))
246 (local.set $res ($EVAL_AST $ast $env 0))
250 (local.set $ast ($MACROEXPAND $ast $env))
251 ;;($PR_VALUE ">>> >>> EVAL ast: '%s'\n" $ast)
253 (if (i32.ne ($TYPE $ast) (global.get $LIST_T))
255 (local.set $res ($EVAL_AST $ast $env 0))
260 (local.set $res ($INC_REF $ast))
263 (local.set $a0 ($MEM_VAL1_ptr $ast))
264 (local.set $a0sym "")
265 (if (i32.eq ($TYPE $a0) (global.get $SYMBOL_T))
266 (local.set $a0sym ($to_String $a0)))
268 (if (i32.eqz ($strcmp "def!" $a0sym))
270 (local.set $a1 ($MAL_GET_A1 $ast))
271 (local.set $a2 ($MAL_GET_A2 $ast))
272 (local.set $res ($EVAL $a2 $env))
273 (br_if $EVAL_return (global.get $error_type))
275 ;; set a1 in env to a2
276 (local.set $res ($ENV_SET $env $a1 $res))
278 (else (if (i32.eqz ($strcmp "let*" $a0sym))
280 (local.set $a1 ($MAL_GET_A1 $ast))
281 (local.set $a2 ($MAL_GET_A2 $ast))
283 ;; create new environment with outer as current environment
284 (local.set $prev_env $env) ;; save env for later release
285 (local.set $env ($ENV_NEW $env))
289 (br_if $done (i32.eqz ($VAL0 $a1)))
290 ;; eval current A1 odd element
291 (local.set $res ($EVAL ($MEM_VAL1_ptr ($MEM_VAL0_ptr $a1)) $env))
293 (br_if $done (global.get $error_type))
295 ;; set key/value in the let environment
296 (local.set $res ($ENV_SET $env ($MEM_VAL1_ptr $a1) $res))
297 ;; release our use, ENV_SET took ownership
300 ;; skip to the next pair of a1 elements
301 (local.set $a1 ($MEM_VAL0_ptr ($MEM_VAL0_ptr $a1)))
306 ;; release previous environment if not the current EVAL env
307 (if (i32.ne $prev_env $orig_env)
310 (local.set $prev_env 0)))
314 (else (if (i32.eqz ($strcmp "do" $a0sym))
316 ;; EVAL the rest through second to last
317 (local.set $el ($EVAL_AST ($MEM_VAL0_ptr $ast) $env 1))
318 (local.set $ast ($LAST $ast))
319 ($RELEASE $ast) ;; we already own it via ast
322 (else (if (i32.eqz ($strcmp "quote" $a0sym))
324 (local.set $res ($INC_REF ($MEM_VAL1_ptr ($MEM_VAL0_ptr $ast))))
326 (else (if (i32.eqz ($strcmp "quasiquoteexpand" $a0sym))
328 (local.set $res ($QUASIQUOTE ($MEM_VAL1_ptr ($MEM_VAL0_ptr $ast))))
330 (else (if (i32.eqz ($strcmp "quasiquote" $a0sym))
332 (local.set $ast ($QUASIQUOTE ($MEM_VAL1_ptr ($MEM_VAL0_ptr $ast))))
334 ;; if we have already been here via TCO, release previous ast
335 (if $prev_ast ($RELEASE $prev_ast))
336 (local.set $prev_ast $ast)
338 (else (if (i32.eqz ($strcmp "defmacro!" $a0sym))
340 (local.set $a1 ($MAL_GET_A1 $ast))
341 (local.set $a2 ($MAL_GET_A2 $ast))
342 (local.set $res ($EVAL $a2 $env))
343 ($SET_TYPE $res (global.get $MACRO_T))
344 (br_if $EVAL_return (global.get $error_type))
346 ;; set a1 in env to a2
347 (local.set $res ($ENV_SET $env $a1 $res))
349 (else (if (i32.eqz ($strcmp "macroexpand" $a0sym))
351 ;; since we are returning it unevaluated, inc the ref cnt
352 (local.set $res ($INC_REF ($MACROEXPAND
353 ($MEM_VAL1_ptr ($MEM_VAL0_ptr $ast))
355 (else (if (i32.eqz ($strcmp "try*" $a0sym))
357 (local.set $a1 ($MAL_GET_A1 $ast))
358 (local.set $res ($EVAL $a1 $env))
360 ;; if there is no error, return
361 (br_if $EVAL_return (i32.eqz (global.get $error_type)))
362 ;; if there is an error and res is set, we need to free it
364 ;; if there is no catch block then return
366 (i32.eqz ($VAL0 ($MEM_VAL0_ptr ($MEM_VAL0_ptr $ast)))))
368 ;; save the current environment for release
369 (local.set $prev_env $env)
370 ;; create environment for the catch block eval
371 (local.set $env ($ENV_NEW $env))
373 ;; set a1 and a2 from the catch block
374 (local.set $a1 ($MAL_GET_A1 ($MAL_GET_A2 $ast)))
375 (local.set $a2 ($MAL_GET_A2 ($MAL_GET_A2 $ast)))
377 ;; create object for string errors
378 (if (i32.eq (global.get $error_type) 1)
380 (local.set $err ($STRING (global.get $STRING_T)
381 (global.get $error_str))))
383 (local.set $err (global.get $error_val))))
384 ;; bind the catch symbol to the error object
385 (drop ($ENV_SET $env $a1 $err))
386 ;; release our use, env took ownership
389 ;; unset error for catch eval
390 (global.set $error_type 0)
391 (i32.store (global.get $error_str) (CHR "\x00"))
393 ;; release previous environment if not the current EVAL env
394 (if (i32.ne $prev_env $orig_env)
397 (local.set $prev_env 0)))
401 (else (if (i32.eqz ($strcmp "if" $a0sym))
403 (local.set $a1 ($MAL_GET_A1 $ast))
404 (local.set $res ($EVAL $a1 $env))
406 (if (global.get $error_type)
408 (else (if (OR (i32.eq $res (global.get $NIL))
409 (i32.eq $res (global.get $FALSE)))
412 ;; if no false case (A3), return nil
413 (if (i32.lt_u ($COUNT $ast) 4)
415 (local.set $res ($INC_REF (global.get $NIL)))
418 (local.set $ast ($MAL_GET_A3 $ast)))))
421 (local.set $ast ($MAL_GET_A2 $ast))))))
423 (else (if (i32.eqz ($strcmp "fn*" $a0sym))
425 (local.set $a1 ($MAL_GET_A1 $ast))
426 (local.set $a2 ($MAL_GET_A2 $ast))
427 (local.set $res ($MALFUNC $a2 $a1 $env))
431 (local.set $res ($EVAL_AST $ast $env 0))
432 (local.set $f_args $res)
434 ;; if error, return f/args for release by caller
435 (if (global.get $error_type)
437 (local.set $res $f_args)
440 (local.set $args ($MEM_VAL0_ptr $f_args)) ;; rest
441 (local.set $f ($DEREF_META ($MEM_VAL1_ptr $f_args))) ;; value
443 (local.set $ftype ($TYPE $f))
444 (if (i32.eq $ftype (global.get $FUNCTION_T))
446 (if (i32.eq ($VAL0 $f) 0) ;; eval
448 (local.set $res ($EVAL ($MEM_VAL1_ptr $args)
449 (global.get $repl_env))))
451 (local.set $res (call_indirect (type $fnT) $args ($VAL0 $f)))))
455 (else (if (i32.eq $ftype (global.get $MALFUNC_T))
457 ;; save the current environment for release
458 (local.set $prev_env $env)
459 ;; create new environment using env and params stored in function
460 (local.set $env ($ENV_NEW_BINDS ($MEM_VAL2_ptr $f)
461 ($MEM_VAL1_ptr $f) $args))
463 ;; release previous environment if not the current EVAL env
464 ;; because our new env refers to it and we no longer need to
465 ;; track it (since we are TCO recurring)
466 (if (i32.ne $prev_env $orig_env)
469 (local.set $prev_env 0)))
471 ;; claim the AST before releasing the list containing it
472 (local.set $ast ($MEM_VAL0_ptr $f))
473 (drop ($INC_REF $ast))
475 ;; if we have already been here via TCO, release previous
478 (if $prev_ast ($RELEASE $prev_ast))
479 (local.set $prev_ast $ast)
486 ($THROW_STR_1 "apply of non-function type: %d\n" $ftype)
489 (br $EVAL_return)))))))))))))))))))))))))))
492 ) ;; end of EVAL_return
495 (if (i32.ne $env $orig_env) ($RELEASE $env))
496 (if $prev_ast ($RELEASE $prev_ast))
498 ;; release memory from MACROEXPAND
499 ;; TODO: needs to happen here so self-hosting doesn't leak
502 (br_if $done (i32.le_s (global.get $mac_stack_top) $orig_mac_stack_top))
503 ($RELEASE (i32.load (i32.add
504 (global.get $mac_stack)
505 (i32.mul (global.get $mac_stack_top) 4))))
506 (global.set $mac_stack_top
507 (i32.sub (global.get $mac_stack_top) 1))
516 (func $PRINT (param $ast i32) (result i32)
521 (func $RE (param $line i32 $env i32) (result i32)
524 (local.set $mv1 ($READ $line))
525 (br_if $done (global.get $error_type))
527 (local.set $res ($EVAL $mv1 $env))
530 ;; release memory from MAL_READ
535 (func $REP (param $line i32 $env i32) (result i32)
538 (local.set $mv2 ($RE $line $env))
539 (br_if $done (global.get $error_type))
541 ;; ($PR_MEMORY -1 -1)
542 (local.set $ms ($PRINT $mv2))
545 ;; release memory from RE
550 (func $main (param $argc i32 $argv i32) (result i32)
551 (LET $line (STATIC_ARRAY 201)
552 $res 0 $repl_env 0 $ms 0
553 ;; argument processing
554 $i 0 $ret 0 $empty 0 $current 0 $val2 0)
557 ;; ($printf_1 "argc: 0x%x\n" $argc)
558 ;; ($printf_1 "memoryBase: 0x%x\n" (global.get $memoryBase))
559 ;; ($printf_1 "heap_start: 0x%x\n" (global.get $heap_start))
560 ;; ($printf_1 "heap_end: 0x%x\n" (global.get $heap_end))
561 ;; ($printf_1 "mem: 0x%x\n" (global.get $mem))
562 ;; ($printf_1 "string_mem: %d\n" (global.get $string_mem))
564 (global.set $repl_env ($ENV_NEW (global.get $NIL)))
565 (local.set $repl_env (global.get $repl_env))
567 ;; core.EXT: defined in wasm
568 ($add_core_ns $repl_env)
569 (drop ($ENV_SET_S $repl_env "eval" ($FUNCTION 0)))
571 ($checkpoint_user_memory)
573 ;; core.mal: defined using the language itself
574 ($RELEASE ($RE "(def! *host-language* \"WebAssembly\")" $repl_env))
575 ($RELEASE ($RE "(def! not (fn* (a) (if a false true)))" $repl_env))
576 ($RELEASE ($RE "(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \"\nnil)\")))))" $repl_env))
577 ($RELEASE ($RE "(defmacro! cond (fn* (& xs) (if (> (count xs) 0) (list 'if (first xs) (if (> (count xs) 1) (nth xs 1) (throw \"odd number of forms to cond\")) (cons 'cond (rest (rest xs)))))))" $repl_env))
579 ;; Command line arguments
580 (local.set $res ($MAP_LOOP_START (global.get $LIST_T)))
581 ;; push MAP_LOP stack
582 ;; empty = current = ret = res
583 (local.set $ret $res)
584 (local.set $current $res)
585 (local.set $empty $res)
590 (br_if $done (i32.ge_u $i $argc))
592 (local.set $val2 ($STRING (global.get $STRING_T)
593 (i32.load (i32.add $argv (i32.mul $i 4)))))
596 (local.set $res ($MAP_LOOP_UPDATE
597 (global.get $LIST_T) $empty $current $val2 0))
598 (if (i32.le_u $current (global.get $EMPTY_HASHMAP))
599 ;; if first element, set return to new element
600 (local.set $ret $res))
601 ;; update current to point to new element
602 (local.set $current $res)
604 (local.set $i (i32.add $i 1))
608 (drop ($ENV_SET_S $repl_env "*ARGV*" $ret))
613 (if (i32.gt_u $argc 1)
615 (drop ($ENV_SET_S $repl_env
616 "*FILE*" ($STRING (global.get $STRING_T)
617 (i32.load (i32.add $argv 4)))))
618 ($RELEASE ($RE "(load-file *FILE*)" $repl_env))
619 (if (global.get $error_type)
621 ($printf_1 "Error: %s\n" (global.get $error_str))
626 ($RELEASE ($RE "(println (str \"Mal [\" *host-language* \"]\"))" $repl_env))
631 (br_if $repl_done (i32.eqz ($readline "user> " $line)))
632 (br_if $repl_loop (i32.eq (i32.load8_u $line) 0))
633 (local.set $res ($REP $line $repl_env))
634 (if (global.get $error_type)
636 (if (i32.eq 2 (global.get $error_type))
638 (local.set $ms ($pr_str (global.get $error_val) 1))
639 ($printf_1 "Error: %s\n" ($to_String $ms))
641 ($RELEASE (global.get $error_val)))
643 ($printf_1 "Error: %s\n" (global.get $error_str))))
644 (global.set $error_type 0))
646 ($printf_1 "%s\n" ($to_String $res))))
648 ;;($PR_MEMORY_SUMMARY_SMALL)