forked from kanaka/mal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
step3_env.ck
226 lines (194 loc) · 4.84 KB
/
step3_env.ck
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
// @import readline.ck
// @import types/boxed/*.ck
// @import types/MalObject.ck
// @import types/mal/MalAtom.ck
// @import types/mal/MalError.ck
// @import types/mal/MalNil.ck
// @import types/mal/MalFalse.ck
// @import types/mal/MalTrue.ck
// @import types/mal/MalInt.ck
// @import types/mal/MalString.ck
// @import types/mal/MalSymbol.ck
// @import types/mal/MalKeyword.ck
// @import types/mal/MalList.ck
// @import types/mal/MalVector.ck
// @import types/mal/MalHashMap.ck
// @import util/*.ck
// @import reader.ck
// @import printer.ck
// @import env.ck
// @import types/MalSubr.ck
// @import types/subr/*.ck
fun MalObject READ(string input)
{
return Reader.read_str(input);
}
fun MalObject EVAL(MalObject m, Env env)
{
if( m.type == "list" )
{
if( (m$MalList).value().size() == 0 )
{
return m;
}
(m$MalList).value() @=> MalObject ast[];
(ast[0]$MalSymbol).value() => string a0;
if( a0 == "def!" )
{
(ast[1]$MalSymbol).value() => string a1;
EVAL(ast[2], env) @=> MalObject value;
if( value.type == "error" )
{
return value;
}
env.set(a1, value);
return value;
}
else if( a0 == "let*" )
{
Env.create(env) @=> Env let_env;
Util.sequenceToMalObjectArray(ast[1]) @=> MalObject bindings[];
for( 0 => int i; i < bindings.size(); 2 +=> i)
{
(bindings[i]$MalSymbol).value() => string symbol;
EVAL(bindings[i+1], let_env) @=> MalObject value;
if( value.type == "error" )
{
return value;
}
let_env.set(symbol, value);
}
return EVAL(ast[2], let_env);
}
eval_ast(m, env) @=> MalObject result;
if( result.type == "error" )
{
return result;
}
(result$MalList).value() @=> MalObject values[];
values[0]$MalSubr @=> MalSubr subr;
MalObject.slice(values, 1) @=> MalObject args[];
return subr.call(args);
}
else
{
eval_ast(m, env) @=> MalObject result;
return result;
}
}
fun MalObject eval_ast(MalObject m, Env env)
{
m.type => string type;
if( type == "symbol" )
{
(m$MalSymbol).value() => string symbol;
return env.get(symbol);
}
else if( type == "list" || type == "vector" || type == "hashmap" )
{
(m$MalList).value() @=> MalObject values[];
MalObject results[values.size()];
if( type != "hashmap" )
{
for( 0 => int i; i < values.size(); i++ )
{
EVAL(values[i], env) @=> MalObject result;
if( result.type == "error" )
{
return result;
}
result @=> results[i];
}
}
else
{
for( 0 => int i; i < values.size(); i++ )
{
if( i % 2 == 0 )
{
values[i] @=> results[i];
}
else
{
EVAL(values[i], env) @=> results[i];
}
}
}
if( type == "list" )
{
return MalList.create(results);
}
else if( type == "vector" )
{
return MalVector.create(results);
}
else if( type == "hashmap" )
{
return MalHashMap.create(results);
}
}
else
{
return m;
}
}
fun string PRINT(MalObject m)
{
return Printer.pr_str(m, true);
}
Env.create(null) @=> Env repl_env;
repl_env.set("+", new MalAdd);
repl_env.set("-", new MalSub);
repl_env.set("*", new MalMul);
repl_env.set("/", new MalDiv);
fun string errorMessage(MalObject m)
{
(m$MalError).value() @=> MalObject value;
if( value.type == "string" )
{
return Printer.pr_str(value, false);
}
else
{
return "exception: " + Printer.pr_str(value, true);
}
}
fun string rep(string input)
{
READ(input) @=> MalObject m;
if( m.type == "error" )
{
return errorMessage(m);
}
EVAL(m, repl_env) @=> MalObject result;
if( result.type == "error" )
{
return errorMessage(result);
}
return PRINT(result);
}
fun void main()
{
int done;
while( !done )
{
Readline.readline("user> ") => string input;
if( input != null )
{
rep(input) => string output;
if( output == "empty input" )
{
// proceed immediately with prompt
}
else
{
Util.println(output);
}
}
else
{
true => done;
}
}
}
main();