(module $step5_tco (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 $skiplast i32) (result i32) (LET $res 0 $val2 0 $val3 0 $type 0 $found 0 $ret 0 $empty 0 $current 0) (if (get_global $error_type) (return 0)) (set_local $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 (set_local $res ($ENV_GET $env $ast)) (br $done)) ;; list, vector, hashmap ;; MAP_LOOP_START (set_local $res ($MAP_LOOP_START $type)) ;; push MAP_LOOP stack ;;; empty = current = ret = res (set_local $ret $res) (set_local $current $res) (set_local $empty $res) (block $done (loop $loop ;; check if we are done evaluating the source sequence (br_if $done (i32.eq ($VAL0 $ast) 0)) (if $skiplast (br_if $done (i32.eqz ($VAL0 ($MEM_VAL0_ptr $ast))))) (if (i32.eq $type (get_global $HASHMAP_T)) (then (set_local $res ($EVAL ($MEM_VAL2_ptr $ast) $env))) (else (set_local $res ($EVAL ($MEM_VAL1_ptr $ast) $env)))) (set_local $val2 $res) ;; if error, release the unattached element (if (get_global $error_type) (then ($RELEASE $res) (set_local $res 0) (br $done))) ;; for hash-maps, copy the key (inc ref since we are going ;; to release it below) (if (i32.eq $type (get_global $HASHMAP_T)) (then (set_local $val3 $val2) (set_local $val2 ($MEM_VAL1_ptr $ast)) (drop ($INC_REF $val2)))) ;; MAP_LOOP_UPDATE (set_local $res ($MAP_LOOP_UPDATE $type $empty $current $val2 $val3)) (if (i32.le_u $current (get_global $EMPTY_HASHMAP)) ;; if first element, set return to new element (set_local $ret $res)) ;; update current to point to new element (set_local $current $res) (set_local $ast ($MEM_VAL0_ptr $ast)) (br $loop) ) ) ;; MAP_LOOP_DONE (set_local $res $ret) ;; EVAL_AST_RETURN: nothing to do (br $done)) ;; default (set_local $res ($INC_REF $ast)) ) $res ) (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 $orig_ast i32 $orig_env i32) (result i32) (LET $ast $orig_ast $env $orig_env $prev_ast 0 $prev_env 0 $res 0 $el 0 $ftype 0 $f_args 0 $f 0 $args 0 $a0 0 $a0sym 0 $a1 0 $a2 0) (block $EVAL_return (loop $TCO_loop (set_local $f_args 0) (set_local $f 0) (set_local $args 0) (if (get_global $error_type) (then (set_local $res 0) (br $EVAL_return))) ;;($PR_VALUE ">>> EVAL ast: '%s'\n" $ast) (if (i32.ne ($TYPE $ast) (get_global $LIST_T)) (then (set_local $res ($EVAL_AST $ast $env 0)) (br $EVAL_return))) ;; APPLY_LIST (if ($EMPTY_Q $ast) (then (set_local $res ($INC_REF $ast)) (br $EVAL_return))) (set_local $a0 ($MEM_VAL1_ptr $ast)) (set_local $a0sym "") (if (i32.eq ($TYPE $a0) (get_global $SYMBOL_T)) (set_local $a0sym ($to_String $a0))) (if (i32.eqz ($strcmp "def!" $a0sym)) (then (set_local $a1 ($MAL_GET_A1 $ast)) (set_local $a2 ($MAL_GET_A2 $ast)) (set_local $res ($EVAL $a2 $env)) (br_if $EVAL_return (get_global $error_type)) ;; set a1 in env to a2 (set_local $res ($ENV_SET $env $a1 $res)) (br $EVAL_return)) (else (if (i32.eqz ($strcmp "let*" $a0sym)) (then (set_local $a1 ($MAL_GET_A1 $ast)) (set_local $a2 ($MAL_GET_A2 $ast)) ;; create new environment with outer as current environment (set_local $prev_env $env) ;; save env for later release (set_local $env ($ENV_NEW $env)) (block $done (loop $loop (br_if $done (i32.eqz ($VAL0 $a1))) ;; eval current A1 odd element (set_local $res ($EVAL ($MEM_VAL1_ptr ($MEM_VAL0_ptr $a1)) $env)) (br_if $done (get_global $error_type)) ;; set key/value in the let environment (set_local $res ($ENV_SET $env ($MEM_VAL1_ptr $a1) $res)) ;; release our use, ENV_SET took ownership ($RELEASE $res) ;; skip to the next pair of a1 elements (set_local $a1 ($MEM_VAL0_ptr ($MEM_VAL0_ptr $a1))) (br $loop) ) ) ;; release previous environment if not the current EVAL env (if (i32.ne $prev_env $orig_env) (then ($RELEASE $prev_env) (set_local $prev_env 0))) (set_local $ast $a2) (br $TCO_loop)) (else (if (i32.eqz ($strcmp "do" $a0sym)) (then ;; EVAL the rest through second to last (set_local $el ($EVAL_AST ($MEM_VAL0_ptr $ast) $env 1)) (set_local $ast ($LAST $ast)) ($RELEASE $ast) ;; we already own it via ast ($RELEASE $el) (br $TCO_loop)) (else (if (i32.eqz ($strcmp "if" $a0sym)) (then (set_local $a1 ($MAL_GET_A1 $ast)) (set_local $res ($EVAL $a1 $env)) (if (get_global $error_type) (then (nop)) (else (if (OR (i32.eq $res (get_global $NIL)) (i32.eq $res (get_global $FALSE))) (then ($RELEASE $res) ;; if no false case (A3), return nil (if (i32.lt_u ($COUNT $ast) 4) (then (set_local $res ($INC_REF (get_global $NIL))) (br $EVAL_return)) (else (set_local $ast ($MAL_GET_A3 $ast))))) (else ($RELEASE $res) (set_local $ast ($MAL_GET_A2 $ast)))))) (br $TCO_loop)) (else (if (i32.eqz ($strcmp "fn*" $a0sym)) (then (set_local $a1 ($MAL_GET_A1 $ast)) (set_local $a2 ($MAL_GET_A2 $ast)) (set_local $res ($MALFUNC $a2 $a1 $env)) (br $EVAL_return)) (else ;; EVAL_INVOKE (set_local $res ($EVAL_AST $ast $env 0)) (set_local $f_args $res) ;; if error, return f/args for release by caller (if (get_global $error_type) (then (set_local $res $f_args) (br $EVAL_return))) (set_local $args ($MEM_VAL0_ptr $f_args)) ;; rest (set_local $f ($MEM_VAL1_ptr $f_args)) ;; value (set_local $ftype ($TYPE $f)) (if (i32.eq $ftype (get_global $FUNCTION_T)) (then (set_local $res (call_indirect (type $fnT) $args ($VAL0 $f))) ;; release f/args ($RELEASE $f_args) (br $EVAL_return)) (else (if (i32.eq $ftype (get_global $MALFUNC_T)) (then ;; save the current environment for release (set_local $prev_env $env) ;; create new environment using env and params stored in function (set_local $env ($ENV_NEW_BINDS ($MEM_VAL2_ptr $f) ($MEM_VAL1_ptr $f) $args)) ;; release previous environment if not the current EVAL env ;; because our new env refers to it and we no longer need to ;; track it (since we are TCO recurring) (if (i32.ne $prev_env $orig_env) (then ($RELEASE $prev_env) (set_local $prev_env 0))) ;; claim the AST before releasing the list containing it (set_local $ast ($MEM_VAL0_ptr $f)) (drop ($INC_REF $ast)) ;; if we have already been here via TCO, release previous ;; ast ;; PEND_A_LV (if $prev_ast ($RELEASE $prev_ast)) (set_local $prev_ast $ast) ;; release f/args ($RELEASE $f_args) (br $TCO_loop)) (else ($THROW_STR_1 "apply of non-function type: %d\n" $ftype) (set_local $res 0) ($RELEASE $f_args) (br $EVAL_return))))))))))))))) ) ;; end of TCO_loop ) ;; end of EVAL_return ;; EVAL_RETURN (if (i32.ne $env $orig_env) ($RELEASE $env)) (if $prev_ast ($RELEASE $prev_ast)) $res ) ;; PRINT (func $PRINT (param $ast i32) (result i32) ($pr_str $ast 1) ) ;; REPL (func $RE (param $line i32 $env i32) (result i32) (LET $mv1 0 $res 0) (block $done (set_local $mv1 ($READ $line)) (br_if $done (get_global $error_type)) (set_local $res ($EVAL $mv1 $env)) ) ;; release memory from MAL_READ ($RELEASE $mv1) $res ) (func $REP (param $line i32 $env i32) (result i32) (LET $mv2 0 $ms 0) (block $done (set_local $mv2 ($RE $line $env)) (br_if $done (get_global $error_type)) ;; ($PR_MEMORY -1 -1) (set_local $ms ($PRINT $mv2)) ) ;; release memory from RE ($RELEASE $mv2) $ms ) (func $main (result i32) (LET $line (STATIC_ARRAY 201) $res 0 $repl_env 0 $ms 0) ;; DEBUG ;; ($printf_1 "memoryBase: 0x%x\n" (get_global $memoryBase)) ;; ($printf_1 "heap_start: 0x%x\n" (get_global $heap_start)) ;; ($printf_1 "heap_end: 0x%x\n" (get_global $heap_end)) ;; ($printf_1 "mem: 0x%x\n" (get_global $mem)) ;; ($printf_1 "string_mem: %d\n" (get_global $string_mem)) (set_global $repl_env ($ENV_NEW (get_global $NIL))) (set_local $repl_env (get_global $repl_env)) ;; core.EXT: defined in wasm ($add_core_ns $repl_env) ($checkpoint_user_memory) ;; core.mal: defined using the language itself ($RELEASE ($RE "(def! not (fn* (a) (if a false true)))" $repl_env)) ;;($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)) (set_local $res ($REP $line $repl_env)) (if (get_global $error_type) (then (if (i32.eq 2 (get_global $error_type)) (then (set_local $ms ($pr_str (get_global $error_val) 1)) ($printf_1 "Error: %s\n" ($to_String $ms)) ($RELEASE $ms) ($RELEASE (get_global $error_val))) (else ($printf_1 "Error: %s\n" (get_global $error_str)))) (set_global $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 ) (export "_main" (func $main)) (export "__post_instantiate" (func $init_memory)) )