crystal: fix early steps broken because of later refactoring
[jackhill/mal.git] / crystal / step7_quote.cr
CommitLineData
ce0696d5 1#! /usr/bin/env crystal run
2
3require "./readline"
4require "./reader"
5require "./printer"
6require "./types"
7require "./env"
8require "./core"
9require "./error"
10
11# Note:
12# Employed downcase names because Crystal prohibits uppercase names for methods
13
14def func_of(env, binds, body)
15 -> (args : Array(Mal::Type)) {
16 new_env = Mal::Env.new(env, binds, args)
17 eval(body, new_env)
18 } as Mal::Func
19end
20
21def eval_ast(ast, env)
22 return ast.map{|n| eval(n, env) as Mal::Type} if ast.is_a? Mal::List
23
24 val = ast.unwrap
25
26 Mal::Type.new case val
27 when Mal::Symbol
28 if e = env.get(val.str)
29 e
30 else
31 eval_error "'#{val.str}' not found"
32 end
33 when Mal::List
34 val.each_with_object(Mal::List.new){|n, l| l << eval(n, env)}
35 when Mal::Vector
36 val.each_with_object(Mal::Vector.new){|n, l| l << eval(n, env)}
37 when Array(Mal::Type)
38 val.map{|n| eval(n, env)}
39 when Mal::HashMap
40 val.each{|k, v| val[k] = eval(v, env)}
41 val
42 else
43 val
44 end
45end
46
47def read(str)
48 read_str str
49end
50
51macro is_pair(list)
52 {{list}}.is_a?(Array) && !{{list}}.empty?
53end
54
55def quasiquote(ast)
56 list = ast.unwrap
57
58 unless is_pair(list)
59 return Mal::Type.new(
60 Mal::List.new << gen_type(Mal::Symbol, "quote") << ast
61 )
62 end
63
64 head = list.first.unwrap
65
66 case
67 # ("unquote" ...)
68 when head.is_a?(Mal::Symbol) && head.str == "unquote"
69 list[1]
70 # (("splice-unquote" ...) ...)
71 when is_pair(head) && (arg0 = head.first.unwrap).is_a?(Mal::Symbol) && arg0.str == "splice-unquote"
72 tail = Mal::Type.new list[1..-1].each_with_object(Mal::List.new){|e,l| l << e}
73 Mal::Type.new(
74 Mal::List.new << gen_type(Mal::Symbol, "concat") << head[1] << quasiquote(tail)
75 )
76 else
77 tail = Mal::Type.new list[1..-1].each_with_object(Mal::List.new){|e,l| l << e}
78 Mal::Type.new(
79 Mal::List.new << gen_type(Mal::Symbol, "cons") << quasiquote(list.first) << quasiquote(tail)
80 )
81 end
82end
83
84macro invoke_list(l, env)
85 f = eval({{l}}.first, {{env}}).unwrap
86 args = eval_ast({{l}}[1..-1].each_with_object(Mal::List.new){|i, l| l << i}, {{env}})
87 case f
88 when Mal::Closure
89 ast = f.ast
90 {{env}} = Mal::Env.new(f.env, f.params, args)
91 next # TCO
92 when Mal::Func
93 return f.call args
94 else
95 eval_error "expected function as the first argument"
96 end
97end
98
99def eval(ast, env)
100 # 'next' in 'do...end' has a bug in crystal 0.7.1
101 # https://github.com/manastech/crystal/issues/659
102 while true
103 list = ast.unwrap
104
105 return eval_ast(ast, env) unless list.is_a? Mal::List
106 return gen_type Mal::List if list.empty?
107
108 head = list.first.unwrap
109
110 unless head.is_a? Mal::Symbol
111 return invoke_list(list, env)
112 end
113
114 return Mal::Type.new case head.str
115 when "def!"
116 eval_error "wrong number of argument for 'def!'" unless list.size == 3
117 a1 = list[1].unwrap
118 eval_error "1st argument of 'def!' must be symbol" unless a1.is_a? Mal::Symbol
119 env.set(a1.str, eval(list[2], env))
120 when "let*"
121 eval_error "wrong number of argument for 'def!'" unless list.size == 3
122
123 bindings = list[1].unwrap
124 eval_error "1st argument of 'let*' must be list or vector" unless bindings.is_a? Array
125 eval_error "size of binding list must be even" unless bindings.size.even?
126
127 new_env = Mal::Env.new env
128 bindings.each_slice(2) do |binding|
129 key, value = binding
130 name = key.unwrap
131 eval_error "name of binding must be specified as symbol" unless name.is_a? Mal::Symbol
132 new_env.set(name.str, eval(value, new_env))
133 end
134
135 ast, env = list[2], new_env
136 next # TCO
137 when "do"
138 if list.empty?
139 ast = Mal::Type.new nil
140 next
141 end
142
143 eval_ast(list[1..-2].each_with_object(Mal::List.new){|i,l| l << i}, env)
144 ast = list.last
145 next # TCO
146 when "if"
147 ast = unless eval(list[1], env).unwrap
148 list.size >= 4 ? list[3] : Mal::Type.new(nil)
149 else
150 list[2]
151 end
152 next # TCO
153 when "fn*"
154 params = list[1].unwrap
155 unless params.is_a?(Mal::List) || params.is_a?(Mal::Vector)
156 eval_error "'fn*' parameters must be list"
157 end
158 Mal::Closure.new(list[2], params, env, func_of(env, params, list[2]))
159 when "quote"
160 list[1]
161 when "quasiquote"
162 ast = quasiquote list[1]
163 next # TCO
164 else
165 invoke_list(list, env)
166 end
167 end
168end
169
170def print(result)
171 pr_str(result, true)
172end
173
174def rep(str)
175 print(eval(read(str), $repl_env))
176end
177
178$repl_env = Mal::Env.new nil
179Mal::NS.each{|k,v| $repl_env.set(k, Mal::Type.new(v))}
180$repl_env.set("eval", Mal::Type.new -> (args: Array(Mal::Type)){ eval(args[0], $repl_env) })
181rep "(def! not (fn* (a) (if a false true)))"
182rep "(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))"
183$argv = Mal::List.new
184$repl_env.set("*ARGV*", Mal::Type.new $argv)
185
186unless ARGV.empty?
187 if ARGV.size > 1
188 ARGV[1..-1].each do |a|
189 $argv << Mal::Type.new(a)
190 end
191 end
192
193 begin
194 rep "(load-file \"#{ARGV[0]}\")"
195 rescue e
196 STDERR.puts e
197 end
198 exit
199end
200
201while line = my_readline("user> ")
202 begin
203 puts rep(line)
204 rescue e
205 STDERR.puts e
206 end
207end