2015-03-29 22:33:57 +03:00
|
|
|
#!/usr/bin/env julia
|
|
|
|
|
2015-10-24 23:39:13 +03:00
|
|
|
push!(LOAD_PATH, pwd(), "/usr/share/julia/base")
|
2015-04-01 06:17:47 +03:00
|
|
|
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
|
2022-01-10 02:15:40 +03:00
|
|
|
function EVAL(ast, env)
|
|
|
|
# println("EVAL: $(printer.pr_str(ast,true))")
|
|
|
|
|
2015-03-29 22:33:57 +03:00
|
|
|
if typeof(ast) == Symbol
|
2022-01-10 02:15:40 +03:00
|
|
|
return env[ast]
|
|
|
|
elseif isa(ast, Tuple)
|
|
|
|
return map((x) -> EVAL(x,env), ast)
|
2015-04-01 05:23:22 +03:00
|
|
|
elseif isa(ast, Dict)
|
2022-01-10 02:15:40 +03:00
|
|
|
return [x[1] => EVAL(x[2], env) for x=ast]
|
|
|
|
elseif !isa(ast, Array)
|
|
|
|
return ast
|
2015-03-29 22:33:57 +03:00
|
|
|
end
|
|
|
|
|
2016-04-03 01:56:04 +03:00
|
|
|
if isempty(ast) return ast end
|
2015-03-29 22:33:57 +03:00
|
|
|
|
|
|
|
# apply
|
2022-01-10 02:15:40 +03:00
|
|
|
el = map((x) -> EVAL(x,env), ast)
|
2015-03-29 22:33:57 +03:00
|
|
|
f, args = el[1], el[2:end]
|
|
|
|
f(args...)
|
|
|
|
end
|
|
|
|
|
|
|
|
# PRINT
|
|
|
|
function PRINT(exp)
|
|
|
|
printer.pr_str(exp)
|
|
|
|
end
|
|
|
|
|
|
|
|
# REPL
|
2015-10-24 23:39:13 +03:00
|
|
|
repl_env = Dict{Any,Any}(:+ => +,
|
|
|
|
:- => -,
|
|
|
|
:* => *,
|
|
|
|
:/ => div)
|
2015-03-29 22:33:57 +03:00
|
|
|
function REP(str)
|
|
|
|
return PRINT(EVAL(READ(str), repl_env))
|
|
|
|
end
|
|
|
|
|
|
|
|
while true
|
2015-04-01 06:17:47 +03:00
|
|
|
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
|