1
1
mirror of https://github.com/kanaka/mal.git synced 2024-09-19 09:38:28 +03:00
mal/wasm/step1_read_print.wam
Joel Martin ed13313d87 wasm: direct platform, wam memory
- Rename platform_os.wam to platform_direct.wam and update the direct
  interface to use printline instead of fputs/stdout.
- Update to wamp 1.0.7 and new memory/memoryBase semantics: in
  platform_direct and platform_libc, import memory and memoryBase
  rather than relying on wamp to add it because wamp add direct memory
  and memoryBase defintions (not imports) if the program doesn't
  already define or import memory/memoryBase.
- Simplify entry point logic by moving it into the platform files.
2019-04-15 00:57:59 -05:00

82 lines
1.9 KiB
Plaintext

(module $step1_read_print
;; READ
(func $READ (param $str i32) (result i32)
($read_str $str)
)
;; EVAL
(func $EVAL (param $ast i32 $env i32) (result i32)
$ast
)
;; PRINT
(func $PRINT (param $ast i32) (result i32)
($pr_str $ast 1)
)
;; REPL
(func $REP (param $line i32 $env i32) (result i32)
(LET $mv1 0 $mv2 0 $ms 0)
(block $done
(local.set $mv1 ($READ $line))
(br_if $done (global.get $error_type))
(local.set $mv2 ($EVAL $mv1 $env))
(br_if $done (global.get $error_type))
;; ($PR_MEMORY -1 -1)
(local.set $ms ($PRINT $mv2))
)
;; release memory from MAL_READ
($RELEASE $mv1)
$ms
)
(func $main (param $argc i32 $argv i32) (result i32)
(LET $line (STATIC_ARRAY 201)
$res 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))
;; ($PR_MEMORY_RAW
;; (global.get $mem) (i32.add (global.get $mem)
;; (i32.mul (global.get $mem_unused_start) 4)))
(drop ($STRING (global.get $STRING_T) "uvw"))
(drop ($STRING (global.get $STRING_T) "xyz"))
;;($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 0))
(if (global.get $error_type)
(then
($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
)
)