matlab: support Octave 4.0.0
[jackhill/mal.git] / d / step7_quote.d
CommitLineData
f82cb965
DM
1module main;
2
3import std.algorithm;
4import std.array;
5import std.range;
6import std.stdio;
7import std.string;
8import std.c.process;
9import env;
10import mal_core;
11import readline;
12import reader;
13import printer;
14import types;
15
16bool is_pair(MalType ast)
17{
18 auto lst = cast(MalSequential) ast;
19 if (lst is null) return false;
20 return lst.elements.length > 0;
21}
22
23MalType quasiquote(MalType ast)
24{
25 if (!is_pair(ast))
26 {
27 return new MalList([sym_quote, ast]);
28 }
29 auto ast_seq = verify_cast!MalSequential(ast);
30 auto aste = ast_seq.elements;
31 if (aste[0] == sym_unquote)
32 {
33 return aste[1];
34 }
35
36 if (is_pair(aste[0]))
37 {
38 auto ast0_seq = verify_cast!MalSequential(aste[0]);
39 if (ast0_seq.elements[0] == sym_splice_unquote)
40 {
41 return new MalList([new MalSymbol("concat"), ast0_seq.elements[1], quasiquote(new MalList(aste[1..$]))]);
42 }
43 }
44
45 return new MalList([new MalSymbol("cons"), quasiquote(aste[0]), quasiquote(new MalList(aste[1..$]))]);
46}
47
48MalType READ(string str)
49{
50 return read_str(str);
51}
52
53MalType eval_ast(MalType ast, Env env)
54{
55 if (typeid(ast) == typeid(MalSymbol))
56 {
57 auto sym = verify_cast!MalSymbol(ast);
58 return env.get(sym);
59 }
60 else if (typeid(ast) == typeid(MalList))
61 {
62 auto lst = verify_cast!MalList(ast);
63 auto el = array(lst.elements.map!(e => EVAL(e, env)));
64 return new MalList(el);
65 }
66 else if (typeid(ast) == typeid(MalVector))
67 {
68 auto lst = verify_cast!MalVector(ast);
69 auto el = array(lst.elements.map!(e => EVAL(e, env)));
70 return new MalVector(el);
71 }
72 else if (typeid(ast) == typeid(MalHashmap))
73 {
74 auto hm = verify_cast!MalHashmap(ast);
75 typeof(hm.data) new_data;
76 foreach (string k, MalType v; hm.data)
77 {
78 new_data[k] = EVAL(v, env);
79 }
80 return new MalHashmap(new_data);
81 }
82 else
83 {
84 return ast;
85 }
86}
87
88MalType EVAL(MalType ast, Env env)
89{
90 for (;;)
91 {
92 MalList ast_list = cast(MalList) ast;
93 if (ast_list is null)
94 {
95 return eval_ast(ast, env);
96 }
97
98 auto aste = ast_list.elements;
99 auto a0_sym = cast(MalSymbol) aste[0];
100 auto sym_name = a0_sym is null ? "" : a0_sym.name;
101 switch (sym_name)
102 {
103 case "def!":
104 auto a1 = verify_cast!MalSymbol(aste[1]);
105 return env.set(a1, EVAL(aste[2], env));
106
107 case "let*":
108 auto a1 = verify_cast!MalSequential(aste[1]);
109 auto let_env = new Env(env);
110 foreach (kv; chunks(a1.elements, 2))
111 {
112 if (kv.length < 2) throw new Exception("let* requires even number of elements");
113 auto var_name = verify_cast!MalSymbol(kv[0]);
114 let_env.set(var_name, EVAL(kv[1], let_env));
115 }
116 ast = aste[2];
117 env = let_env;
118 continue; // TCO
119
120 case "quote":
121 return aste[1];
122
123 case "quasiquote":
124 ast = quasiquote(aste[1]);
125 continue; // TCO
126
127 case "do":
128 auto all_but_last = new MalList(aste[1..$-1]);
129 eval_ast(all_but_last, env);
130 ast = aste[$-1];
131 continue; // TCO
132
133 case "if":
134 auto cond = EVAL(aste[1], env);
135 if (cond.is_truthy())
136 {
137 ast = aste[2];
138 continue; // TCO
139 }
140 else
141 if (aste.length > 3)
142 {
143 ast = aste[3];
144 continue; // TCO
145 }
146 else
147 {
148 return mal_nil;
149 }
150
151 case "fn*":
152 auto args_list = verify_cast!MalSequential(aste[1]);
153 return new MalFunc(args_list.elements, aste[2], env);
154
155 default:
156 auto el = verify_cast!MalList(eval_ast(ast, env));
157 if (el.elements.length == 0)
158 {
159 throw new Exception("Expected a non-empty list");
160 }
161 auto first = el.elements[0];
162 auto rest = el.elements[1..$];
163 if (typeid(first) == typeid(MalFunc))
164 {
165 auto funcobj = verify_cast!MalFunc(first);
166 auto callenv = new Env(funcobj.def_env, funcobj.arg_names, rest);
167 ast = funcobj.func_body;
168 env = callenv;
169 continue; // TCO
170 }
171 else if (typeid(first) == typeid(MalBuiltinFunc))
172 {
173 auto builtinfuncobj = verify_cast!MalBuiltinFunc(first);
174 return builtinfuncobj.fn(rest);
175 }
176 else
177 {
178 throw new Exception("Expected a function");
179 }
180 }
181 }
182}
183
184string PRINT(MalType ast)
185{
186 return pr_str(ast);
187}
188
189MalType re(string str, Env env)
190{
191 return EVAL(READ(str), env);
192}
193
194string rep(string str, Env env)
195{
196 return PRINT(re(str, env));
197}
198
199static MalList create_argv_list(string[] args)
200{
201 if (args.length <= 2) return new MalList([]);
202 return new MalList(array(args[2..$].map!(s => cast(MalType)(new MalString(s)))));
203}
204
205void main(string[] args)
206{
207 Env repl_env = new Env(null);
208 foreach (string sym_name, BuiltinStaticFuncType f; core_ns)
209 {
210 repl_env.set(new MalSymbol(sym_name), new MalBuiltinFunc(f, sym_name));
211 }
212
213 BuiltinFuncType eval_func = (a ...) {
214 verify_args_count(a, 1);
215 return EVAL(a[0], repl_env);
216 };
217 repl_env.set(new MalSymbol("eval"), new MalBuiltinFunc(eval_func, "eval"));
218 repl_env.set(new MalSymbol("*ARGV*"), create_argv_list(args));
219
220 // core.mal: defined using the language itself
221 re("(def! not (fn* (a) (if a false true)))", repl_env);
222 re("(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))", repl_env);
223
224 if (args.length > 1)
225 {
226 try
227 {
228 rep("(load-file \"" ~ args[1] ~ "\")", repl_env);
229 return;
230 }
231 catch (Exception e)
232 {
233 writeln("Error: ", e.msg);
234 std.c.process.exit(1);
235 }
236 }
237
238 for (;;)
239 {
240 string line = _readline("user> ");
241 if (line is null) break;
242 if (line.length == 0) continue;
243 try
244 {
245 writeln(rep(line, repl_env));
246 }
247 catch (Exception e)
248 {
249 writeln("Error: ", e.msg);
250 }
251 }
252 writeln("");
253}