Merge pull request #378 from asarhaddon/test-macro-not-changing-function
[jackhill/mal.git] / cs / step6_file.cs
1 using System;
2 using System.IO;
3 using System.Collections;
4 using System.Collections.Generic;
5 using Mal;
6 using MalVal = Mal.types.MalVal;
7 using MalString = Mal.types.MalString;
8 using MalSymbol = Mal.types.MalSymbol;
9 using MalInt = Mal.types.MalInt;
10 using MalList = Mal.types.MalList;
11 using MalVector = Mal.types.MalVector;
12 using MalHashMap = Mal.types.MalHashMap;
13 using MalFunc = Mal.types.MalFunc;
14 using Env = Mal.env.Env;
15
16 namespace Mal {
17 class step6_file {
18 // read
19 static MalVal READ(string str) {
20 return reader.read_str(str);
21 }
22
23 // eval
24 static MalVal eval_ast(MalVal ast, Env env) {
25 if (ast is MalSymbol) {
26 return env.get((MalSymbol)ast);
27 } else if (ast is MalList) {
28 MalList old_lst = (MalList)ast;
29 MalList new_lst = ast.list_Q() ? new MalList()
30 : (MalList)new MalVector();
31 foreach (MalVal mv in old_lst.getValue()) {
32 new_lst.conj_BANG(EVAL(mv, env));
33 }
34 return new_lst;
35 } else if (ast is MalHashMap) {
36 var new_dict = new Dictionary<string, MalVal>();
37 foreach (var entry in ((MalHashMap)ast).getValue()) {
38 new_dict.Add(entry.Key, EVAL((MalVal)entry.Value, env));
39 }
40 return new MalHashMap(new_dict);
41 } else {
42 return ast;
43 }
44 }
45
46
47 static MalVal EVAL(MalVal orig_ast, Env env) {
48 MalVal a0, a1, a2, res;
49 MalList el;
50
51 while (true) {
52
53 //Console.WriteLine("EVAL: " + printer._pr_str(orig_ast, true));
54 if (!orig_ast.list_Q()) {
55 return eval_ast(orig_ast, env);
56 }
57
58 // apply list
59 MalList ast = (MalList)orig_ast;
60 if (ast.size() == 0) { return ast; }
61 a0 = ast[0];
62
63 String a0sym = a0 is MalSymbol ? ((MalSymbol)a0).getName()
64 : "__<*fn*>__";
65
66 switch (a0sym) {
67 case "def!":
68 a1 = ast[1];
69 a2 = ast[2];
70 res = EVAL(a2, env);
71 env.set((MalSymbol)a1, res);
72 return res;
73 case "let*":
74 a1 = ast[1];
75 a2 = ast[2];
76 MalSymbol key;
77 MalVal val;
78 Env let_env = new Env(env);
79 for(int i=0; i<((MalList)a1).size(); i+=2) {
80 key = (MalSymbol)((MalList)a1)[i];
81 val = ((MalList)a1)[i+1];
82 let_env.set(key, EVAL(val, let_env));
83 }
84 orig_ast = a2;
85 env = let_env;
86 break;
87 case "do":
88 eval_ast(ast.slice(1, ast.size()-1), env);
89 orig_ast = ast[ast.size()-1];
90 break;
91 case "if":
92 a1 = ast[1];
93 MalVal cond = EVAL(a1, env);
94 if (cond == Mal.types.Nil || cond == Mal.types.False) {
95 // eval false slot form
96 if (ast.size() > 3) {
97 orig_ast = ast[3];
98 } else {
99 return Mal.types.Nil;
100 }
101 } else {
102 // eval true slot form
103 orig_ast = ast[2];
104 }
105 break;
106 case "fn*":
107 MalList a1f = (MalList)ast[1];
108 MalVal a2f = ast[2];
109 Env cur_env = env;
110 return new MalFunc(a2f, env, a1f,
111 args => EVAL(a2f, new Env(cur_env, a1f, args)) );
112 default:
113 el = (MalList)eval_ast(ast, env);
114 var f = (MalFunc)el[0];
115 MalVal fnast = f.getAst();
116 if (fnast != null) {
117 orig_ast = fnast;
118 env = f.genEnv(el.rest());
119 } else {
120 return f.apply(el.rest());
121 }
122 break;
123 }
124
125 }
126 }
127
128 // print
129 static string PRINT(MalVal exp) {
130 return printer._pr_str(exp, true);
131 }
132
133 // repl
134 static void Main(string[] args) {
135 var repl_env = new Mal.env.Env(null);
136 Func<string, MalVal> RE = (string str) => EVAL(READ(str), repl_env);
137
138 // core.cs: defined using C#
139 foreach (var entry in core.ns) {
140 repl_env.set(new MalSymbol(entry.Key), entry.Value);
141 }
142 repl_env.set(new MalSymbol("eval"), new MalFunc(
143 a => EVAL(a[0], repl_env)));
144 int fileIdx = 0;
145 if (args.Length > 0 && args[0] == "--raw") {
146 Mal.readline.mode = Mal.readline.Mode.Raw;
147 fileIdx = 1;
148 }
149 MalList _argv = new MalList();
150 for (int i=fileIdx+1; i < args.Length; i++) {
151 _argv.conj_BANG(new MalString(args[i]));
152 }
153 repl_env.set(new MalSymbol("*ARGV*"), _argv);
154
155 // core.mal: defined using the language itself
156 RE("(def! not (fn* (a) (if a false true)))");
157 RE("(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))");
158
159 if (args.Length > fileIdx) {
160 RE("(load-file \"" + args[fileIdx] + "\")");
161 return;
162 }
163
164 // repl loop
165 while (true) {
166 string line;
167 try {
168 line = Mal.readline.Readline("user> ");
169 if (line == null) { break; }
170 if (line == "") { continue; }
171 } catch (IOException e) {
172 Console.WriteLine("IOException: " + e.Message);
173 break;
174 }
175 try {
176 Console.WriteLine(PRINT(RE(line)));
177 } catch (Mal.types.MalContinue) {
178 continue;
179 } catch (Exception e) {
180 Console.WriteLine("Error: " + e.Message);
181 Console.WriteLine(e.StackTrace);
182 continue;
183 }
184 }
185 }
186 }
187 }