Merge pull request #238 from prt2121/pt/haskell-7.10.1
[jackhill/mal.git] / ruby / stepA_mal.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 :"rb*"
103 res = eval(a1)
104 return case res
105 when Array; List.new res
106 else; res
107 end
108 when :"try*"
109 begin
110 return EVAL(a1, env)
111 rescue Exception => exc
112 if exc.is_a? MalException
113 exc = exc.data
114 else
115 exc = exc.message
116 end
117 if a2 && a2[0] == :"catch*"
118 return EVAL(a2[2], Env.new(env, [a2[1]], [exc]))
119 else
120 raise esc
121 end
122 end
123 when :do
124 eval_ast(ast[1..-2], env)
125 ast = ast.last # Continue loop (TCO)
126 when :if
127 cond = EVAL(a1, env)
128 if not cond
129 return nil if a3 == nil
130 ast = a3 # Continue loop (TCO)
131 else
132 ast = a2 # Continue loop (TCO)
133 end
134 when :"fn*"
135 return Function.new(a2, env, a1) {|*args|
136 EVAL(a2, Env.new(env, a1, List.new(args)))
137 }
138 else
139 el = eval_ast(ast, env)
140 f = el[0]
141 if f.class == Function
142 ast = f.ast
143 env = f.gen_env(el.drop(1)) # Continue loop (TCO)
144 else
145 return f[*el.drop(1)]
146 end
147 end
148
149 end
150 end
151
152 # print
153 def PRINT(exp)
154 return _pr_str(exp, true)
155 end
156
157 # repl
158 repl_env = Env.new
159 RE = lambda {|str| EVAL(READ(str), repl_env) }
160 REP = lambda {|str| PRINT(EVAL(READ(str), repl_env)) }
161
162 # core.rb: defined using ruby
163 $core_ns.each do |k,v| repl_env.set(k,v) end
164 repl_env.set(:eval, lambda {|ast| EVAL(ast, repl_env)})
165 repl_env.set(:"*ARGV*", List.new(ARGV.slice(1,ARGV.length) || []))
166
167 # core.mal: defined using the language itself
168 RE["(def! *host-language* \"ruby\")"]
169 RE["(def! not (fn* (a) (if a false true)))"]
170 RE["(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))"]
171 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)))))))"]
172 RE["(def! *gensym-counter* (atom 0))"]
173 RE["(def! gensym (fn* [] (symbol (str \"G__\" (swap! *gensym-counter* (fn* [x] (+ 1 x)))))))"]
174 RE["(defmacro! or (fn* (& xs) (if (empty? xs) nil (if (= 1 (count xs)) (first xs) (let* (condvar (gensym)) `(let* (~condvar ~(first xs)) (if ~condvar ~condvar (or ~@(rest xs)))))))))"]
175
176 if ARGV.size > 0
177 RE["(load-file \"" + ARGV[0] + "\")"]
178 exit 0
179 end
180
181 # repl loop
182 RE["(println (str \"Mal [\" *host-language* \"]\"))"]
183 while line = _readline("user> ")
184 begin
185 puts REP[line]
186 rescue Exception => e
187 puts "Error: #{e}"
188 puts "\t#{e.backtrace.join("\n\t")}"
189 end
190 end