Commit | Line | Data |
---|---|---|
9dc47efa JM |
1 | #!/usr/bin/env julia |
2 | ||
82484631 | 3 | push!(LOAD_PATH, pwd(), "/usr/share/julia/base") |
85110962 | 4 | import readline_mod |
9dc47efa JM |
5 | import reader |
6 | import printer | |
7 | using env | |
8 | ||
9 | # READ | |
10 | function READ(str) | |
11 | reader.read_str(str) | |
12 | end | |
13 | ||
14 | # EVAL | |
15 | function eval_ast(ast, env) | |
16 | if typeof(ast) == Symbol | |
82484631 | 17 | env_get(env,ast) |
9dc47efa JM |
18 | elseif isa(ast, Array) || isa(ast, Tuple) |
19 | map((x) -> EVAL(x,env), ast) | |
7e0bb668 JM |
20 | elseif isa(ast, Dict) |
21 | [EVAL(x[1],env) => EVAL(x[2], env) for x=ast] | |
9dc47efa JM |
22 | else |
23 | ast | |
24 | end | |
25 | end | |
26 | ||
27 | function EVAL(ast, env) | |
7e0bb668 | 28 | if !isa(ast, Array) return eval_ast(ast, env) end |
9dc47efa JM |
29 | |
30 | # apply | |
31 | if :def! == ast[1] | |
82484631 | 32 | env_set(env, ast[2], EVAL(ast[3], env)) |
9dc47efa | 33 | elseif symbol("let*") == ast[1] |
7e0bb668 | 34 | let_env = Env(env) |
9dc47efa | 35 | for i = 1:2:length(ast[2]) |
82484631 | 36 | env_set(let_env, ast[2][i], EVAL(ast[2][i+1], let_env)) |
9dc47efa JM |
37 | end |
38 | EVAL(ast[3], let_env) | |
39 | else | |
40 | el = eval_ast(ast, env) | |
41 | f, args = el[1], el[2:end] | |
42 | f(args...) | |
43 | end | |
44 | end | |
45 | ||
46 | ||
47 | function PRINT(exp) | |
48 | printer.pr_str(exp) | |
49 | end | |
50 | ||
51 | # REPL | |
52 | repl_env = Env(nothing, | |
82484631 JM |
53 | Dict{Any,Any}(:+ => +, |
54 | :- => -, | |
55 | :* => *, | |
56 | :/ => div)) | |
9dc47efa JM |
57 | function REP(str) |
58 | return PRINT(EVAL(READ(str), repl_env)) | |
59 | end | |
60 | ||
61 | while true | |
85110962 JM |
62 | line = readline_mod.do_readline("user> ") |
63 | if line === nothing break end | |
9dc47efa JM |
64 | try |
65 | println(REP(line)) | |
66 | catch e | |
67 | if isa(e, ErrorException) | |
68 | println("Error: $(e.msg)") | |
69 | else | |
70 | println("Error: $(string(e))") | |
71 | end | |
72 | bt = catch_backtrace() | |
73 | Base.show_backtrace(STDERR, bt) | |
74 | println() | |
75 | end | |
76 | end |