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 (global $mac_stack (mut i32) (i32.const 0))
62 (global $mac_stack_top (mut i32) (i32.const -1))
64 (func $MACROEXPAND (param $orig_ast i32 $env i32) (result i32)
68 (global.set $mac_stack (STATIC_ARRAY 2048)) ;; 512 * 4, TODO: move to init
72 (OR (i32.ne ($TYPE $ast) (global.get $LIST_T)) ;; a list
73 (i32.eqz ($VAL0 $ast)) ;; non-empty
74 (i32.ne ($TYPE ($MEM_VAL1_ptr $ast)) ;; leading symbol
75 (global.get $SYMBOL_T))))
76 (local.set $mac_env ($ENV_FIND $env ($MEM_VAL1_ptr $ast)))
77 (local.set $mac (i32.wrap_i64 (i64.shr_u $mac_env (i64.const 32))))
78 (br_if $done (OR (i32.eqz (i32.wrap_i64 $mac_env)) ;; defined in env
79 (i32.ne ($TYPE $mac) ;; a macro
80 (global.get $MACRO_T))))
82 (local.set $ast ($APPLY $mac ($MEM_VAL0_ptr $ast)))
84 ;; if ast is not the first ast that was passed in, then add it
85 ;; to the pending release list.
86 (if (i32.ne $ast $orig_ast)
88 (global.set $mac_stack_top
89 (i32.add (global.get $mac_stack_top) 1))
90 (if (i32.ge_s (i32.mul (global.get $mac_stack_top) 4) 2048) ;; 512 * 4
91 ($fatal 7 "Exhausted mac_stack!\n"))
93 (global.get $mac_stack)
94 (i32.mul (global.get $mac_stack_top) 4))
96 (br_if $done (global.get $error_type))
104 (func $EVAL_AST (param $ast i32 $env i32 $skiplast i32) (result i32)
105 (LET $res 0 $val2 0 $val3 0 $type 0 $found 0
106 $ret 0 $empty 0 $current 0)
108 (if (global.get $error_type) (return 0))
109 (local.set $type ($TYPE $ast))
111 ;;($PR_VALUE ">>> EVAL_AST ast: '%s'\n" $ast)
115 (block $default (block (block
116 (br_table 2 2 2 2 2 0 1 1 1 2 2 2 2 2 2 2 $type))
118 ;; found/res returned as hi 32/lo 32 of i64
119 (local.set $res ($ENV_GET $env $ast))
121 ;; list, vector, hashmap
123 (local.set $res ($MAP_LOOP_START $type))
124 ;; push MAP_LOOP stack
125 ;;; empty = current = ret = res
126 (local.set $ret $res)
127 (local.set $current $res)
128 (local.set $empty $res)
132 ;; check if we are done evaluating the source sequence
133 (br_if $done (i32.eq ($VAL0 $ast) 0))
136 (br_if $done (i32.eqz ($VAL0 ($MEM_VAL0_ptr $ast)))))
138 (if (i32.eq $type (global.get $HASHMAP_T))
140 (local.set $res ($EVAL ($MEM_VAL2_ptr $ast) $env)))
142 (local.set $res ($EVAL ($MEM_VAL1_ptr $ast) $env))))
143 (local.set $val2 $res)
145 ;; if error, release the unattached element
146 (if (global.get $error_type)
152 ;; for hash-maps, copy the key (inc ref since we are going
153 ;; to release it below)
154 (if (i32.eq $type (global.get $HASHMAP_T))
156 (local.set $val3 $val2)
157 (local.set $val2 ($MEM_VAL1_ptr $ast))
158 (drop ($INC_REF $val2))))
161 (local.set $res ($MAP_LOOP_UPDATE $type $empty $current $val2 $val3))
162 (if (i32.le_u $current (global.get $EMPTY_HASHMAP))
163 ;; if first element, set return to new element
164 (local.set $ret $res))
165 ;; update current to point to new element
166 (local.set $current $res)
168 (local.set $ast ($MEM_VAL0_ptr $ast))
174 (local.set $res $ret)
175 ;; EVAL_AST_RETURN: nothing to do
178 (local.set $res ($INC_REF $ast))
184 (func $MAL_GET_A1 (param $ast i32) (result i32)
185 ($MEM_VAL1_ptr ($MEM_VAL0_ptr $ast)))
186 (func $MAL_GET_A2 (param $ast i32) (result i32)
187 ($MEM_VAL1_ptr ($MEM_VAL0_ptr ($MEM_VAL0_ptr $ast))))
188 (func $MAL_GET_A3 (param $ast i32) (result i32)
189 ($MEM_VAL1_ptr ($MEM_VAL0_ptr ($MEM_VAL0_ptr ($MEM_VAL0_ptr $ast)))))
191 (func $EVAL (param $orig_ast i32 $orig_env i32) (result i32)
194 $orig_mac_stack_top (global.get $mac_stack_top)
195 $prev_ast 0 $prev_env 0 $res 0 $el 0
196 $ftype 0 $f_args 0 $f 0 $args 0
197 $a0 0 $a0sym 0 $a1 0 $a2 0
203 (local.set $f_args 0)
207 (if (global.get $error_type)
212 ;;($PR_VALUE ">>> EVAL ast: '%s'\n" $ast)
214 (if (i32.ne ($TYPE $ast) (global.get $LIST_T))
216 (local.set $res ($EVAL_AST $ast $env 0))
220 (local.set $ast ($MACROEXPAND $ast $env))
221 ;;($PR_VALUE ">>> >>> EVAL ast: '%s'\n" $ast)
223 (if (i32.ne ($TYPE $ast) (global.get $LIST_T))
225 (local.set $res ($EVAL_AST $ast $env 0))
230 (local.set $res ($INC_REF $ast))
233 (local.set $a0 ($MEM_VAL1_ptr $ast))
234 (local.set $a0sym "")
235 (if (i32.eq ($TYPE $a0) (global.get $SYMBOL_T))
236 (local.set $a0sym ($to_String $a0)))
238 (if (i32.eqz ($strcmp "def!" $a0sym))
240 (local.set $a1 ($MAL_GET_A1 $ast))
241 (local.set $a2 ($MAL_GET_A2 $ast))
242 (local.set $res ($EVAL $a2 $env))
243 (br_if $EVAL_return (global.get $error_type))
245 ;; set a1 in env to a2
246 (local.set $res ($ENV_SET $env $a1 $res))
248 (else (if (i32.eqz ($strcmp "let*" $a0sym))
250 (local.set $a1 ($MAL_GET_A1 $ast))
251 (local.set $a2 ($MAL_GET_A2 $ast))
253 ;; create new environment with outer as current environment
254 (local.set $prev_env $env) ;; save env for later release
255 (local.set $env ($ENV_NEW $env))
259 (br_if $done (i32.eqz ($VAL0 $a1)))
260 ;; eval current A1 odd element
261 (local.set $res ($EVAL ($MEM_VAL1_ptr ($MEM_VAL0_ptr $a1)) $env))
263 (br_if $done (global.get $error_type))
265 ;; set key/value in the let environment
266 (local.set $res ($ENV_SET $env ($MEM_VAL1_ptr $a1) $res))
267 ;; release our use, ENV_SET took ownership
270 ;; skip to the next pair of a1 elements
271 (local.set $a1 ($MEM_VAL0_ptr ($MEM_VAL0_ptr $a1)))
276 ;; release previous environment if not the current EVAL env
277 (if (i32.ne $prev_env $orig_env)
280 (local.set $prev_env 0)))
284 (else (if (i32.eqz ($strcmp "do" $a0sym))
286 ;; EVAL the rest through second to last
287 (local.set $el ($EVAL_AST ($MEM_VAL0_ptr $ast) $env 1))
288 (local.set $ast ($LAST $ast))
289 ($RELEASE $ast) ;; we already own it via ast
292 (else (if (i32.eqz ($strcmp "quote" $a0sym))
294 (local.set $res ($INC_REF ($MEM_VAL1_ptr ($MEM_VAL0_ptr $ast))))
296 (else (if (i32.eqz ($strcmp "quasiquote" $a0sym))
298 (local.set $ast ($QUASIQUOTE ($MEM_VAL1_ptr ($MEM_VAL0_ptr $ast))))
300 ;; if we have already been here via TCO, release previous ast
301 (if $prev_ast ($RELEASE $prev_ast))
302 (local.set $prev_ast $ast)
304 (else (if (i32.eqz ($strcmp "defmacro!" $a0sym))
306 (local.set $a1 ($MAL_GET_A1 $ast))
307 (local.set $a2 ($MAL_GET_A2 $ast))
308 (local.set $res ($EVAL $a2 $env))
309 ($SET_TYPE $res (global.get $MACRO_T))
310 (br_if $EVAL_return (global.get $error_type))
312 ;; set a1 in env to a2
313 (local.set $res ($ENV_SET $env $a1 $res))
315 (else (if (i32.eqz ($strcmp "macroexpand" $a0sym))
317 ;; since we are returning it unevaluated, inc the ref cnt
318 (local.set $res ($INC_REF ($MACROEXPAND
319 ($MEM_VAL1_ptr ($MEM_VAL0_ptr $ast))
321 (else (if (i32.eqz ($strcmp "try*" $a0sym))
323 (local.set $a1 ($MAL_GET_A1 $ast))
324 (local.set $res ($EVAL $a1 $env))
326 ;; if there is no error, return
327 (br_if $EVAL_return (i32.eqz (global.get $error_type)))
328 ;; if there is an error and res is set, we need to free it
330 ;; if there is no catch block then return
332 (i32.eqz ($VAL0 ($MEM_VAL0_ptr ($MEM_VAL0_ptr $ast)))))
334 ;; save the current environment for release
335 (local.set $prev_env $env)
336 ;; create environment for the catch block eval
337 (local.set $env ($ENV_NEW $env))
339 ;; set a1 and a2 from the catch block
340 (local.set $a1 ($MAL_GET_A1 ($MAL_GET_A2 $ast)))
341 (local.set $a2 ($MAL_GET_A2 ($MAL_GET_A2 $ast)))
343 ;; create object for string errors
344 (if (i32.eq (global.get $error_type) 1)
346 (local.set $err ($STRING (global.get $STRING_T)
347 (global.get $error_str))))
349 (local.set $err (global.get $error_val))))
350 ;; bind the catch symbol to the error object
351 (drop ($ENV_SET $env $a1 $err))
352 ;; release our use, env took ownership
355 ;; unset error for catch eval
356 (global.set $error_type 0)
357 (i32.store (global.get $error_str) (CHR "\x00"))
359 ;; release previous environment if not the current EVAL env
360 (if (i32.ne $prev_env $orig_env)
363 (local.set $prev_env 0)))
367 (else (if (i32.eqz ($strcmp "if" $a0sym))
369 (local.set $a1 ($MAL_GET_A1 $ast))
370 (local.set $res ($EVAL $a1 $env))
372 (if (global.get $error_type)
374 (else (if (OR (i32.eq $res (global.get $NIL))
375 (i32.eq $res (global.get $FALSE)))
378 ;; if no false case (A3), return nil
379 (if (i32.lt_u ($COUNT $ast) 4)
381 (local.set $res ($INC_REF (global.get $NIL)))
384 (local.set $ast ($MAL_GET_A3 $ast)))))
387 (local.set $ast ($MAL_GET_A2 $ast))))))
389 (else (if (i32.eqz ($strcmp "fn*" $a0sym))
391 (local.set $a1 ($MAL_GET_A1 $ast))
392 (local.set $a2 ($MAL_GET_A2 $ast))
393 (local.set $res ($MALFUNC $a2 $a1 $env))
397 (local.set $res ($EVAL_AST $ast $env 0))
398 (local.set $f_args $res)
400 ;; if error, return f/args for release by caller
401 (if (global.get $error_type)
403 (local.set $res $f_args)
406 (local.set $args ($MEM_VAL0_ptr $f_args)) ;; rest
407 (local.set $f ($DEREF_META ($MEM_VAL1_ptr $f_args))) ;; value
409 (local.set $ftype ($TYPE $f))
410 (if (i32.eq $ftype (global.get $FUNCTION_T))
412 (if (i32.eq ($VAL0 $f) 0) ;; eval
414 (local.set $res ($EVAL ($MEM_VAL1_ptr $args)
415 (global.get $repl_env))))
417 (local.set $res (call_indirect (type $fnT) $args ($VAL0 $f)))))
421 (else (if (i32.eq $ftype (global.get $MALFUNC_T))
423 ;; save the current environment for release
424 (local.set $prev_env $env)
425 ;; create new environment using env and params stored in function
426 (local.set $env ($ENV_NEW_BINDS ($MEM_VAL2_ptr $f)
427 ($MEM_VAL1_ptr $f) $args))
429 ;; release previous environment if not the current EVAL env
430 ;; because our new env refers to it and we no longer need to
431 ;; track it (since we are TCO recurring)
432 (if (i32.ne $prev_env $orig_env)
435 (local.set $prev_env 0)))
437 ;; claim the AST before releasing the list containing it
438 (local.set $ast ($MEM_VAL0_ptr $f))
439 (drop ($INC_REF $ast))
441 ;; if we have already been here via TCO, release previous
444 (if $prev_ast ($RELEASE $prev_ast))
445 (local.set $prev_ast $ast)
452 ($THROW_STR_1 "apply of non-function type: %d\n" $ftype)
455 (br $EVAL_return)))))))))))))))))))))))))
458 ) ;; end of EVAL_return
461 (if (i32.ne $env $orig_env) ($RELEASE $env))
462 (if $prev_ast ($RELEASE $prev_ast))
464 ;; release memory from MACROEXPAND
465 ;; TODO: needs to happen here so self-hosting doesn't leak
468 (br_if $done (i32.le_s (global.get $mac_stack_top) $orig_mac_stack_top))
469 ($RELEASE (i32.load (i32.add
470 (global.get $mac_stack)
471 (i32.mul (global.get $mac_stack_top) 4))))
472 (global.set $mac_stack_top
473 (i32.sub (global.get $mac_stack_top) 1))
482 (func $PRINT (param $ast i32) (result i32)
487 (func $RE (param $line i32 $env i32) (result i32)
490 (local.set $mv1 ($READ $line))
491 (br_if $done (global.get $error_type))
493 (local.set $res ($EVAL $mv1 $env))
496 ;; release memory from MAL_READ
501 (func $REP (param $line i32 $env i32) (result i32)
504 (local.set $mv2 ($RE $line $env))
505 (br_if $done (global.get $error_type))
507 ;; ($PR_MEMORY -1 -1)
508 (local.set $ms ($PRINT $mv2))
511 ;; release memory from RE
516 (func $main (param $argc i32 $argv i32) (result i32)
517 (LET $line (STATIC_ARRAY 201)
518 $res 0 $repl_env 0 $ms 0
519 ;; argument processing
520 $i 0 $ret 0 $empty 0 $current 0 $val2 0)
523 ;; ($printf_1 "argc: 0x%x\n" $argc)
524 ;; ($printf_1 "memoryBase: 0x%x\n" (global.get $memoryBase))
525 ;; ($printf_1 "heap_start: 0x%x\n" (global.get $heap_start))
526 ;; ($printf_1 "heap_end: 0x%x\n" (global.get $heap_end))
527 ;; ($printf_1 "mem: 0x%x\n" (global.get $mem))
528 ;; ($printf_1 "string_mem: %d\n" (global.get $string_mem))
530 (global.set $repl_env ($ENV_NEW (global.get $NIL)))
531 (local.set $repl_env (global.get $repl_env))
533 ;; core.EXT: defined in wasm
534 ($add_core_ns $repl_env)
535 (drop ($ENV_SET_S $repl_env "eval" ($FUNCTION 0)))
537 ($checkpoint_user_memory)
539 ;; core.mal: defined using the language itself
540 ($RELEASE ($RE "(def! *host-language* \"WebAssembly\")" $repl_env))
541 ($RELEASE ($RE "(def! not (fn* (a) (if a false true)))" $repl_env))
542 ($RELEASE ($RE "(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \"\nnil)\")))))" $repl_env))
543 ($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))
545 ;; Command line arguments
546 (local.set $res ($MAP_LOOP_START (global.get $LIST_T)))
547 ;; push MAP_LOP stack
548 ;; empty = current = ret = res
549 (local.set $ret $res)
550 (local.set $current $res)
551 (local.set $empty $res)
556 (br_if $done (i32.ge_u $i $argc))
558 (local.set $val2 ($STRING (global.get $STRING_T)
559 (i32.load (i32.add $argv (i32.mul $i 4)))))
562 (local.set $res ($MAP_LOOP_UPDATE
563 (global.get $LIST_T) $empty $current $val2 0))
564 (if (i32.le_u $current (global.get $EMPTY_HASHMAP))
565 ;; if first element, set return to new element
566 (local.set $ret $res))
567 ;; update current to point to new element
568 (local.set $current $res)
570 (local.set $i (i32.add $i 1))
574 (drop ($ENV_SET_S $repl_env "*ARGV*" $ret))
579 (if (i32.gt_u $argc 1)
581 (drop ($ENV_SET_S $repl_env
582 "*FILE*" ($STRING (global.get $STRING_T)
583 (i32.load (i32.add $argv 4)))))
584 ($RELEASE ($RE "(load-file *FILE*)" $repl_env))
585 (if (global.get $error_type)
587 ($printf_1 "Error: %s\n" (global.get $error_str))
592 ($RELEASE ($RE "(println (str \"Mal [\" *host-language* \"]\"))" $repl_env))
597 (br_if $repl_done (i32.eqz ($readline "user> " $line)))
598 (br_if $repl_loop (i32.eq (i32.load8_u $line) 0))
599 (local.set $res ($REP $line $repl_env))
600 (if (global.get $error_type)
602 (if (i32.eq 2 (global.get $error_type))
604 (local.set $ms ($pr_str (global.get $error_val) 1))
605 ($printf_1 "Error: %s\n" ($to_String $ms))
607 ($RELEASE (global.get $error_val)))
609 ($printf_1 "Error: %s\n" (global.get $error_str))))
610 (global.set $error_type 0))
612 ($printf_1 "%s\n" ($to_String $res))))
614 ;;($PR_MEMORY_SUMMARY_SMALL)