3 (global $repl_env (mut i32) (i32.const 0))
6 (func $READ (param $str i32) (result i32)
11 (func $EVAL_AST (param $ast i32 $env i32) (result i32)
13 (LET $res 0 $val2 0 $val3 0 $type 0 $found 0
14 $ret 0 $empty 0 $current 0)
16 (if (global.get $error_type) (return 0))
17 (local.set $type ($TYPE $ast))
19 ;;($PR_VALUE ">>> EVAL_AST ast: '%s'\n" $ast)
23 (block $default (block (block
24 (br_table 2 2 2 2 2 0 1 1 1 2 2 2 2 2 2 2 $type))
26 ;; found/res returned as hi 32/lo 32 of i64
27 (local.set $res2 ($HASHMAP_GET $env $ast))
28 (local.set $res (i32.wrap_i64 $res2))
29 (local.set $found (i32.wrap_i64 (i64.shr_u $res2
32 ($THROW_STR_1 "'%s' not found"
34 (local.set $res ($INC_REF $res))
37 ;; list, vector, hashmap
39 (local.set $res ($MAP_LOOP_START $type))
40 ;; push MAP_LOOP stack
41 ;;; empty = current = ret = res
43 (local.set $current $res)
44 (local.set $empty $res)
48 ;; check if we are done evaluating the source sequence
49 (br_if $done (i32.eq ($VAL0 $ast) 0))
51 (if (i32.eq $type (global.get $HASHMAP_T))
53 (local.set $res ($EVAL ($MEM_VAL2_ptr $ast) $env)))
55 (local.set $res ($EVAL ($MEM_VAL1_ptr $ast) $env))))
56 (local.set $val2 $res)
58 ;; if error, release the unattached element
59 (if (global.get $error_type)
65 ;; for hash-maps, copy the key (inc ref since we are going
66 ;; to release it below)
67 (if (i32.eq $type (global.get $HASHMAP_T))
69 (local.set $val3 $val2)
70 (local.set $val2 ($MEM_VAL1_ptr $ast))
71 (drop ($INC_REF $val2))))
74 (local.set $res ($MAP_LOOP_UPDATE $type $empty $current $val2 $val3))
75 (if (i32.le_u $current (global.get $EMPTY_HASHMAP))
76 ;; if first element, set return to new element
77 (local.set $ret $res))
78 ;; update current to point to new element
79 (local.set $current $res)
81 (local.set $ast ($MEM_VAL0_ptr $ast))
88 ;; EVAL_AST_RETURN: nothing to do
91 (local.set $res ($INC_REF $ast))
97 (type $fnT (func (param i32) (result i32)))
101 $add $subtract $multiply $divide))
103 (func $EVAL (param $ast i32 $env i32) (result i32)
105 $ftype 0 $f_args 0 $f 0 $args 0)
107 (local.set $f_args 0)
111 (if (global.get $error_type) (return 0))
113 ;;($PR_VALUE ">>> EVAL ast: '%s'\n" $ast)
115 (if (i32.ne ($TYPE $ast) (global.get $LIST_T))
116 (return ($EVAL_AST $ast $env)))
120 (return ($INC_REF $ast)))
123 (local.set $res ($EVAL_AST $ast $env))
124 (local.set $f_args $res)
126 ;; if error, return f/args for release by caller
127 (if (global.get $error_type)
130 (local.set $args ($MEM_VAL0_ptr $f_args)) ;; rest
131 (local.set $f ($MEM_VAL1_ptr $f_args)) ;; value
133 (local.set $ftype ($TYPE $f))
134 (if (i32.eq $ftype (global.get $FUNCTION_T))
136 (local.set $res (call_indirect (type $fnT) $args ($VAL0 $f))))
138 ($THROW_STR_1 "apply of non-function type: %d\n" $ftype)
147 (func $PRINT (param $ast i32) (result i32)
152 (func $REP (param $line i32 $env i32) (result i32)
153 (LET $mv1 0 $mv2 0 $ms 0)
155 (local.set $mv1 ($READ $line))
156 (br_if $done (global.get $error_type))
158 (local.set $mv2 ($EVAL $mv1 $env))
159 (br_if $done (global.get $error_type))
161 ;; ($PR_MEMORY -1 -1)
162 (local.set $ms ($PRINT $mv2))
165 ;; release memory from MAL_READ and EVAL
171 (func $add (param $args i32) (result i32)
173 (i32.add ($VAL0 ($MEM_VAL1_ptr $args))
174 ($VAL0 ($MEM_VAL1_ptr ($MEM_VAL0_ptr $args))))))
175 (func $subtract (param $args i32) (result i32)
177 (i32.sub ($VAL0 ($MEM_VAL1_ptr $args))
178 ($VAL0 ($MEM_VAL1_ptr ($MEM_VAL0_ptr $args))))))
179 (func $multiply (param $args i32) (result i32)
181 (i32.mul ($VAL0 ($MEM_VAL1_ptr $args))
182 ($VAL0 ($MEM_VAL1_ptr ($MEM_VAL0_ptr $args))))))
183 (func $divide (param $args i32) (result i32)
185 (i32.div_s ($VAL0 ($MEM_VAL1_ptr $args))
186 ($VAL0 ($MEM_VAL1_ptr ($MEM_VAL0_ptr $args))))))
188 (func $main (param $argc i32 $argv i32) (result i32)
189 (LET $line (STATIC_ARRAY 201)
193 ;; ($printf_1 "memoryBase: 0x%x\n" (global.get $memoryBase))
194 ;; ($printf_1 "heap_start: 0x%x\n" (global.get $heap_start))
195 ;; ($printf_1 "heap_end: 0x%x\n" (global.get $heap_end))
196 ;; ($printf_1 "mem: 0x%x\n" (global.get $mem))
197 ;; ($printf_1 "string_mem: %d\n" (global.get $string_mem))
199 (global.set $repl_env ($HASHMAP))
200 (local.set $repl_env (global.get $repl_env))
202 (local.set $repl_env ($ASSOC1_S $repl_env "+" ($FUNCTION 0)))
203 (local.set $repl_env ($ASSOC1_S $repl_env "-" ($FUNCTION 1)))
204 (local.set $repl_env ($ASSOC1_S $repl_env "*" ($FUNCTION 2)))
205 (local.set $repl_env ($ASSOC1_S $repl_env "/" ($FUNCTION 3)))
212 (br_if $repl_done (i32.eqz ($readline "user> " $line)))
213 (br_if $repl_loop (i32.eq (i32.load8_u $line) 0))
214 (local.set $res ($REP $line $repl_env))
215 (if (global.get $error_type)
217 ($printf_1 "Error: %s\n" (global.get $error_str))
218 (global.set $error_type 0))
220 ($printf_1 "%s\n" ($to_String $res))))
222 ;;($PR_MEMORY_SUMMARY_SMALL)