crystal: add seq and string?
[jackhill/mal.git] / julia / step2_eval.jl
CommitLineData
2991d6e3
JM
1#!/usr/bin/env julia
2
82484631 3push!(LOAD_PATH, pwd(), "/usr/share/julia/base")
85110962 4import readline_mod
2991d6e3
JM
5import reader
6import printer
7
8# READ
9function READ(str)
10 reader.read_str(str)
11end
12
13# EVAL
14function eval_ast(ast, env)
15 if typeof(ast) == Symbol
16 env[ast]
9dc47efa 17 elseif isa(ast, Array) || isa(ast, Tuple)
2991d6e3 18 map((x) -> EVAL(x,env), ast)
4430aab9
JM
19 elseif isa(ast, Dict)
20 [EVAL(x[1],env) => EVAL(x[2], env) for x=ast]
2991d6e3
JM
21 else
22 ast
23 end
24end
25
26function EVAL(ast, env)
4430aab9 27 if !isa(ast, Array) return eval_ast(ast, env) end
2991d6e3
JM
28
29 # apply
30 el = eval_ast(ast, env)
31 f, args = el[1], el[2:end]
32 f(args...)
33end
34
35# PRINT
36function PRINT(exp)
37 printer.pr_str(exp)
38end
39
40# REPL
82484631
JM
41repl_env = Dict{Any,Any}(:+ => +,
42 :- => -,
43 :* => *,
44 :/ => div)
2991d6e3
JM
45function REP(str)
46 return PRINT(EVAL(READ(str), repl_env))
47end
48
49while true
85110962
JM
50 line = readline_mod.do_readline("user> ")
51 if line === nothing break end
2991d6e3
JM
52 try
53 println(REP(line))
54 catch e
55 if isa(e, ErrorException)
56 println("Error: $(e.msg)")
57 else
58 println("Error: $(string(e))")
59 end
60 bt = catch_backtrace()
61 Base.show_backtrace(STDERR, bt)
62 println()
63 end
64end