1
1
mirror of https://github.com/kanaka/mal.git synced 2024-10-27 22:58:00 +03:00
mal/julia/step2_eval.jl

64 lines
1.1 KiB
Julia
Raw Normal View History

2015-03-29 22:33:57 +03:00
#!/usr/bin/env julia
import readline_mod
2015-03-29 22:33:57 +03:00
import reader
import printer
# READ
function READ(str)
reader.read_str(str)
end
# EVAL
function eval_ast(ast, env)
if typeof(ast) == Symbol
env[ast]
2015-03-29 23:00:27 +03:00
elseif isa(ast, Array) || isa(ast, Tuple)
2015-03-29 22:33:57 +03:00
map((x) -> EVAL(x,env), ast)
2015-04-01 05:23:22 +03:00
elseif isa(ast, Dict)
[EVAL(x[1],env) => EVAL(x[2], env) for x=ast]
2015-03-29 22:33:57 +03:00
else
ast
end
end
function EVAL(ast, env)
2015-04-01 05:23:22 +03:00
if !isa(ast, Array) return eval_ast(ast, env) end
2015-03-29 22:33:57 +03:00
# apply
el = eval_ast(ast, env)
f, args = el[1], el[2:end]
f(args...)
end
# PRINT
function PRINT(exp)
printer.pr_str(exp)
end
# REPL
repl_env = {:+ => +,
:- => -,
:* => *,
:/ => div}
function REP(str)
return PRINT(EVAL(READ(str), repl_env))
end
while true
line = readline_mod.do_readline("user> ")
if line === nothing break end
2015-03-29 22:33:57 +03:00
try
println(REP(line))
catch e
if isa(e, ErrorException)
println("Error: $(e.msg)")
else
println("Error: $(string(e))")
end
bt = catch_backtrace()
Base.show_backtrace(STDERR, bt)
println()
end
end