application/octet-stream
•
1.87 KB
•
76 lines
[def display/error [let*
[def wrap [λ [i text]
[cond [[eq? i 0] [ansi-red text]]
[[eq? i 1] text]
[[eq? i 2] [ansi-yellow text]]
[#t text]
]
]]
[def iter [λ [error i]
[if error
[cons [wrap i [string [car error]]]
[iter [cdr error] [++ i]]]
[cons "" #nil]]
]]
[λ [error]
"Display ERROR in a nice, human readable way"
[display [join [iter error 0] "\r\n"]]
]
]]
[def repl/exception-handler [λ [error]
[display/error error]
]]
[def repl/history #nil]
[def repl/prompt [λ []
"> "
]]
[def repl/wasm [let*
[def ctx [ω]]
[λ [line]
"Evaluate LINE in the wasm context"
[try repl/exception-handler
[def raw [read line]]
[def expr [cons 'do raw]]
[def cexpr [cons [compile expr]]]
[apply ctx cexpr]
]
]
]]
[def repl/readline [λ [ctx]
[def line [readline [repl/prompt]]]
[when [nil? line]
[display "Bye!\r\n"]
[exit 0]
]
[readline/history/add line]
[def raw [read line]]
[def expr [cons 'do raw]]
[def cexpr [cons [compile expr]]]
[def result [apply ctx cexpr]]
[set! repl/history [cons line repl/history]]
[display [cat [if [nil? result]
""
[str/write result]]
"\n"
]]
]]
[def repl [λ []
[readline/history/load [readline/history/path]]
[def ctx [ω]]
[while #t
[try repl/exception-handler
[repl/readline ctx]
]
[readline/history/save [readline/history/path]]
]
]]