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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
|
using System;
using System.IO;
using System.Collections.Generic;
using MalVal = Mal.types.MalVal;
using MalConstant = Mal.types.MalConstant;
using MalInt = Mal.types.MalInt;
using MalSymbol = Mal.types.MalSymbol;
using MalString = Mal.types.MalString;
using MalList = Mal.types.MalList;
using MalVector = Mal.types.MalVector;
using MalHashMap = Mal.types.MalHashMap;
using MalAtom = Mal.types.MalAtom;
using MalFunc = Mal.types.MalFunc;
namespace Mal {
public class core {
static MalConstant Nil = Mal.types.Nil;
static MalConstant True = Mal.types.True;
static MalConstant False = Mal.types.False;
// Errors/Exceptions
static public MalFunc mal_throw = new MalFunc(
a => { throw new Mal.types.MalException(a[0]); });
// Scalar functions
static MalFunc nil_Q = new MalFunc(
a => a[0] == Nil ? True : False);
static MalFunc true_Q = new MalFunc(
a => a[0] == True ? True : False);
static MalFunc false_Q = new MalFunc(
a => a[0] == False ? True : False);
static MalFunc symbol_Q = new MalFunc(
a => a[0] is MalSymbol ? True : False);
static MalFunc keyword = new MalFunc(
a => new MalString("\u029e" + ((MalString)a[0]).getValue()));
static MalFunc keyword_Q = new MalFunc(
a => {
if (a[0] is MalString &&
((MalString)a[0]).getValue()[0] == '\u029e') {
return True;
} else {
return False;
}
} );
// Number functions
static MalFunc time_ms = new MalFunc(
a => new MalInt(DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond));
// String functions
static public MalFunc pr_str = new MalFunc(
a => new MalString(printer._pr_str_args(a, " ", true)) );
static public MalFunc str = new MalFunc(
a => new MalString(printer._pr_str_args(a, "", false)) );
static public MalFunc prn = new MalFunc(
a => {
Console.WriteLine(printer._pr_str_args(a, " ", true));
return Nil;
} );
static public MalFunc println = new MalFunc(
a => {
Console.WriteLine(printer._pr_str_args(a, " ", false));
return Nil;
} );
static public MalFunc mal_readline = new MalFunc(
a => {
var line = readline.Readline(((MalString)a[0]).getValue());
if (line == null) { return types.Nil; }
else { return new MalString(line); }
} );
static public MalFunc read_string = new MalFunc(
a => reader.read_str(((MalString)a[0]).getValue()));
static public MalFunc slurp = new MalFunc(
a => new MalString(File.ReadAllText(
((MalString)a[0]).getValue())));
// List/Vector functions
static public MalFunc list_Q = new MalFunc(
a => a[0].GetType() == typeof(MalList) ? True : False);
static public MalFunc vector_Q = new MalFunc(
a => a[0].GetType() == typeof(MalVector) ? True : False);
// HashMap functions
static public MalFunc hash_map_Q = new MalFunc(
a => a[0].GetType() == typeof(MalHashMap) ? True : False);
static MalFunc contains_Q = new MalFunc(
a => {
string key = ((MalString)a[1]).getValue();
var dict = ((MalHashMap)a[0]).getValue();
return dict.ContainsKey(key) ? True : False;
});
static MalFunc assoc = new MalFunc(
a => {
var new_hm = ((MalHashMap)a[0]).copy();
return new_hm.assoc_BANG((MalList)a.slice(1));
});
static MalFunc dissoc = new MalFunc(
a => {
var new_hm = ((MalHashMap)a[0]).copy();
return new_hm.dissoc_BANG((MalList)a.slice(1));
});
static MalFunc get = new MalFunc(
a => {
string key = ((MalString)a[1]).getValue();
if (a[0] == Nil) {
return Nil;
} else {
var dict = ((MalHashMap)a[0]).getValue();
return dict.ContainsKey(key) ? dict[key] : Nil;
}
});
static MalFunc keys = new MalFunc(
a => {
var dict = ((MalHashMap)a[0]).getValue();
MalList key_lst = new MalList();
foreach (var key in dict.Keys) {
key_lst.conj_BANG(new MalString(key));
}
return key_lst;
});
static MalFunc vals = new MalFunc(
a => {
var dict = ((MalHashMap)a[0]).getValue();
MalList val_lst = new MalList();
foreach (var val in dict.Values) {
val_lst.conj_BANG(val);
}
return val_lst;
});
// Sequence functions
static public MalFunc sequential_Q = new MalFunc(
a => a[0] is MalList ? True : False);
static MalFunc cons = new MalFunc(
a => {
var lst = new List<MalVal>();
lst.Add(a[0]);
lst.AddRange(((MalList)a[1]).getValue());
return (MalVal)new MalList(lst);
});
static MalFunc concat = new MalFunc(
a => {
if (a.size() == 0) { return new MalList(); }
var lst = new List<MalVal>();
lst.AddRange(((MalList)a[0]).getValue());
for(int i=1; i<a.size(); i++) {
lst.AddRange(((MalList)a[i]).getValue());
}
return (MalVal)new MalList(lst);
});
static MalFunc nth = new MalFunc(
a => {
var idx = (int)((MalInt)a[1]).getValue();
if (idx < ((MalList)a[0]).size()) {
return ((MalList)a[0])[idx];
} else {
throw new Mal.types.MalException(
"nth: index out of range");
}
});
static MalFunc first = new MalFunc(
a => ((MalList)a[0])[0]);
static MalFunc rest = new MalFunc(
a => ((MalList)a[0]).rest());
static MalFunc empty_Q = new MalFunc(
a => ((MalList)a[0]).size() == 0 ? True : False);
static MalFunc count = new MalFunc(
a => {
return (a[0] == Nil)
? new MalInt(0)
:new MalInt(((MalList)a[0]).size());
});
static MalFunc conj = new MalFunc(
a => {
var src_lst = ((MalList)a[0]).getValue();
var new_lst = new List<MalVal>();
new_lst.AddRange(src_lst);
if (a[0] is MalVector) {
for(int i=1; i<a.size(); i++) {
new_lst.Add(a[i]);
}
return new MalVector(new_lst);
} else {
for(int i=1; i<a.size(); i++) {
new_lst.Insert(0, a[i]);
}
return new MalList(new_lst);
}
});
// General list related functions
static MalFunc apply = new MalFunc(
a => {
var f = (MalFunc)a[0];
var lst = new List<MalVal>();
lst.AddRange(a.slice(1,a.size()-1).getValue());
lst.AddRange(((MalList)a[a.size()-1]).getValue());
return f.apply(new MalList(lst));
});
static MalFunc map = new MalFunc(
a => {
MalFunc f = (MalFunc) a[0];
var src_lst = ((MalList)a[1]).getValue();
var new_lst = new List<MalVal>();
for(int i=0; i<src_lst.Count; i++) {
new_lst.Add(f.apply(new MalList(src_lst[i])));
}
return new MalList(new_lst);
});
// Metadata functions
static MalFunc meta = new MalFunc(
a => a[0].getMeta());
static MalFunc with_meta = new MalFunc(
a => ((MalVal)a[0]).copy().setMeta(a[1]));
// Atom functions
static MalFunc atom_Q = new MalFunc(
a => a[0] is MalAtom ? True : False);
static MalFunc deref = new MalFunc(
a => ((MalAtom)a[0]).getValue());
static MalFunc reset_BANG = new MalFunc(
a => ((MalAtom)a[0]).setValue(a[1]));
static MalFunc swap_BANG = new MalFunc(
a => {
MalAtom atm = (MalAtom)a[0];
MalFunc f = (MalFunc)a[1];
var new_lst = new List<MalVal>();
new_lst.Add(atm.getValue());
new_lst.AddRange(((MalList)a.slice(2)).getValue());
return atm.setValue(f.apply(new MalList(new_lst)));
});
static public Dictionary<string, MalVal> ns =
new Dictionary<string, MalVal> {
{"=", new MalFunc(
a => Mal.types._equal_Q(a[0], a[1]) ? True : False)},
{"throw", mal_throw},
{"nil?", nil_Q},
{"true?", true_Q},
{"false?", false_Q},
{"symbol", new MalFunc(a => new MalSymbol((MalString)a[0]))},
{"symbol?", symbol_Q},
{"keyword", keyword},
{"keyword?", keyword_Q},
{"pr-str", pr_str},
{"str", str},
{"prn", prn},
{"println", println},
{"readline", mal_readline},
{"read-string", read_string},
{"slurp", slurp},
{"<", new MalFunc(a => (MalInt)a[0] < (MalInt)a[1])},
{"<=", new MalFunc(a => (MalInt)a[0] <= (MalInt)a[1])},
{">", new MalFunc(a => (MalInt)a[0] > (MalInt)a[1])},
{">=", new MalFunc(a => (MalInt)a[0] >= (MalInt)a[1])},
{"+", new MalFunc(a => (MalInt)a[0] + (MalInt)a[1])},
{"-", new MalFunc(a => (MalInt)a[0] - (MalInt)a[1])},
{"*", new MalFunc(a => (MalInt)a[0] * (MalInt)a[1])},
{"/", new MalFunc(a => (MalInt)a[0] / (MalInt)a[1])},
{"time-ms", time_ms},
{"list", new MalFunc(a => new MalList(a.getValue()))},
{"list?", list_Q},
{"vector", new MalFunc(a => new MalVector(a.getValue()))},
{"vector?", vector_Q},
{"hash-map", new MalFunc(a => new MalHashMap(a))},
{"map?", hash_map_Q},
{"contains?", contains_Q},
{"assoc", assoc},
{"dissoc", dissoc},
{"get", get},
{"keys", keys},
{"vals", vals},
{"sequential?", sequential_Q},
{"cons", cons},
{"concat", concat},
{"nth", nth},
{"first", first},
{"rest", rest},
{"empty?", empty_Q},
{"count", count},
{"conj", conj},
{"apply", apply},
{"map", map},
{"with-meta", with_meta},
{"meta", meta},
{"atom", new MalFunc(a => new MalAtom(a[0]))},
{"atom?", atom_Q},
{"deref", deref},
{"reset!", reset_BANG},
{"swap!", swap_BANG},
};
}
}
|