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