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