Perl: add vector, hash-map, metadata, atom support. TCO let*
[jackhill/mal.git] / cs / step8_macros.cs
CommitLineData
faa20db2
JM
1using System;
2using System.IO;
3using System.Collections;
4using System.Collections.Generic;
5using Mal;
6using MalVal = Mal.types.MalVal;
7using MalString = Mal.types.MalString;
8using MalSymbol = Mal.types.MalSymbol;
9using MalInteger = Mal.types.MalInteger;
10using MalList = Mal.types.MalList;
11using MalVector = Mal.types.MalVector;
12using MalHashMap = Mal.types.MalHashMap;
13using MalFunction = Mal.types.MalFunction;
14using Env = Mal.env.Env;
15
16namespace Mal {
8cb5cda4 17 class step8_macros {
faa20db2
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());
57 if (mac is MalFunction &&
58 ((MalFunction)mac).isMacro()) {
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];
69 MalFunction mac = (MalFunction) env.get(a0.getName());
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 }
139 return EVAL(a2, let_env);
140 case "quote":
141 return ast[1];
142 case "quasiquote":
143 return EVAL(quasiquote(ast[1]), env);
144 case "defmacro!":
145 a1 = ast[1];
146 a2 = ast[2];
147 res = EVAL(a2, env);
148 ((MalFunction)res).setMacro();
149 env.set(((MalSymbol)a1).getName(), res);
150 return res;
151 case "macroexpand":
152 a1 = ast[1];
153 return macroexpand(a1, env);
154 case "do":
155 eval_ast(ast.slice(1, ast.size()-1), env);
156 orig_ast = ast[ast.size()-1];
157 break;
158 case "if":
159 a1 = ast[1];
160 MalVal cond = EVAL(a1, env);
161 if (cond == Mal.types.Nil || cond == Mal.types.False) {
162 // eval false slot form
163 if (ast.size() > 3) {
164 orig_ast = ast[3];
165 } else {
166 return Mal.types.Nil;
167 }
168 } else {
169 // eval true slot form
170 orig_ast = ast[2];
171 }
172 break;
173 case "fn*":
174 MalList a1f = (MalList)ast[1];
175 MalVal a2f = ast[2];
176 Env cur_env = env;
177 return new MalFunction(a2f, env, a1f,
178 args => EVAL(a2f, new Env(cur_env, a1f, args)) );
179 default:
180 el = (MalList)eval_ast(ast, env);
181 var f = (MalFunction)el[0];
182 MalVal fnast = f.getAst();
183 if (fnast != null) {
184 orig_ast = fnast;
185 env = f.genEnv(el.rest());
186 } else {
187 return f.apply(el.rest());
188 }
189 break;
190 }
191
192 }
193 }
194
195 // print
196 static string PRINT(MalVal exp) {
197 return printer._pr_str(exp, true);
198 }
199
86b689f3 200 // repl
faa20db2
JM
201 static MalVal RE(Env env, string str) {
202 return EVAL(READ(str), env);
203 }
faa20db2
JM
204
205 static void Main(string[] args) {
206 string prompt = "user> ";
207
8cb5cda4
JM
208 // core.cs: defined using C#
209 var repl_env = new env.Env(null);
210 foreach (var entry in core.ns) {
211 repl_env.set(entry.Key, entry.Value);
faa20db2 212 }
8cb5cda4 213 repl_env.set("eval", new MalFunction(a => EVAL(a[0], repl_env)));
86b689f3
JM
214 MalList _argv = new MalList();
215 for (int i=1; i < args.Length; i++) {
216 _argv.conj_BANG(new MalString(args[i]));
217 }
218 repl_env.set("*ARGV*", _argv);
faa20db2 219
8cb5cda4 220 // core.mal: defined using the language itself
faa20db2
JM
221 RE(repl_env, "(def! not (fn* (a) (if a false true)))");
222 RE(repl_env, "(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))");
8cb5cda4
JM
223 RE(repl_env, "(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)))))))");
224 RE(repl_env, "(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))))))))");
faa20db2
JM
225
226 int fileIdx = 0;
227 if (args.Length > 0 && args[0] == "--raw") {
228 Mal.readline.mode = Mal.readline.Mode.Raw;
229 fileIdx = 1;
230 }
231 if (args.Length > fileIdx) {
86b689f3 232 RE(repl_env, "(load-file \"" + args[fileIdx] + "\")");
faa20db2
JM
233 return;
234 }
86b689f3
JM
235
236 // repl loop
faa20db2
JM
237 while (true) {
238 string line;
239 try {
240 line = Mal.readline.Readline(prompt);
241 if (line == null) { break; }
242 } catch (IOException e) {
243 Console.WriteLine("IOException: " + e.Message);
244 break;
245 }
246 try {
247 Console.WriteLine(PRINT(RE(repl_env, line)));
248 } catch (Mal.types.MalContinue) {
249 continue;
faa20db2
JM
250 } catch (Exception e) {
251 Console.WriteLine("Error: " + e.Message);
252 Console.WriteLine(e.StackTrace);
253 continue;
254 }
255 }
256 }
257 }
258}