Merge branch 'master' into chuck-implementation
[jackhill/mal.git] / ruby / step9_try.rb
1 require_relative "mal_readline"
2 require_relative "types"
3 require_relative "reader"
4 require_relative "printer"
5 require_relative "env"
6 require_relative "core"
7
8 # read
9 def READ(str)
10 return read_str(str)
11 end
12
13 # eval
14 def pair?(x)
15 return sequential?(x) && x.size > 0
16 end
17
18 def quasiquote(ast)
19 if not pair?(ast)
20 return List.new [:quote, ast]
21 elsif ast[0] == :unquote
22 return ast[1]
23 elsif pair?(ast[0]) && ast[0][0] == :"splice-unquote"
24 return List.new [:concat, ast[0][1], quasiquote(ast.drop(1))]
25 else
26 return List.new [:cons, quasiquote(ast[0]), quasiquote(ast.drop(1))]
27 end
28 end
29
30 def macro_call?(ast, env)
31 return (ast.is_a?(List) &&
32 ast[0].is_a?(Symbol) &&
33 env.find(ast[0]) &&
34 env.get(ast[0]).is_a?(Function) &&
35 env.get(ast[0]).is_macro)
36 end
37
38 def macroexpand(ast, env)
39 while macro_call?(ast, env)
40 mac = env.get(ast[0])
41 ast = mac[*ast.drop(1)]
42 end
43 return ast
44 end
45
46 def eval_ast(ast, env)
47 return case ast
48 when Symbol
49 env.get(ast)
50 when List
51 List.new ast.map{|a| EVAL(a, env)}
52 when Vector
53 Vector.new ast.map{|a| EVAL(a, env)}
54 when Hash
55 new_hm = {}
56 ast.each{|k,v| new_hm[EVAL(k,env)] = EVAL(v, env)}
57 new_hm
58 else
59 ast
60 end
61 end
62
63 def EVAL(ast, env)
64 while true
65
66 #puts "EVAL: #{_pr_str(ast, true)}"
67
68 if not ast.is_a? List
69 return eval_ast(ast, env)
70 end
71
72 # apply list
73 ast = macroexpand(ast, env)
74 if not ast.is_a? List
75 return eval_ast(ast, env)
76 end
77 if ast.empty?
78 return ast
79 end
80
81 a0,a1,a2,a3 = ast
82 case a0
83 when :def!
84 return env.set(a1, EVAL(a2, env))
85 when :"let*"
86 let_env = Env.new(env)
87 a1.each_slice(2) do |a,e|
88 let_env.set(a, EVAL(e, let_env))
89 end
90 env = let_env
91 ast = a2 # Continue loop (TCO)
92 when :quote
93 return a1
94 when :quasiquote
95 ast = quasiquote(a1); # Continue loop (TCO)
96 when :defmacro!
97 func = EVAL(a2, env)
98 func.is_macro = true
99 return env.set(a1, func)
100 when :macroexpand
101 return macroexpand(a1, env)
102 when :"try*"
103 begin
104 return EVAL(a1, env)
105 rescue Exception => exc
106 if exc.is_a? MalException
107 exc = exc.data
108 else
109 exc = exc.message
110 end
111 if a2 && a2[0] == :"catch*"
112 return EVAL(a2[2], Env.new(env, [a2[1]], [exc]))
113 else
114 raise esc
115 end
116 end
117 when :do
118 eval_ast(ast[1..-2], env)
119 ast = ast.last # Continue loop (TCO)
120 when :if
121 cond = EVAL(a1, env)
122 if not cond
123 return nil if a3 == nil
124 ast = a3 # Continue loop (TCO)
125 else
126 ast = a2 # Continue loop (TCO)
127 end
128 when :"fn*"
129 return Function.new(a2, env, a1) {|*args|
130 EVAL(a2, Env.new(env, a1, List.new(args)))
131 }
132 else
133 el = eval_ast(ast, env)
134 f = el[0]
135 if f.class == Function
136 ast = f.ast
137 env = f.gen_env(el.drop(1)) # Continue loop (TCO)
138 else
139 return f[*el.drop(1)]
140 end
141 end
142
143 end
144 end
145
146 # print
147 def PRINT(exp)
148 return _pr_str(exp, true)
149 end
150
151 # repl
152 repl_env = Env.new
153 RE = lambda {|str| EVAL(READ(str), repl_env) }
154 REP = lambda {|str| PRINT(EVAL(READ(str), repl_env)) }
155
156 # core.rb: defined using ruby
157 $core_ns.each do |k,v| repl_env.set(k,v) end
158 repl_env.set(:eval, lambda {|ast| EVAL(ast, repl_env)})
159 repl_env.set(:"*ARGV*", List.new(ARGV.slice(1,ARGV.length) || []))
160
161 # core.mal: defined using the language itself
162 RE["(def! not (fn* (a) (if a false true)))"]
163 RE["(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))"]
164 RE["(defmacro! cond (fn* (& xs) (if (> (count xs) 0) (list 'if (first xs) (if (> (count xs) 1) (nth xs 1) (throw \"odd number of forms to cond\")) (cons 'cond (rest (rest xs)))))))"]
165 RE["(defmacro! or (fn* (& xs) (if (empty? xs) nil (if (= 1 (count xs)) (first xs) `(let* (or_FIXME ~(first xs)) (if or_FIXME or_FIXME (or ~@(rest xs))))))))"]
166
167 if ARGV.size > 0
168 RE["(load-file \"" + ARGV[0] + "\")"]
169 exit 0
170 end
171
172 # repl loop
173 while line = _readline("user> ")
174 begin
175 puts REP[line]
176 rescue Exception => e
177 puts "Error: #{e}"
178 puts "\t#{e.backtrace.join("\n\t")}"
179 end
180 end