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