Merge pull request #238 from prt2121/pt/haskell-7.10.1
[jackhill/mal.git] / ruby / step8_macros.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 :do
103 eval_ast(ast[1..-2], env)
104 ast = ast.last # Continue loop (TCO)
105 when :if
106 cond = EVAL(a1, env)
107 if not cond
108 return nil if a3 == nil
109 ast = a3 # Continue loop (TCO)
110 else
111 ast = a2 # Continue loop (TCO)
112 end
113 when :"fn*"
114 return Function.new(a2, env, a1) {|*args|
115 EVAL(a2, Env.new(env, a1, List.new(args)))
116 }
117 else
118 el = eval_ast(ast, env)
119 f = el[0]
120 if f.class == Function
121 ast = f.ast
122 env = f.gen_env(el.drop(1)) # Continue loop (TCO)
123 else
124 return f[*el.drop(1)]
125 end
126 end
127
128 end
129 end
130
131 # print
132 def PRINT(exp)
133 return _pr_str(exp, true)
134 end
135
136 # repl
137 repl_env = Env.new
138 RE = lambda {|str| EVAL(READ(str), repl_env) }
139 REP = lambda {|str| PRINT(EVAL(READ(str), repl_env)) }
140
141 # core.rb: defined using ruby
142 $core_ns.each do |k,v| repl_env.set(k,v) end
143 repl_env.set(:eval, lambda {|ast| EVAL(ast, repl_env)})
144 repl_env.set(:"*ARGV*", List.new(ARGV.slice(1,ARGV.length) || []))
145
146 # core.mal: defined using the language itself
147 RE["(def! not (fn* (a) (if a false true)))"]
148 RE["(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))"]
149 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)))))))"]
150 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))))))))"]
151
152 if ARGV.size > 0
153 RE["(load-file \"" + ARGV[0] + "\")"]
154 exit 0
155 end
156
157 # repl loop
158 while line = _readline("user> ")
159 begin
160 puts REP[line]
161 rescue Exception => e
162 puts "Error: #{e}"
163 puts "\t#{e.backtrace.join("\n\t")}"
164 end
165 end