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 | is_pair = (x) -> types._sequential_Q(x) && x.length > 0 | |
13 | ||
14 | quasiquote = (ast) -> | |
15 | if !is_pair(ast) then [types._symbol('quote'), ast] | |
16 | else if ast[0].name == 'unquote' then ast[1] | |
17 | else if is_pair(ast[0]) && ast[0][0].name == 'splice-unquote' | |
18 | [types._symbol('concat'), ast[0][1], quasiquote(ast[1..])] | |
19 | else | |
20 | [types._symbol('cons'), quasiquote(ast[0]), quasiquote(ast[1..])] | |
21 | ||
22 | ||
23 | ||
24 | eval_ast = (ast, env) -> | |
25 | if types._symbol_Q(ast) then env.get ast.name | |
26 | else if types._list_Q(ast) then ast.map((a) -> EVAL(a, env)) | |
27 | else if types._vector_Q(ast) | |
28 | types._vector(ast.map((a) -> EVAL(a, env))...) | |
29 | else if types._hash_map_Q(ast) | |
30 | new_hm = {} | |
31 | new_hm[k] = EVAL(ast[k],env) for k,v of ast | |
32 | new_hm | |
33 | else ast | |
34 | ||
35 | EVAL = (ast, env) -> | |
36 | loop | |
37 | #console.log "EVAL:", printer._pr_str ast | |
38 | if !types._list_Q ast then return eval_ast ast, env | |
39 | ||
40 | # apply list | |
41 | [a0, a1, a2, a3] = ast | |
42 | switch a0.name | |
43 | when "def!" | |
44 | return env.set(a1.name, EVAL(a2, env)) | |
45 | when "let*" | |
46 | let_env = new Env(env) | |
47 | for k,i in a1 when i %% 2 == 0 | |
48 | let_env.set(a1[i].name, EVAL(a1[i+1], let_env)) | |
49 | ast = a2 | |
50 | env = let_env | |
51 | when "quote" | |
52 | return a1 | |
53 | when "quasiquote" | |
54 | ast = quasiquote(a1) | |
55 | when "do" | |
56 | eval_ast(ast[1..-2], env) | |
57 | ast = ast[ast.length-1] | |
58 | when "if" | |
59 | cond = EVAL(a1, env) | |
60 | if cond == null or cond == false | |
61 | if a3? then ast = a3 else return null | |
62 | else | |
63 | ast = a2 | |
64 | when "fn*" | |
65 | return types._function(EVAL, a2, env, a1) | |
66 | else | |
67 | [f, args...] = eval_ast ast, env | |
68 | if types._function_Q(f) | |
69 | ast = f.__ast__ | |
70 | env = f.__gen_env__(args) | |
71 | else | |
72 | return f(args...) | |
73 | ||
74 | ||
75 | ||
76 | PRINT = (exp) -> printer._pr_str exp, true | |
77 | ||
78 | # repl | |
79 | repl_env = new Env() | |
80 | rep = (str) -> PRINT(EVAL(READ(str), repl_env)) | |
81 | ||
82 | # core.coffee: defined using CoffeeScript | |
83 | repl_env.set k, v for k,v of core.ns | |
84 | repl_env.set 'eval', (ast) -> EVAL(ast, repl_env) | |
85 | repl_env.set '*ARGV*', [] | |
86 | ||
87 | # core.mal: defined using the language itself | |
88 | rep("(def! not (fn* (a) (if a false true)))"); | |
89 | rep("(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))"); | |
90 | ||
91 | if process? && process.argv.length > 2 | |
92 | repl_env.set '*ARGV*', process.argv[3..] | |
93 | rep('(load-file "' + process.argv[2] + '")') | |
94 | process.exit 0 | |
95 | ||
96 | # repl loop | |
97 | while (line = readline.readline("user> ")) != null | |
98 | continue if line == "" | |
99 | try | |
100 | console.log rep line | |
101 | catch exc | |
102 | continue if exc instanceof reader.BlankException | |
103 | if exc.stack then console.log exc.stack | |
104 | else console.log exc | |
105 | ||
106 | # vim: ts=2:sw=2 |