forked from kanaka/mal
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstep2_eval.d
More file actions
136 lines (123 loc) Β· 3.14 KB
/
step2_eval.d
File metadata and controls
136 lines (123 loc) Β· 3.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import std.algorithm;
import std.array;
import std.stdio;
import std.string;
import readline;
import reader;
import printer;
import types;
alias Env = MalType[string];
MalType READ(string str)
{
return read_str(str);
}
MalType eval_ast(MalType ast, Env env)
{
if (typeid(ast) == typeid(MalSymbol))
{
MalSymbol sym = verify_cast!MalSymbol(ast);
auto v = (sym.name in env);
if (v is null) throw new Exception("'" ~ sym.name ~ "' not found");
return *v;
}
else if (typeid(ast) == typeid(MalList))
{
auto lst = verify_cast!MalList(ast);
auto el = array(lst.elements.map!(e => EVAL(e, env)));
return new MalList(el);
}
else if (typeid(ast) == typeid(MalVector))
{
auto lst = verify_cast!MalVector(ast);
auto el = array(lst.elements.map!(e => EVAL(e, env)));
return new MalVector(el);
}
else if (typeid(ast) == typeid(MalHashmap))
{
auto hm = verify_cast!MalHashmap(ast);
typeof(hm.data) new_data;
foreach (string k, MalType v; hm.data)
{
new_data[k] = EVAL(v, env);
}
return new MalHashmap(new_data);
}
else
{
return ast;
}
}
MalType EVAL(MalType ast, Env env)
{
if (typeid(ast) != typeid(MalList))
{
return eval_ast(ast, env);
}
if ((cast(MalList) ast).elements.length == 0)
{
return ast;
}
auto el = verify_cast!MalList(eval_ast(ast, env));
auto fobj = verify_cast!MalBuiltinFunc(el.elements[0]);
auto args = el.elements[1..$];
return fobj.fn(args);
}
string PRINT(MalType ast)
{
return pr_str(ast);
}
string rep(string str, Env env)
{
return PRINT(EVAL(READ(str), env));
}
static MalType mal_add(MalType[] a ...)
{
verify_args_count(a, 2);
MalInteger i0 = verify_cast!MalInteger(a[0]);
MalInteger i1 = verify_cast!MalInteger(a[1]);
return new MalInteger(i0.val + i1.val);
}
static MalType mal_sub(MalType[] a ...)
{
verify_args_count(a, 2);
MalInteger i0 = verify_cast!MalInteger(a[0]);
MalInteger i1 = verify_cast!MalInteger(a[1]);
return new MalInteger(i0.val - i1.val);
}
static MalType mal_mul(MalType[] a ...)
{
verify_args_count(a, 2);
MalInteger i0 = verify_cast!MalInteger(a[0]);
MalInteger i1 = verify_cast!MalInteger(a[1]);
return new MalInteger(i0.val * i1.val);
}
static MalType mal_div(MalType[] a ...)
{
verify_args_count(a, 2);
MalInteger i0 = verify_cast!MalInteger(a[0]);
MalInteger i1 = verify_cast!MalInteger(a[1]);
return new MalInteger(i0.val / i1.val);
}
void main()
{
Env repl_env;
repl_env["+"] = new MalBuiltinFunc(&mal_add, "+");
repl_env["-"] = new MalBuiltinFunc(&mal_sub, "-");
repl_env["*"] = new MalBuiltinFunc(&mal_mul, "*");
repl_env["/"] = new MalBuiltinFunc(&mal_div, "/");
for (;;)
{
string line = _readline("user> ");
if (line is null) break;
if (line.length == 0) continue;
try
{
writeln(rep(line, repl_env));
}
catch (Exception e)
{
writeln("Error: ", e.msg);
}
}
writeln("");
}