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 ismacroCall(ast, env)
34 return isa(ast, Array) &&
35 isa(ast[1], Symbol) &&
36 env_find(env, ast[1]) != nothing &&
37 isa(env_get(env, ast[1]), MalFunc) &&
38 env_get(env, ast[1]).ismacro
41 function macroexpand(ast, env)
42 while ismacroCall(ast, env)
43 mac = env_get(env, ast[1])
44 ast = mac.fn(ast[2:end]...)
49 function eval_ast(ast, env)
50 if typeof(ast) == Symbol
52 elseif isa(ast, Array) || isa(ast, Tuple)
53 map((x) -> EVAL(x,env), ast)
55 [EVAL(x[1],env) => EVAL(x[2], env) for x=ast]
61 function EVAL(ast, env)
63 #println("EVAL: $(printer.pr_str(ast,true))")
64 if !isa(ast, Array) return eval_ast(ast, env) end
67 ast = macroexpand(ast, env)
68 if !isa(ast, Array) return ast end
71 return env_set(env, ast[2], EVAL(ast[3], env))
72 elseif symbol("let*") == ast[1]
74 for i = 1:2:length(ast[2])
75 env_set(let_env, ast[2][i], EVAL(ast[2][i+1], let_env))
80 elseif :quote == ast[1]
82 elseif :quasiquote == ast[1]
83 ast = quasiquote(ast[2])
85 elseif :defmacro! == ast[1]
86 func = EVAL(ast[3], env)
88 return env_set(env, ast[2], func)
89 elseif :macroexpand == ast[1]
90 return macroexpand(ast[2], env)
91 elseif symbol("try*") == ast[1]
93 return EVAL(ast[2], env)
96 if isa(exc, MalException)
98 elseif isa(exc, ErrorException)
103 if length(ast) > 2 && ast[3][1] == symbol("catch*")
104 return EVAL(ast[3][3], Env(env, Any[ast[3][2]], Any[e]))
110 eval_ast(ast[2:end-1], env)
114 cond = EVAL(ast[2], env)
115 if cond === nothing || cond === false
126 elseif symbol("fn*") == ast[1]
128 (args...) -> EVAL(ast[3], Env(env, ast[2], args)),
131 el = eval_ast(ast, env)
132 f, args = el[1], el[2:end]
135 env = Env(f.env, f.params, args)
152 return PRINT(EVAL(READ(str), repl_env))
155 # core.jl: defined using Julia
156 repl_env = Env(nothing, core.ns)
157 env_set(repl_env, :eval, (ast) -> EVAL(ast, repl_env))
158 env_set(repl_env, symbol("*ARGV*"), ARGS[2:end])
160 # core.mal: defined using the language itself
161 REP("(def! not (fn* (a) (if a false true)))")
162 REP("(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))")
163 REP("(defmacro! cond (fn* (& xs) (if (> (count xs) 0) (list 'if (first xs) (if (> (count xs) 1) (nth xs 1) (throw \"odd number of forms to cond\")) (cons 'cond (rest (rest xs)))))))")
164 REP("(defmacro! or (fn* (& xs) (if (empty? xs) nil (if (= 1 (count xs)) (first xs) `(let* (or_FIXME ~(first xs)) (if or_FIXME or_FIXME (or ~@(rest xs))))))))")
168 REP("(load-file \"$(ARGS[1])\")")
173 line = readline_mod.do_readline("user> ")
174 if line === nothing break end
178 if isa(e, ErrorException)
179 println("Error: $(e.msg)")
181 println("Error: $(string(e))")
183 # TODO: show at least part of stack
184 if !isa(e, StackOverflowError)
185 bt = catch_backtrace()
186 Base.show_backtrace(STDERR, bt)