Commit | Line | Data |
---|---|---|
891c3f3b JM |
1 | readline = require "./node_readline.coffee" |
2 | types = require "./types.coffee" | |
3 | reader = require "./reader.coffee" | |
4 | printer = require "./printer.coffee" | |
5 | Env = require("./env.coffee").Env | |
6 | core = require("./core.coffee") | |
7 | ||
8 | # read | |
9 | READ = (str) -> reader.read_str str | |
10 | ||
11 | # eval | |
12 | eval_ast = (ast, env) -> | |
b8ee29b2 | 13 | if types._symbol_Q(ast) then env.get ast |
891c3f3b JM |
14 | else if types._list_Q(ast) then ast.map((a) -> EVAL(a, env)) |
15 | else if types._vector_Q(ast) | |
16 | types._vector(ast.map((a) -> EVAL(a, env))...) | |
17 | else if types._hash_map_Q(ast) | |
18 | new_hm = {} | |
19 | new_hm[k] = EVAL(ast[k],env) for k,v of ast | |
20 | new_hm | |
21 | else ast | |
22 | ||
23 | EVAL = (ast, env) -> | |
24 | loop | |
25 | #console.log "EVAL:", printer._pr_str ast | |
26 | if !types._list_Q ast then return eval_ast ast, env | |
903b669d | 27 | if ast.length == 0 then return ast |
891c3f3b JM |
28 | |
29 | # apply list | |
30 | [a0, a1, a2, a3] = ast | |
31 | switch a0.name | |
32 | when "def!" | |
b8ee29b2 | 33 | return env.set(a1, EVAL(a2, env)) |
891c3f3b JM |
34 | when "let*" |
35 | let_env = new Env(env) | |
36 | for k,i in a1 when i %% 2 == 0 | |
b8ee29b2 | 37 | let_env.set(a1[i], EVAL(a1[i+1], let_env)) |
891c3f3b JM |
38 | ast = a2 |
39 | env = let_env | |
40 | when "do" | |
41 | eval_ast(ast[1..-2], env) | |
42 | ast = ast[ast.length-1] | |
43 | when "if" | |
44 | cond = EVAL(a1, env) | |
45 | if cond == null or cond == false | |
46 | if a3? then ast = a3 else return null | |
47 | else | |
48 | ast = a2 | |
49 | when "fn*" | |
50 | return types._function(EVAL, a2, env, a1) | |
51 | else | |
52 | [f, args...] = eval_ast ast, env | |
53 | if types._function_Q(f) | |
54 | ast = f.__ast__ | |
55 | env = f.__gen_env__(args) | |
56 | else | |
57 | return f(args...) | |
58 | ||
59 | ||
60 | ||
61 | PRINT = (exp) -> printer._pr_str exp, true | |
62 | ||
63 | # repl | |
64 | repl_env = new Env() | |
65 | rep = (str) -> PRINT(EVAL(READ(str), repl_env)) | |
66 | ||
67 | # core.coffee: defined using CoffeeScript | |
b8ee29b2 JM |
68 | repl_env.set types._symbol(k), v for k,v of core.ns |
69 | repl_env.set types._symbol('eval'), (ast) -> EVAL(ast, repl_env) | |
70 | repl_env.set types._symbol('*ARGV*'), [] | |
891c3f3b JM |
71 | |
72 | # core.mal: defined using the language itself | |
73 | rep("(def! not (fn* (a) (if a false true)))"); | |
e6d41de4 | 74 | rep("(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \"\nnil)\")))))"); |
891c3f3b JM |
75 | |
76 | if process? && process.argv.length > 2 | |
b8ee29b2 | 77 | repl_env.set types._symbol('*ARGV*'), process.argv[3..] |
891c3f3b JM |
78 | rep('(load-file "' + process.argv[2] + '")') |
79 | process.exit 0 | |
80 | ||
81 | # repl loop | |
82 | while (line = readline.readline("user> ")) != null | |
83 | continue if line == "" | |
84 | try | |
85 | console.log rep line | |
86 | catch exc | |
87 | continue if exc instanceof reader.BlankException | |
4ed89670 JM |
88 | if exc.stack? and exc.stack.length > 2000 |
89 | console.log exc.stack.slice(0,1000) + "\n ..." + exc.stack.slice(-1000) | |
dd7a4f55 JM |
90 | else if exc.stack? then console.log exc.stack |
91 | else console.log exc | |
891c3f3b JM |
92 | |
93 | # vim: ts=2:sw=2 |