basic: args file that doesn't rely on return value.
[jackhill/mal.git] / coffee / step7_quote.coffee
CommitLineData
891c3f3b
JM
1readline = require "./node_readline.coffee"
2types = require "./types.coffee"
3reader = require "./reader.coffee"
4printer = require "./printer.coffee"
5Env = require("./env.coffee").Env
6core = require("./core.coffee")
7
8# read
9READ = (str) -> reader.read_str str
10
11# eval
12is_pair = (x) -> types._sequential_Q(x) && x.length > 0
13
14quasiquote = (ast) ->
15 if !is_pair(ast) then [types._symbol('quote'), ast]
c963be7a 16 else if ast[0] != null && ast[0].name == 'unquote' then ast[1]
891c3f3b
JM
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
24eval_ast = (ast, env) ->
b8ee29b2 25 if types._symbol_Q(ast) then env.get ast
891c3f3b
JM
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
35EVAL = (ast, env) ->
36 loop
37 #console.log "EVAL:", printer._pr_str ast
38 if !types._list_Q ast then return eval_ast ast, env
903b669d 39 if ast.length == 0 then return ast
891c3f3b
JM
40
41 # apply list
42 [a0, a1, a2, a3] = ast
43 switch a0.name
44 when "def!"
b8ee29b2 45 return env.set(a1, EVAL(a2, env))
891c3f3b
JM
46 when "let*"
47 let_env = new Env(env)
48 for k,i in a1 when i %% 2 == 0
b8ee29b2 49 let_env.set(a1[i], EVAL(a1[i+1], let_env))
891c3f3b
JM
50 ast = a2
51 env = let_env
52 when "quote"
53 return a1
54 when "quasiquote"
55 ast = quasiquote(a1)
56 when "do"
57 eval_ast(ast[1..-2], env)
58 ast = ast[ast.length-1]
59 when "if"
60 cond = EVAL(a1, env)
61 if cond == null or cond == false
62 if a3? then ast = a3 else return null
63 else
64 ast = a2
65 when "fn*"
66 return types._function(EVAL, a2, env, a1)
67 else
68 [f, args...] = eval_ast ast, env
69 if types._function_Q(f)
70 ast = f.__ast__
71 env = f.__gen_env__(args)
72 else
73 return f(args...)
74
75
76# print
77PRINT = (exp) -> printer._pr_str exp, true
78
79# repl
80repl_env = new Env()
81rep = (str) -> PRINT(EVAL(READ(str), repl_env))
82
83# core.coffee: defined using CoffeeScript
b8ee29b2
JM
84repl_env.set types._symbol(k), v for k,v of core.ns
85repl_env.set types._symbol('eval'), (ast) -> EVAL(ast, repl_env)
86repl_env.set types._symbol('*ARGV*'), []
891c3f3b
JM
87
88# core.mal: defined using the language itself
89rep("(def! not (fn* (a) (if a false true)))");
90rep("(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))");
91
92if process? && process.argv.length > 2
b8ee29b2 93 repl_env.set types._symbol('*ARGV*'), process.argv[3..]
891c3f3b
JM
94 rep('(load-file "' + process.argv[2] + '")')
95 process.exit 0
96
97# repl loop
98while (line = readline.readline("user> ")) != null
99 continue if line == ""
100 try
101 console.log rep line
102 catch exc
103 continue if exc instanceof reader.BlankException
4ed89670
JM
104 if exc.stack? and exc.stack.length > 2000
105 console.log exc.stack.slice(0,1000) + "\n ..." + exc.stack.slice(-1000)
dd7a4f55
JM
106 else if exc.stack? then console.log exc.stack
107 else console.log exc
891c3f3b
JM
108
109# vim: ts=2:sw=2