3 push!(LOAD_PATH, pwd(), "/usr/share/julia/base")
18 (isa(ast, Array) || isa(ast, Tuple)) && length(ast) > 0
21 function quasiquote(ast)
24 elseif ast[1] == :unquote
26 elseif ispair(ast[1]) && ast[1][1] == symbol("splice-unquote")
27 [[:concat]; Any[ast[1][2]]; Any[quasiquote(ast[2:end])]]
29 [[:cons]; Any[quasiquote(ast[1])]; Any[quasiquote(ast[2:end])]]
33 function eval_ast(ast, env)
34 if typeof(ast) == Symbol
36 elseif isa(ast, Array) || isa(ast, Tuple)
37 map((x) -> EVAL(x,env), ast)
39 [EVAL(x[1],env) => EVAL(x[2], env) for x=ast]
45 function EVAL(ast, env)
47 #println("EVAL: $(printer.pr_str(ast,true))")
48 if !isa(ast, Array) return eval_ast(ast, env) end
49 if isempty(ast) return ast end
53 return env_set(env, ast[2], EVAL(ast[3], env))
54 elseif symbol("let*") == ast[1]
56 for i = 1:2:length(ast[2])
57 env_set(let_env, ast[2][i], EVAL(ast[2][i+1], let_env))
62 elseif :quote == ast[1]
64 elseif :quasiquote == ast[1]
65 ast = quasiquote(ast[2])
68 eval_ast(ast[2:end-1], env)
72 cond = EVAL(ast[2], env)
73 if cond === nothing || cond === false
84 elseif symbol("fn*") == ast[1]
86 (args...) -> EVAL(ast[3], Env(env, ast[2], Any[args...])),
89 el = eval_ast(ast, env)
90 f, args = el[1], el[2:end]
93 env = Env(f.env, f.params, args)
110 return PRINT(EVAL(READ(str), repl_env))
113 # core.jl: defined using Julia
114 repl_env = Env(nothing, core.ns)
115 env_set(repl_env, :eval, (ast) -> EVAL(ast, repl_env))
116 env_set(repl_env, symbol("*ARGV*"), ARGS[2:end])
118 # core.mal: defined using the language itself
119 REP("(def! not (fn* (a) (if a false true)))")
120 REP("(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))")
123 REP("(load-file \"$(ARGS[1])\")")
128 line = readline_mod.do_readline("user> ")
129 if line === nothing break end
133 if isa(e, ErrorException)
134 println("Error: $(e.msg)")
136 println("Error: $(string(e))")
138 # TODO: show at least part of stack
139 if !isa(e, StackOverflowError)
140 bt = catch_backtrace()
141 Base.show_backtrace(STDERR, bt)