mirror of
https://github.com/kanaka/mal.git
synced 2024-09-21 02:27:10 +03:00
0a19c2f1c7
wasm: update to wat syntax as of Jan 2019. Examples: - get_local -> local.get - i32.wrap/i64 -> i32.warp_i64 - etc The distinction between wat and wast has been clarified: - wat: textual format for web assembly modules - wast: superset of wat used in the specification to define tests.
329 lines
9.6 KiB
Plaintext
329 lines
9.6 KiB
Plaintext
(module $step4_if_fn_do
|
|
|
|
(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) (result i32)
|
|
(LET $res 0 $val2 0 $val3 0 $type 0 $found 0
|
|
$ret 0 $empty 0 $current 0)
|
|
|
|
(if (global.get $error_type) (return 0))
|
|
(local.set $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
|
|
(local.set $res ($ENV_GET $env $ast))
|
|
(br $done))
|
|
;; list, vector, hashmap
|
|
;; MAP_LOOP_START
|
|
(local.set $res ($MAP_LOOP_START $type))
|
|
;; push MAP_LOOP stack
|
|
;;; empty = current = ret = res
|
|
(local.set $ret $res)
|
|
(local.set $current $res)
|
|
(local.set $empty $res)
|
|
|
|
(block $done
|
|
(loop $loop
|
|
;; check if we are done evaluating the source sequence
|
|
(br_if $done (i32.eq ($VAL0 $ast) 0))
|
|
|
|
(if (i32.eq $type (global.get $HASHMAP_T))
|
|
(then
|
|
(local.set $res ($EVAL ($MEM_VAL2_ptr $ast) $env)))
|
|
(else
|
|
(local.set $res ($EVAL ($MEM_VAL1_ptr $ast) $env))))
|
|
(local.set $val2 $res)
|
|
|
|
;; if error, release the unattached element
|
|
(if (global.get $error_type)
|
|
(then
|
|
($RELEASE $res)
|
|
(local.set $res 0)
|
|
(br $done)))
|
|
|
|
;; for hash-maps, copy the key (inc ref since we are going
|
|
;; to release it below)
|
|
(if (i32.eq $type (global.get $HASHMAP_T))
|
|
(then
|
|
(local.set $val3 $val2)
|
|
(local.set $val2 ($MEM_VAL1_ptr $ast))
|
|
(drop ($INC_REF $val2))))
|
|
|
|
;; MAP_LOOP_UPDATE
|
|
(local.set $res ($MAP_LOOP_UPDATE $type $empty $current $val2 $val3))
|
|
(if (i32.le_u $current (global.get $EMPTY_HASHMAP))
|
|
;; if first element, set return to new element
|
|
(local.set $ret $res))
|
|
;; update current to point to new element
|
|
(local.set $current $res)
|
|
|
|
(local.set $ast ($MEM_VAL0_ptr $ast))
|
|
|
|
(br $loop)
|
|
)
|
|
)
|
|
;; MAP_LOOP_DONE
|
|
(local.set $res $ret)
|
|
;; EVAL_AST_RETURN: nothing to do
|
|
(br $done))
|
|
;; default
|
|
(local.set $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 $ast i32 $env i32) (result i32)
|
|
(LET $res 0 $el 0
|
|
$ftype 0 $f_args 0 $f 0 $args 0
|
|
$a0 0 $a0sym 0 $a1 0 $a2 0 $a3 0
|
|
$let_env 0 $fn_env 0 $a 0)
|
|
|
|
(local.set $f_args 0)
|
|
(local.set $f 0)
|
|
(local.set $args 0)
|
|
|
|
(if (global.get $error_type) (return 0))
|
|
|
|
;;($PR_VALUE ">>> EVAL ast: '%s'\n" $ast)
|
|
|
|
(if (i32.ne ($TYPE $ast) (global.get $LIST_T))
|
|
(return ($EVAL_AST $ast $env)))
|
|
|
|
;; APPLY_LIST
|
|
(if ($EMPTY_Q $ast)
|
|
(return ($INC_REF $ast)))
|
|
|
|
(local.set $a0 ($MEM_VAL1_ptr $ast))
|
|
(local.set $a0sym "")
|
|
(if (i32.eq ($TYPE $a0) (global.get $SYMBOL_T))
|
|
(local.set $a0sym ($to_String $a0)))
|
|
|
|
(if (i32.eqz ($strcmp "def!" $a0sym))
|
|
(then
|
|
(local.set $a1 ($MAL_GET_A1 $ast))
|
|
(local.set $a2 ($MAL_GET_A2 $ast))
|
|
(local.set $res ($EVAL $a2 $env))
|
|
(if (global.get $error_type) (return $res))
|
|
|
|
;; set a1 in env to a2
|
|
(local.set $res ($ENV_SET $env $a1 $res)))
|
|
(else (if (i32.eqz ($strcmp "let*" $a0sym))
|
|
(then
|
|
(local.set $a1 ($MAL_GET_A1 $ast))
|
|
(local.set $a2 ($MAL_GET_A2 $ast))
|
|
|
|
;; create new environment with outer as current environment
|
|
(local.set $let_env ($ENV_NEW $env))
|
|
|
|
(block $done
|
|
(loop $loop
|
|
(br_if $done (i32.eqz ($VAL0 $a1)))
|
|
;; eval current A1 odd element
|
|
(local.set $res ($EVAL ($MEM_VAL1_ptr ($MEM_VAL0_ptr $a1))
|
|
$let_env))
|
|
|
|
(br_if $done (global.get $error_type))
|
|
|
|
;; set key/value in the let environment
|
|
(local.set $res ($ENV_SET $let_env ($MEM_VAL1_ptr $a1) $res))
|
|
;; release our use, ENV_SET took ownership
|
|
($RELEASE $res)
|
|
|
|
;; skip to the next pair of a1 elements
|
|
(local.set $a1 ($MEM_VAL0_ptr ($MEM_VAL0_ptr $a1)))
|
|
(br $loop)
|
|
)
|
|
)
|
|
(local.set $res ($EVAL $a2 $let_env))
|
|
;; EVAL_RETURN
|
|
($RELEASE $let_env))
|
|
(else (if (i32.eqz ($strcmp "do" $a0sym))
|
|
(then
|
|
(local.set $el ($EVAL_AST ($MEM_VAL0_ptr $ast) $env))
|
|
(local.set $res ($LAST $el))
|
|
($RELEASE $el))
|
|
(else (if (i32.eqz ($strcmp "if" $a0sym))
|
|
(then
|
|
(local.set $a1 ($MAL_GET_A1 $ast))
|
|
(local.set $res ($EVAL $a1 $env))
|
|
|
|
(if (global.get $error_type)
|
|
(then (nop))
|
|
(else (if (OR (i32.eq $res (global.get $NIL))
|
|
(i32.eq $res (global.get $FALSE)))
|
|
(then
|
|
($RELEASE $res)
|
|
;; if no false case (A3), return nil
|
|
(if (i32.lt_u ($COUNT $ast) 4)
|
|
(then
|
|
(local.set $res ($INC_REF (global.get $NIL))))
|
|
(else
|
|
(local.set $a3 ($MAL_GET_A3 $ast))
|
|
(local.set $res ($EVAL $a3 $env)))))
|
|
(else
|
|
($RELEASE $res)
|
|
(local.set $a2 ($MAL_GET_A2 $ast))
|
|
(local.set $res ($EVAL $a2 $env)))))))
|
|
(else (if (i32.eqz ($strcmp "fn*" $a0sym))
|
|
(then
|
|
(local.set $a1 ($MAL_GET_A1 $ast))
|
|
(local.set $a2 ($MAL_GET_A2 $ast))
|
|
(local.set $res ($ALLOC (global.get $MALFUNC_T) $a2 $a1 $env)))
|
|
(else
|
|
;; EVAL_INVOKE
|
|
(local.set $res ($EVAL_AST $ast $env))
|
|
(local.set $f_args $res)
|
|
|
|
;; if error, return f/args for release by caller
|
|
(if (global.get $error_type)
|
|
(return $f_args))
|
|
|
|
(local.set $args ($MEM_VAL0_ptr $f_args)) ;; rest
|
|
(local.set $f ($MEM_VAL1_ptr $f_args)) ;; value
|
|
|
|
(local.set $ftype ($TYPE $f))
|
|
(if (i32.eq $ftype (global.get $FUNCTION_T))
|
|
(then
|
|
(local.set $res (call_indirect (type $fnT) $args ($VAL0 $f)))
|
|
;; release f/args
|
|
($RELEASE $f_args))
|
|
(else (if (i32.eq $ftype (global.get $MALFUNC_T))
|
|
(then
|
|
(local.set $fn_env ($ENV_NEW_BINDS ($MEM_VAL2_ptr $f)
|
|
($MEM_VAL1_ptr $f) $args))
|
|
|
|
;; claim the AST before releasing the list containing it
|
|
(local.set $a ($MEM_VAL0_ptr $f))
|
|
(drop ($INC_REF $a))
|
|
|
|
;; release f/args
|
|
($RELEASE $f_args)
|
|
|
|
(local.set $res ($EVAL $a $fn_env))
|
|
;; EVAL_RETURN
|
|
($RELEASE $fn_env)
|
|
($RELEASE $a))
|
|
(else
|
|
;; create new environment using env and params stored in function
|
|
($THROW_STR_1 "apply of non-function type: %d\n" $ftype)
|
|
(local.set $res 0)
|
|
($RELEASE $f_args)))))))))))))))
|
|
|
|
$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
|
|
(local.set $mv1 ($READ $line))
|
|
(br_if $done (global.get $error_type))
|
|
|
|
(local.set $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
|
|
(local.set $mv2 ($RE $line $env))
|
|
(br_if $done (global.get $error_type))
|
|
|
|
;; ($PR_MEMORY -1 -1)
|
|
(local.set $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" (global.get $memoryBase))
|
|
;; ($printf_1 "heap_start: 0x%x\n" (global.get $heap_start))
|
|
;; ($printf_1 "heap_end: 0x%x\n" (global.get $heap_end))
|
|
;; ($printf_1 "mem: 0x%x\n" (global.get $mem))
|
|
;; ($printf_1 "string_mem: %d\n" (global.get $string_mem))
|
|
|
|
(global.set $repl_env ($ENV_NEW (global.get $NIL)))
|
|
(local.set $repl_env (global.get $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))
|
|
(local.set $res ($REP $line $repl_env))
|
|
(if (global.get $error_type)
|
|
(then
|
|
(if (i32.eq 2 (global.get $error_type))
|
|
(then
|
|
(local.set $ms ($pr_str (global.get $error_val) 1))
|
|
($printf_1 "Error: %s\n" ($to_String $ms))
|
|
($RELEASE $ms)
|
|
($RELEASE (global.get $error_val)))
|
|
(else
|
|
($printf_1 "Error: %s\n" (global.get $error_str))))
|
|
(global.set $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))
|
|
)
|
|
|