gensym: hide the counter in an environment, define inc in stepA.
[jackhill/mal.git] / ruby / step8_macros.rb
CommitLineData
107d9694
IJ
1require_relative "mal_readline"
2require_relative "types"
3require_relative "reader"
4require_relative "printer"
5require_relative "env"
6require_relative "core"
d85fc037
JM
7
8# read
9def READ(str)
10 return read_str(str)
11end
12
13# eval
3e8a088f 14def pair?(x)
d85fc037
JM
15 return sequential?(x) && x.size > 0
16end
17
18def quasiquote(ast)
3e8a088f
JM
19 if not pair?(ast)
20 return List.new [:quote, ast]
d85fc037
JM
21 elsif ast[0] == :unquote
22 return ast[1]
3e8a088f
JM
23 elsif pair?(ast[0]) && ast[0][0] == :"splice-unquote"
24 return List.new [:concat, ast[0][1], quasiquote(ast.drop(1))]
d85fc037 25 else
3e8a088f 26 return List.new [:cons, quasiquote(ast[0]), quasiquote(ast.drop(1))]
d85fc037
JM
27 end
28end
29
30def 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)
36end
37
38def 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
44end
45
46def 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)}
3e8a088f
JM
54 when Hash
55 new_hm = {}
56 ast.each{|k,v| new_hm[EVAL(k,env)] = EVAL(v, env)}
57 new_hm
d85fc037
JM
58 else
59 ast
60 end
61end
62
63def 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)
92c7576c
DM
74 if not ast.is_a? List
75 return eval_ast(ast, env)
76 end
f53183a3
DM
77 if ast.empty?
78 return ast
79 end
d85fc037
JM
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
6301e0b6
JM
90 env = let_env
91 ast = a2 # Continue loop (TCO)
d85fc037
JM
92 when :quote
93 return a1
94 when :quasiquote
6301e0b6 95 ast = quasiquote(a1); # Continue loop (TCO)
d85fc037
JM
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)
6301e0b6 104 ast = ast.last # Continue loop (TCO)
d85fc037
JM
105 when :if
106 cond = EVAL(a1, env)
107 if not cond
108 return nil if a3 == nil
6301e0b6 109 ast = a3 # Continue loop (TCO)
d85fc037 110 else
6301e0b6 111 ast = a2 # Continue loop (TCO)
d85fc037
JM
112 end
113 when :"fn*"
114 return Function.new(a2, env, a1) {|*args|
79859c62 115 EVAL(a2, Env.new(env, a1, List.new(args)))
d85fc037
JM
116 }
117 else
118 el = eval_ast(ast, env)
119 f = el[0]
120 if f.class == Function
121 ast = f.ast
6301e0b6 122 env = f.gen_env(el.drop(1)) # Continue loop (TCO)
d85fc037
JM
123 else
124 return f[*el.drop(1)]
125 end
126 end
127
128 end
129end
130
131# print
132def PRINT(exp)
133 return _pr_str(exp, true)
134end
135
136# repl
137repl_env = Env.new
138RE = lambda {|str| EVAL(READ(str), repl_env) }
139REP = lambda {|str| PRINT(EVAL(READ(str), repl_env)) }
d85fc037 140
8cb5cda4
JM
141# core.rb: defined using ruby
142$core_ns.each do |k,v| repl_env.set(k,v) end
143repl_env.set(:eval, lambda {|ast| EVAL(ast, repl_env)})
86b689f3 144repl_env.set(:"*ARGV*", List.new(ARGV.slice(1,ARGV.length) || []))
d85fc037 145
8cb5cda4 146# core.mal: defined using the language itself
d85fc037
JM
147RE["(def! not (fn* (a) (if a false true)))"]
148RE["(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))"]
8cb5cda4
JM
149RE["(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)))))))"]
150RE["(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))))))))"]
d85fc037 151
d85fc037 152if ARGV.size > 0
86b689f3 153 RE["(load-file \"" + ARGV[0] + "\")"]
d85fc037
JM
154 exit 0
155end
86b689f3
JM
156
157# repl loop
718887c3 158while line = _readline("user> ")
d85fc037
JM
159 begin
160 puts REP[line]
161 rescue Exception => e
162 puts "Error: #{e}"
163 puts "\t#{e.backtrace.join("\n\t")}"
164 end
165end