Merge pull request #153 from dubek/forth-hash-map-equality
[jackhill/mal.git] / cs / step9_try.cs
CommitLineData
faee4d12
JM
1using System;
2using System.IO;
3using System.Collections;
4using System.Collections.Generic;
5using Mal;
faee4d12
JM
6using MalVal = Mal.types.MalVal;
7using MalString = Mal.types.MalString;
8using MalSymbol = Mal.types.MalSymbol;
c3b508af 9using MalInt = Mal.types.MalInt;
faee4d12
JM
10using MalList = Mal.types.MalList;
11using MalVector = Mal.types.MalVector;
12using MalHashMap = Mal.types.MalHashMap;
c3b508af 13using MalFunc = Mal.types.MalFunc;
faee4d12
JM
14using Env = Mal.env.Env;
15
16namespace Mal {
01c97316 17 class step9_try {
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 &&
b8ee29b2
JM
55 env.find((MalSymbol)a0) != null) {
56 MalVal mac = env.get((MalSymbol)a0);
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];
b8ee29b2 69 MalFunc mac = (MalFunc) env.get(a0);
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) {
b8ee29b2 77 return env.get((MalSymbol)ast);
faee4d12
JM
78 } else if (ast is MalList) {
79 MalList old_lst = (MalList)ast;
80 MalList new_lst = ast.list_Q() ? new MalList()
81 : (MalList)new MalVector();
82 foreach (MalVal mv in old_lst.getValue()) {
83 new_lst.conj_BANG(EVAL(mv, env));
84 }
85 return new_lst;
86 } else if (ast is MalHashMap) {
87 var new_dict = new Dictionary<string, MalVal>();
88 foreach (var entry in ((MalHashMap)ast).getValue()) {
89 new_dict.Add(entry.Key, EVAL((MalVal)entry.Value, env));
90 }
91 return new MalHashMap(new_dict);
92 } else {
93 return ast;
94 }
95 }
96
97
98 static MalVal EVAL(MalVal orig_ast, Env env) {
99 MalVal a0, a1, a2, res;
100 MalList el;
101
102 while (true) {
103
b8ee29b2 104 //Console.WriteLine("EVAL: " + printer._pr_str(orig_ast, true));
faee4d12
JM
105 if (!orig_ast.list_Q()) {
106 return eval_ast(orig_ast, env);
107 }
108
109 // apply list
110 MalVal expanded = macroexpand(orig_ast, env);
111 if (!expanded.list_Q()) { return expanded; }
112 MalList ast = (MalList) expanded;
113
114 if (ast.size() == 0) { return ast; }
115 a0 = ast[0];
116
117 String a0sym = a0 is MalSymbol ? ((MalSymbol)a0).getName()
118 : "__<*fn*>__";
119
120 switch (a0sym) {
121 case "def!":
122 a1 = ast[1];
123 a2 = ast[2];
124 res = EVAL(a2, env);
b8ee29b2 125 env.set((MalSymbol)a1, res);
faee4d12
JM
126 return res;
127 case "let*":
128 a1 = ast[1];
129 a2 = ast[2];
130 MalSymbol key;
131 MalVal val;
132 Env let_env = new Env(env);
133 for(int i=0; i<((MalList)a1).size(); i+=2) {
134 key = (MalSymbol)((MalList)a1)[i];
135 val = ((MalList)a1)[i+1];
b8ee29b2 136 let_env.set(key, EVAL(val, let_env));
faee4d12 137 }
6301e0b6
JM
138 orig_ast = a2;
139 env = let_env;
140 break;
faee4d12
JM
141 case "quote":
142 return ast[1];
143 case "quasiquote":
6301e0b6
JM
144 orig_ast = quasiquote(ast[1]);
145 break;
faee4d12
JM
146 case "defmacro!":
147 a1 = ast[1];
148 a2 = ast[2];
149 res = EVAL(a2, env);
c3b508af 150 ((MalFunc)res).setMacro();
b8ee29b2 151 env.set(((MalSymbol)a1), res);
faee4d12
JM
152 return res;
153 case "macroexpand":
154 a1 = ast[1];
155 return macroexpand(a1, env);
156 case "try*":
157 try {
158 return EVAL(ast[1], env);
159 } catch (Exception e) {
160 if (ast.size() > 2) {
161 MalVal exc;
162 a2 = ast[2];
163 MalVal a20 = ((MalList)a2)[0];
164 if (((MalSymbol)a20).getName() == "catch*") {
8cb5cda4
JM
165 if (e is Mal.types.MalException) {
166 exc = ((Mal.types.MalException)e).getValue();
faee4d12
JM
167 } else {
168 exc = new MalString(e.StackTrace);
169 }
170 return EVAL(((MalList)a2)[2],
171 new Env(env, ((MalList)a2).slice(1,2),
172 new MalList(exc)));
173 }
174 }
175 throw e;
176 }
177 case "do":
178 eval_ast(ast.slice(1, ast.size()-1), env);
179 orig_ast = ast[ast.size()-1];
180 break;
181 case "if":
182 a1 = ast[1];
183 MalVal cond = EVAL(a1, env);
8cb5cda4 184 if (cond == Mal.types.Nil || cond == Mal.types.False) {
faee4d12
JM
185 // eval false slot form
186 if (ast.size() > 3) {
187 orig_ast = ast[3];
188 } else {
8cb5cda4 189 return Mal.types.Nil;
faee4d12
JM
190 }
191 } else {
192 // eval true slot form
193 orig_ast = ast[2];
194 }
195 break;
196 case "fn*":
197 MalList a1f = (MalList)ast[1];
198 MalVal a2f = ast[2];
199 Env cur_env = env;
c3b508af 200 return new MalFunc(a2f, env, a1f,
faee4d12
JM
201 args => EVAL(a2f, new Env(cur_env, a1f, args)) );
202 default:
203 el = (MalList)eval_ast(ast, env);
c3b508af 204 var f = (MalFunc)el[0];
faee4d12
JM
205 MalVal fnast = f.getAst();
206 if (fnast != null) {
207 orig_ast = fnast;
208 env = f.genEnv(el.rest());
209 } else {
210 return f.apply(el.rest());
211 }
212 break;
213 }
214
215 }
216 }
217
218 // print
219 static string PRINT(MalVal exp) {
220 return printer._pr_str(exp, true);
221 }
222
86b689f3 223 // repl
faee4d12 224 static void Main(string[] args) {
c3b508af
JM
225 var repl_env = new Mal.env.Env(null);
226 Func<string, MalVal> RE = (string str) => EVAL(READ(str), repl_env);
faee4d12 227
8cb5cda4 228 // core.cs: defined using C#
faee4d12 229 foreach (var entry in core.ns) {
b8ee29b2 230 repl_env.set(new MalSymbol(entry.Key), entry.Value);
faee4d12 231 }
b8ee29b2
JM
232 repl_env.set(new MalSymbol("eval"), new MalFunc(
233 a => EVAL(a[0], repl_env)));
dbbac62f 234 int fileIdx = 0;
aaba2493
JM
235 if (args.Length > 0 && args[0] == "--raw") {
236 Mal.readline.mode = Mal.readline.Mode.Raw;
dbbac62f 237 fileIdx = 1;
aaba2493 238 }
86b689f3 239 MalList _argv = new MalList();
dbbac62f 240 for (int i=fileIdx+1; i < args.Length; i++) {
86b689f3
JM
241 _argv.conj_BANG(new MalString(args[i]));
242 }
b8ee29b2 243 repl_env.set(new MalSymbol("*ARGV*"), _argv);
faee4d12 244
8cb5cda4 245 // core.mal: defined using the language itself
c3b508af
JM
246 RE("(def! not (fn* (a) (if a false true)))");
247 RE("(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))");
248 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)))))))");
249 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 250
faee4d12 251 if (args.Length > fileIdx) {
c3b508af 252 RE("(load-file \"" + args[fileIdx] + "\")");
faee4d12
JM
253 return;
254 }
c3b508af 255
86b689f3 256 // repl loop
faee4d12
JM
257 while (true) {
258 string line;
259 try {
c3b508af 260 line = Mal.readline.Readline("user> ");
faee4d12 261 if (line == null) { break; }
c3b508af 262 if (line == "") { continue; }
faee4d12
JM
263 } catch (IOException e) {
264 Console.WriteLine("IOException: " + e.Message);
265 break;
266 }
267 try {
c3b508af 268 Console.WriteLine(PRINT(RE(line)));
8cb5cda4 269 } catch (Mal.types.MalContinue) {
faee4d12 270 continue;
8cb5cda4 271 } catch (Mal.types.MalException e) {
faee4d12
JM
272 Console.WriteLine("Error: " +
273 printer._pr_str(e.getValue(), false));
274 continue;
275 } catch (Exception e) {
276 Console.WriteLine("Error: " + e.Message);
277 Console.WriteLine(e.StackTrace);
278 continue;
279 }
280 }
281 }
282 }
283}