3 using System
.Collections
;
4 using System
.Collections
.Generic
;
6 using MalVal
= Mal
.types
.MalVal
;
7 using MalSymbol
= Mal
.types
.MalSymbol
;
8 using MalInt
= Mal
.types
.MalInt
;
9 using MalList
= Mal
.types
.MalList
;
10 using MalVector
= Mal
.types
.MalVector
;
11 using MalHashMap
= Mal
.types
.MalHashMap
;
12 using MalFunc
= Mal
.types
.MalFunc
;
17 static MalVal
READ(string str
) {
18 return reader
.read_str(str
);
22 static MalVal
eval_ast(MalVal ast
, Dictionary
<string, MalVal
> env
) {
23 if (ast
is MalSymbol
) {
24 MalSymbol sym
= (MalSymbol
)ast
;
25 return (MalVal
)env
[sym
.getName()];
26 } else if (ast
is MalList
) {
27 MalList old_lst
= (MalList
)ast
;
28 MalList new_lst
= ast
.list_Q() ? new MalList()
29 : (MalList
)new MalVector();
30 foreach (MalVal mv
in old_lst
.getValue()) {
31 new_lst
.conj_BANG(EVAL(mv
, env
));
34 } else if (ast
is MalHashMap
) {
35 var new_dict
= new Dictionary
<string, MalVal
>();
36 foreach (var entry
in ((MalHashMap
)ast
).getValue()) {
37 new_dict
.Add(entry
.Key
, EVAL((MalVal
)entry
.Value
, env
));
39 return new MalHashMap(new_dict
);
46 static MalVal
EVAL(MalVal orig_ast
, Dictionary
<string, MalVal
> env
) {
48 //Console.WriteLine("EVAL: " + printer._pr_str(orig_ast, true));
49 if (!orig_ast
.list_Q()) {
50 return eval_ast(orig_ast
, env
);
54 MalList ast
= (MalList
)orig_ast
;
55 if (ast
.size() == 0) { return ast; }
57 if (!(a0
is MalSymbol
)) {
58 throw new Mal
.types
.MalError("attempt to apply on non-symbol '"
59 + Mal
.printer
._pr_str(a0
,true) + "'");
61 var el
= (MalList
)eval_ast(ast
, env
);
62 var f
= (MalFunc
)el
[0];
63 return f
.apply(el
.rest());
68 static string PRINT(MalVal exp
) {
69 return printer
._pr_str(exp
, true);
73 static void Main(string[] args
) {
74 var repl_env
= new Dictionary
<string, MalVal
> {
75 {"+", new MalFunc(a => (MalInt)a[0] + (MalInt)a[1]) }
,
76 {"-", new MalFunc(a => (MalInt)a[0] - (MalInt)a[1]) }
,
77 {"*", new MalFunc(a => (MalInt)a[0] * (MalInt)a[1]) }
,
78 {"/", new MalFunc(a => (MalInt)a[0] / (MalInt)a[1]) }
,
80 Func
<string, MalVal
> RE
= (string str
) => EVAL(READ(str
), repl_env
);
82 if (args
.Length
> 0 && args
[0] == "--raw") {
83 Mal
.readline
.mode
= Mal
.readline
.Mode
.Raw
;
90 line
= Mal
.readline
.Readline("user> ");
91 if (line
== null) { break; }
92 if (line
== "") { continue; }
93 } catch (IOException e
) {
94 Console
.WriteLine("IOException: " + e
.Message
);
98 Console
.WriteLine(PRINT(RE(line
)));
99 } catch (Mal
.types
.MalContinue
) {
101 } catch (Exception e
) {
102 Console
.WriteLine("Error: " + e
.Message
);
103 Console
.WriteLine(e
.StackTrace
);