3 using System
.Collections
;
4 using System
.Collections
.Generic
;
6 using MalVal
= Mal
.types
.MalVal
;
7 using MalString
= Mal
.types
.MalString
;
8 using MalSymbol
= Mal
.types
.MalSymbol
;
9 using MalInteger
= Mal
.types
.MalInteger
;
10 using MalList
= Mal
.types
.MalList
;
11 using MalVector
= Mal
.types
.MalVector
;
12 using MalHashMap
= Mal
.types
.MalHashMap
;
13 using MalFunction
= Mal
.types
.MalFunction
;
14 using Env
= Mal
.env
.Env
;
19 static MalVal
READ(string str
) {
20 return reader
.read_str(str
);
24 public static bool is_pair(MalVal x
) {
25 return x
is MalList
&& ((MalList
)x
).size() > 0;
28 public static MalVal
quasiquote(MalVal ast
) {
30 return new MalList(new MalSymbol("quote"), ast
);
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"),
42 quasiquote(((MalList
)ast
).rest()));
45 return new MalList(new MalSymbol("cons"),
47 quasiquote(((MalList
)ast
).rest()));
51 static MalVal
eval_ast(MalVal ast
, Env env
) {
52 if (ast
is MalSymbol
) {
53 MalSymbol sym
= (MalSymbol
)ast
;
54 return env
.get(sym
.getName());
55 } else if (ast
is MalList
) {
56 MalList old_lst
= (MalList
)ast
;
57 MalList new_lst
= ast
.list_Q() ? new MalList()
58 : (MalList
)new MalVector();
59 foreach (MalVal mv
in old_lst
.getValue()) {
60 new_lst
.conj_BANG(EVAL(mv
, env
));
63 } else if (ast
is MalHashMap
) {
64 var new_dict
= new Dictionary
<string, MalVal
>();
65 foreach (var entry
in ((MalHashMap
)ast
).getValue()) {
66 new_dict
.Add(entry
.Key
, EVAL((MalVal
)entry
.Value
, env
));
68 return new MalHashMap(new_dict
);
75 static MalVal
EVAL(MalVal orig_ast
, Env env
) {
76 MalVal a0
, a1
, a2
, res
;
81 //System.out.println("EVAL: " + printer._pr_str(orig_ast, true));
82 if (!orig_ast
.list_Q()) {
83 return eval_ast(orig_ast
, env
);
87 MalList ast
= (MalList
)orig_ast
;
88 if (ast
.size() == 0) { return ast; }
91 String a0sym
= a0
is MalSymbol
? ((MalSymbol
)a0
).getName()
99 env
.set(((MalSymbol
)a1
).getName(), res
);
106 Env let_env
= new Env(env
);
107 for(int i
=0; i
<((MalList
)a1
).size(); i
+=2) {
108 key
= (MalSymbol
)((MalList
)a1
)[i
];
109 val
= ((MalList
)a1
)[i
+1];
110 let_env
.set(key
.getName(), EVAL(val
, let_env
));
112 return EVAL(a2
, let_env
);
116 return EVAL(quasiquote(ast
[1]), env
);
118 eval_ast(ast
.slice(1, ast
.size()-1), env
);
119 orig_ast
= ast
[ast
.size()-1];
123 MalVal cond
= EVAL(a1
, env
);
124 if (cond
== Mal
.types
.Nil
|| cond
== Mal
.types
.False
) {
125 // eval false slot form
126 if (ast
.size() > 3) {
129 return Mal
.types
.Nil
;
132 // eval true slot form
137 MalList a1f
= (MalList
)ast
[1];
140 return new MalFunction(a2f
, env
, a1f
,
141 args
=> EVAL(a2f
, new Env(cur_env
, a1f
, args
)) );
143 el
= (MalList
)eval_ast(ast
, env
);
144 var f
= (MalFunction
)el
[0];
145 MalVal fnast
= f
.getAst();
148 env
= f
.genEnv(el
.rest());
150 return f
.apply(el
.rest());
159 static string PRINT(MalVal exp
) {
160 return printer
._pr_str(exp
, true);
164 static MalVal
RE(Env env
, string str
) {
165 return EVAL(READ(str
), env
);
168 static void Main(string[] args
) {
169 string prompt
= "user> ";
171 // core.cs: defined using C#
172 var repl_env
= new env
.Env(null);
173 foreach (var entry
in core
.ns
) {
174 repl_env
.set(entry
.Key
, entry
.Value
);
176 repl_env
.set("eval", new MalFunction(a
=> EVAL(a
[0], repl_env
)));
178 // core.mal: defined using the language itself
179 RE(repl_env
, "(def! not (fn* (a) (if a false true)))");
180 RE(repl_env
, "(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))");
183 if (args
.Length
> 0 && args
[0] == "--raw") {
184 Mal
.readline
.mode
= Mal
.readline
.Mode
.Raw
;
187 if (args
.Length
> fileIdx
) {
188 for(int i
=fileIdx
; i
<args
.Length
; i
++) {
189 RE(repl_env
, "(load-file \"" + args
[i
] + "\")");
196 line
= Mal
.readline
.Readline(prompt
);
197 if (line
== null) { break; }
198 } catch (IOException e
) {
199 Console
.WriteLine("IOException: " + e
.Message
);
203 Console
.WriteLine(PRINT(RE(repl_env
, line
)));
204 } catch (Mal
.types
.MalContinue
) {
206 } catch (Exception e
) {
207 Console
.WriteLine("Error: " + e
.Message
);
208 Console
.WriteLine(e
.StackTrace
);