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 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
;
19 static MalVal
READ(string str
) {
20 return reader
.read_str(str
);
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
));
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
));
40 return new MalHashMap(new_dict
);
47 static MalVal
EVAL(MalVal orig_ast
, Env env
) {
48 MalVal a0
, a1
, a2
, res
;
53 //Console.WriteLine("EVAL: " + printer._pr_str(orig_ast, true));
54 if (!orig_ast
.list_Q()) {
55 return eval_ast(orig_ast
, env
);
59 MalList ast
= (MalList
)orig_ast
;
60 if (ast
.size() == 0) { return ast; }
63 String a0sym
= a0
is MalSymbol
? ((MalSymbol
)a0
).getName()
71 env
.set((MalSymbol
)a1
, res
);
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
));
88 eval_ast(ast
.slice(1, ast
.size()-1), env
);
89 orig_ast
= ast
[ast
.size()-1];
93 MalVal cond
= EVAL(a1
, env
);
94 if (cond
== Mal
.types
.Nil
|| cond
== Mal
.types
.False
) {
95 // eval false slot form
102 // eval true slot form
107 MalList a1f
= (MalList
)ast
[1];
110 return new MalFunc(a2f
, env
, a1f
,
111 args
=> EVAL(a2f
, new Env(cur_env
, a1f
, args
)) );
113 el
= (MalList
)eval_ast(ast
, env
);
114 var f
= (MalFunc
)el
[0];
115 MalVal fnast
= f
.getAst();
118 env
= f
.genEnv(el
.rest());
120 return f
.apply(el
.rest());
129 static string PRINT(MalVal exp
) {
130 return printer
._pr_str(exp
, true);
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
);
138 // core.cs: defined using C#
139 foreach (var entry
in core
.ns
) {
140 repl_env
.set(new MalSymbol(entry
.Key
), entry
.Value
);
142 repl_env
.set(new MalSymbol("eval"), new MalFunc(
143 a
=> EVAL(a
[0], repl_env
)));
145 if (args
.Length
> 0 && args
[0] == "--raw") {
146 Mal
.readline
.mode
= Mal
.readline
.Mode
.Raw
;
149 MalList _argv
= new MalList();
150 for (int i
=fileIdx
+1; i
< args
.Length
; i
++) {
151 _argv
.conj_BANG(new MalString(args
[i
]));
153 repl_env
.set(new MalSymbol("*ARGV*"), _argv
);
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) \")\")))))");
159 if (args
.Length
> fileIdx
) {
160 RE("(load-file \"" + args
[fileIdx
] + "\")");
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
);
176 Console
.WriteLine(PRINT(RE(line
)));
177 } catch (Mal
.types
.MalContinue
) {
179 } catch (Exception e
) {
180 Console
.WriteLine("Error: " + e
.Message
);
181 Console
.WriteLine(e
.StackTrace
);