d21ef941 |
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 | def 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 |
19 | end |
20 | |
21 | def 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 |
45 | end |
46 | |
47 | def eval_invocation(list, env) |
48 | f = eval(list.first, env).unwrap |
49 | case f |
50 | when Mal::Closure |
51 | f.fn.call eval_ast(list[1..-1].each_with_object(Mal::List.new){|i, l| l << i}, env) |
52 | when Mal::Func |
53 | f.call eval_ast(list[1..-1].each_with_object(Mal::List.new){|i, l| l << i}, env) |
54 | else |
55 | eval_error "expected function as the first argument" |
56 | end |
57 | end |
58 | |
59 | def read(str) |
60 | read_str str |
61 | end |
62 | |
63 | macro invoke_list(l, env) |
64 | f = eval({{l}}.first, {{env}}).unwrap |
65 | args = eval_ast({{l}}[1..-1].each_with_object(Mal::List.new){|i, l| l << i}, {{env}}) |
66 | case f |
67 | when Mal::Closure |
68 | ast = f.ast |
69 | {{env}} = Mal::Env.new(f.env, f.params, args) |
70 | next # TCO |
71 | when Mal::Func |
72 | return f.call args |
73 | else |
74 | eval_error "expected function as the first argument" |
75 | end |
76 | end |
77 | |
78 | def eval(ast, env) |
79 | # 'next' in 'do...end' has a bug in crystal 0.7.1 |
80 | # https://github.com/manastech/crystal/issues/659 |
81 | while true |
82 | list = ast.unwrap |
83 | |
84 | return eval_ast(ast, env) unless list.is_a? Mal::List |
85 | return gen_type Mal::List if list.empty? |
86 | |
87 | head = list.first.unwrap |
88 | |
89 | unless head.is_a? Mal::Symbol |
90 | invoke_list(list, env) |
91 | end |
92 | |
93 | return Mal::Type.new case head.str |
94 | when "def!" |
95 | eval_error "wrong number of argument for 'def!'" unless list.size == 3 |
96 | a1 = list[1].unwrap |
97 | eval_error "1st argument of 'def!' must be symbol" unless a1.is_a? Mal::Symbol |
98 | env.set(a1.str, eval(list[2], env)) |
99 | when "let*" |
100 | eval_error "wrong number of argument for 'def!'" unless list.size == 3 |
101 | |
102 | bindings = list[1].unwrap |
103 | eval_error "1st argument of 'let*' must be list or vector" unless bindings.is_a? Array |
104 | eval_error "size of binding list must be even" unless bindings.size.even? |
105 | |
106 | new_env = Mal::Env.new env |
107 | bindings.each_slice(2) do |binding| |
108 | key, value = binding |
109 | name = key.unwrap |
110 | eval_error "name of binding must be specified as symbol" unless name.is_a? Mal::Symbol |
111 | new_env.set(name.str, eval(value, new_env)) |
112 | end |
113 | |
114 | ast, env = list[2], new_env |
115 | next # TCO |
116 | when "do" |
117 | if list.empty? |
118 | ast = Mal::Type.new nil |
119 | next |
120 | end |
121 | |
122 | eval_ast(list[1..-2].each_with_object(Mal::List.new){|i,l| l << i}, env) |
123 | ast = list.last |
124 | next # TCO |
125 | when "if" |
126 | ast = unless eval(list[1], env).unwrap |
127 | list.size >= 4 ? list[3] : Mal::Type.new(nil) |
128 | else |
129 | list[2] |
130 | end |
131 | next # TCO |
132 | when "fn*" |
133 | params = list[1].unwrap |
134 | unless params.is_a?(Mal::List) || params.is_a?(Mal::Vector) |
135 | eval_error "'fn*' parameters must be list" |
136 | end |
137 | Mal::Closure.new(list[2], params, env, func_of(env, params, list[2])) |
138 | else |
139 | invoke_list(list, env) |
140 | end |
141 | end |
142 | end |
143 | |
144 | def print(result) |
145 | pr_str(result, true) |
146 | end |
147 | |
148 | def rep(str) |
149 | print(eval(read(str), $repl_env)) |
150 | end |
151 | |
152 | $repl_env = Mal::Env.new nil |
153 | Mal::NS.each{|k,v| $repl_env.set(k, Mal::Type.new(v))} |
154 | $repl_env.set("eval", Mal::Type.new -> (args: Array(Mal::Type)){ eval(args[0], $repl_env) }) |
155 | rep "(def! not (fn* (a) (if a false true)))" |
156 | rep "(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))" |
157 | $argv = Mal::List.new |
158 | $repl_env.set("*ARGV*", Mal::Type.new $argv) |
159 | |
160 | unless ARGV.empty? |
161 | if ARGV.size > 1 |
162 | ARGV[1..-1].each do |a| |
163 | $argv << Mal::Type.new(a) |
164 | end |
165 | end |
166 | |
167 | rep "(load-file \"#{ARGV[0]}\")" |
168 | exit |
169 | end |
170 | |
171 | while line = my_readline("user> ") |
172 | begin |
173 | puts rep(line) |
174 | rescue e |
175 | STDERR.puts e |
176 | end |
177 | end |