Commit | Line | Data |
---|---|---|
faee4d12 JM |
1 | using System; |
2 | using System.IO; | |
3 | using System.Collections; | |
4 | using System.Collections.Generic; | |
5 | using Mal; | |
faee4d12 JM |
6 | using MalVal = Mal.types.MalVal; |
7 | using MalString = Mal.types.MalString; | |
8 | using MalSymbol = Mal.types.MalSymbol; | |
c3b508af | 9 | using MalInt = Mal.types.MalInt; |
faee4d12 JM |
10 | using MalList = Mal.types.MalList; |
11 | using MalVector = Mal.types.MalVector; | |
12 | using MalHashMap = Mal.types.MalHashMap; | |
c3b508af | 13 | using MalFunc = Mal.types.MalFunc; |
faee4d12 JM |
14 | using Env = Mal.env.Env; |
15 | ||
16 | namespace Mal { | |
01c97316 | 17 | class stepA_interop { |
faee4d12 JM |
18 | // read |
19 | static MalVal READ(string str) { | |
20 | return reader.read_str(str); | |
21 | } | |
22 | ||
23 | // eval | |
24 | public static bool is_pair(MalVal x) { | |
25 | return x is MalList && ((MalList)x).size() > 0; | |
26 | } | |
27 | ||
28 | public static MalVal quasiquote(MalVal ast) { | |
29 | if (!is_pair(ast)) { | |
30 | return new MalList(new MalSymbol("quote"), ast); | |
31 | } else { | |
32 | MalVal a0 = ((MalList)ast)[0]; | |
33 | if ((a0 is MalSymbol) && | |
34 | (((MalSymbol)a0).getName() == "unquote")) { | |
35 | return ((MalList)ast)[1]; | |
36 | } else if (is_pair(a0)) { | |
37 | MalVal a00 = ((MalList)a0)[0]; | |
38 | if ((a00 is MalSymbol) && | |
39 | (((MalSymbol)a00).getName() == "splice-unquote")) { | |
40 | return new MalList(new MalSymbol("concat"), | |
41 | ((MalList)a0)[1], | |
42 | quasiquote(((MalList)ast).rest())); | |
43 | } | |
44 | } | |
45 | return new MalList(new MalSymbol("cons"), | |
46 | quasiquote(a0), | |
47 | quasiquote(((MalList)ast).rest())); | |
48 | } | |
49 | } | |
50 | ||
51 | public static bool is_macro_call(MalVal ast, Env env) { | |
52 | if (ast is MalList) { | |
53 | MalVal a0 = ((MalList)ast)[0]; | |
54 | if (a0 is MalSymbol && | |
55 | env.find(((MalSymbol)a0).getName()) != null) { | |
56 | MalVal mac = env.get(((MalSymbol)a0).getName()); | |
c3b508af JM |
57 | if (mac is MalFunc && |
58 | ((MalFunc)mac).isMacro()) { | |
faee4d12 JM |
59 | return true; |
60 | } | |
61 | } | |
62 | } | |
63 | return false; | |
64 | } | |
65 | ||
66 | public static MalVal macroexpand(MalVal ast, Env env) { | |
67 | while (is_macro_call(ast, env)) { | |
68 | MalSymbol a0 = (MalSymbol)((MalList)ast)[0]; | |
c3b508af | 69 | MalFunc mac = (MalFunc) env.get(a0.getName()); |
faee4d12 JM |
70 | ast = mac.apply(((MalList)ast).rest()); |
71 | } | |
72 | return ast; | |
73 | } | |
74 | ||
75 | static MalVal eval_ast(MalVal ast, Env env) { | |
76 | if (ast is MalSymbol) { | |
77 | MalSymbol sym = (MalSymbol)ast; | |
78 | return env.get(sym.getName()); | |
79 | } else if (ast is MalList) { | |
80 | MalList old_lst = (MalList)ast; | |
81 | MalList new_lst = ast.list_Q() ? new MalList() | |
82 | : (MalList)new MalVector(); | |
83 | foreach (MalVal mv in old_lst.getValue()) { | |
84 | new_lst.conj_BANG(EVAL(mv, env)); | |
85 | } | |
86 | return new_lst; | |
87 | } else if (ast is MalHashMap) { | |
88 | var new_dict = new Dictionary<string, MalVal>(); | |
89 | foreach (var entry in ((MalHashMap)ast).getValue()) { | |
90 | new_dict.Add(entry.Key, EVAL((MalVal)entry.Value, env)); | |
91 | } | |
92 | return new MalHashMap(new_dict); | |
93 | } else { | |
94 | return ast; | |
95 | } | |
96 | } | |
97 | ||
98 | ||
99 | static MalVal EVAL(MalVal orig_ast, Env env) { | |
100 | MalVal a0, a1, a2, res; | |
101 | MalList el; | |
102 | ||
103 | while (true) { | |
104 | ||
105 | //System.out.println("EVAL: " + printer._pr_str(orig_ast, true)); | |
106 | if (!orig_ast.list_Q()) { | |
107 | return eval_ast(orig_ast, env); | |
108 | } | |
109 | ||
110 | // apply list | |
111 | MalVal expanded = macroexpand(orig_ast, env); | |
112 | if (!expanded.list_Q()) { return expanded; } | |
113 | MalList ast = (MalList) expanded; | |
114 | ||
115 | if (ast.size() == 0) { return ast; } | |
116 | a0 = ast[0]; | |
117 | ||
118 | String a0sym = a0 is MalSymbol ? ((MalSymbol)a0).getName() | |
119 | : "__<*fn*>__"; | |
120 | ||
121 | switch (a0sym) { | |
122 | case "def!": | |
123 | a1 = ast[1]; | |
124 | a2 = ast[2]; | |
125 | res = EVAL(a2, env); | |
126 | env.set(((MalSymbol)a1).getName(), res); | |
127 | return res; | |
128 | case "let*": | |
129 | a1 = ast[1]; | |
130 | a2 = ast[2]; | |
131 | MalSymbol key; | |
132 | MalVal val; | |
133 | Env let_env = new Env(env); | |
134 | for(int i=0; i<((MalList)a1).size(); i+=2) { | |
135 | key = (MalSymbol)((MalList)a1)[i]; | |
136 | val = ((MalList)a1)[i+1]; | |
137 | let_env.set(key.getName(), EVAL(val, let_env)); | |
138 | } | |
6301e0b6 JM |
139 | orig_ast = a2; |
140 | env = let_env; | |
141 | break; | |
faee4d12 JM |
142 | case "quote": |
143 | return ast[1]; | |
144 | case "quasiquote": | |
6301e0b6 JM |
145 | orig_ast = quasiquote(ast[1]); |
146 | break; | |
faee4d12 JM |
147 | case "defmacro!": |
148 | a1 = ast[1]; | |
149 | a2 = ast[2]; | |
150 | res = EVAL(a2, env); | |
c3b508af | 151 | ((MalFunc)res).setMacro(); |
faee4d12 JM |
152 | env.set(((MalSymbol)a1).getName(), res); |
153 | return res; | |
154 | case "macroexpand": | |
155 | a1 = ast[1]; | |
156 | return macroexpand(a1, env); | |
157 | case "try*": | |
158 | try { | |
159 | return EVAL(ast[1], env); | |
160 | } catch (Exception e) { | |
161 | if (ast.size() > 2) { | |
162 | MalVal exc; | |
163 | a2 = ast[2]; | |
164 | MalVal a20 = ((MalList)a2)[0]; | |
165 | if (((MalSymbol)a20).getName() == "catch*") { | |
8cb5cda4 JM |
166 | if (e is Mal.types.MalException) { |
167 | exc = ((Mal.types.MalException)e).getValue(); | |
faee4d12 JM |
168 | } else { |
169 | exc = new MalString(e.StackTrace); | |
170 | } | |
171 | return EVAL(((MalList)a2)[2], | |
172 | new Env(env, ((MalList)a2).slice(1,2), | |
173 | new MalList(exc))); | |
174 | } | |
175 | } | |
176 | throw e; | |
177 | } | |
178 | case "do": | |
179 | eval_ast(ast.slice(1, ast.size()-1), env); | |
180 | orig_ast = ast[ast.size()-1]; | |
181 | break; | |
182 | case "if": | |
183 | a1 = ast[1]; | |
184 | MalVal cond = EVAL(a1, env); | |
8cb5cda4 | 185 | if (cond == Mal.types.Nil || cond == Mal.types.False) { |
faee4d12 JM |
186 | // eval false slot form |
187 | if (ast.size() > 3) { | |
188 | orig_ast = ast[3]; | |
189 | } else { | |
8cb5cda4 | 190 | return Mal.types.Nil; |
faee4d12 JM |
191 | } |
192 | } else { | |
193 | // eval true slot form | |
194 | orig_ast = ast[2]; | |
195 | } | |
196 | break; | |
197 | case "fn*": | |
198 | MalList a1f = (MalList)ast[1]; | |
199 | MalVal a2f = ast[2]; | |
200 | Env cur_env = env; | |
c3b508af | 201 | return new MalFunc(a2f, env, a1f, |
faee4d12 JM |
202 | args => EVAL(a2f, new Env(cur_env, a1f, args)) ); |
203 | default: | |
204 | el = (MalList)eval_ast(ast, env); | |
c3b508af | 205 | var f = (MalFunc)el[0]; |
faee4d12 JM |
206 | MalVal fnast = f.getAst(); |
207 | if (fnast != null) { | |
208 | orig_ast = fnast; | |
209 | env = f.genEnv(el.rest()); | |
210 | } else { | |
211 | return f.apply(el.rest()); | |
212 | } | |
213 | break; | |
214 | } | |
215 | ||
216 | } | |
217 | } | |
218 | ||
219 | ||
220 | static string PRINT(MalVal exp) { | |
221 | return printer._pr_str(exp, true); | |
222 | } | |
223 | ||
86b689f3 | 224 | // repl |
faee4d12 | 225 | static void Main(string[] args) { |
c3b508af JM |
226 | var repl_env = new Mal.env.Env(null); |
227 | Func<string, MalVal> RE = (string str) => EVAL(READ(str), repl_env); | |
faee4d12 | 228 | |
8cb5cda4 | 229 | // core.cs: defined using C# |
faee4d12 | 230 | foreach (var entry in core.ns) { |
8cb5cda4 | 231 | repl_env.set(entry.Key, entry.Value); |
faee4d12 | 232 | } |
c3b508af | 233 | repl_env.set("eval", new MalFunc(a => EVAL(a[0], repl_env))); |
86b689f3 JM |
234 | MalList _argv = new MalList(); |
235 | for (int i=1; i < args.Length; i++) { | |
236 | _argv.conj_BANG(new MalString(args[i])); | |
237 | } | |
238 | repl_env.set("*ARGV*", _argv); | |
faee4d12 | 239 | |
8cb5cda4 | 240 | // core.mal: defined using the language itself |
c3b508af JM |
241 | RE("(def! *host-language* \"c#\")"); |
242 | RE("(def! not (fn* (a) (if a false true)))"); | |
243 | RE("(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))"); | |
244 | RE("(defmacro! cond (fn* (& xs) (if (> (count xs) 0) (list 'if (first xs) (if (> (count xs) 1) (nth xs 1) (throw \"odd number of forms to cond\")) (cons 'cond (rest (rest xs)))))))"); | |
245 | RE("(defmacro! or (fn* (& xs) (if (empty? xs) nil (if (= 1 (count xs)) (first xs) `(let* (or_FIXME ~(first xs)) (if or_FIXME or_FIXME (or ~@(rest xs))))))))"); | |
faee4d12 JM |
246 | |
247 | int fileIdx = 0; | |
248 | if (args.Length > 0 && args[0] == "--raw") { | |
8cb5cda4 | 249 | Mal.readline.mode = Mal.readline.Mode.Raw; |
faee4d12 JM |
250 | fileIdx = 1; |
251 | } | |
252 | if (args.Length > fileIdx) { | |
c3b508af | 253 | RE("(load-file \"" + args[fileIdx] + "\")"); |
faee4d12 JM |
254 | return; |
255 | } | |
c3b508af | 256 | |
86b689f3 | 257 | // repl loop |
c3b508af | 258 | RE("(println (str \"Mal [\" *host-language* \"]\"))"); |
faee4d12 JM |
259 | while (true) { |
260 | string line; | |
261 | try { | |
c3b508af | 262 | line = Mal.readline.Readline("user> "); |
faee4d12 | 263 | if (line == null) { break; } |
c3b508af | 264 | if (line == "") { continue; } |
faee4d12 JM |
265 | } catch (IOException e) { |
266 | Console.WriteLine("IOException: " + e.Message); | |
267 | break; | |
268 | } | |
269 | try { | |
c3b508af | 270 | Console.WriteLine(PRINT(RE(line))); |
8cb5cda4 | 271 | } catch (Mal.types.MalContinue) { |
faee4d12 | 272 | continue; |
8cb5cda4 | 273 | } catch (Mal.types.MalException e) { |
faee4d12 JM |
274 | Console.WriteLine("Error: " + |
275 | printer._pr_str(e.getValue(), false)); | |
276 | continue; | |
277 | } catch (Exception e) { | |
278 | Console.WriteLine("Error: " + e.Message); | |
279 | Console.WriteLine(e.StackTrace); | |
280 | continue; | |
281 | } | |
282 | } | |
283 | } | |
284 | } | |
285 | } |