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
|
using System;
using System.IO;
using System.Collections.Generic;
using MalVal = Mal.types.MalVal;
using MalConstant = Mal.types.MalConstant;
using MalInteger = Mal.types.MalInteger;
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 MalFunction = Mal.types.MalFunction;
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 MalFunction mal_throw = new MalFunction(
a => { throw new Mal.types.MalException(a[0]); });
// Scalar functions
static MalFunction nil_Q = new MalFunction(
a => a[0] == Nil ? True : False);
static MalFunction true_Q = new MalFunction(
a => a[0] == True ? True : False);
static MalFunction false_Q = new MalFunction(
a => a[0] == False ? True : False);
static MalFunction symbol_Q = new MalFunction(
a => a[0] is MalSymbol ? True : False);
// Number functions
static MalFunction time_ms = new MalFunction(
a => new MalInteger((int)(
DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond)));
// String functions
static public MalFunction pr_str = new MalFunction(
a => new MalString(printer._pr_str_args(a, " ", true)) );
static public MalFunction str = new MalFunction(
a => new MalString(printer._pr_str_args(a, "", false)) );
static public MalFunction prn = new MalFunction(
a => {
Console.WriteLine(printer._pr_str_args(a, " ", true));
return Nil;
} );
static public MalFunction println = new MalFunction(
a => {
Console.WriteLine(printer._pr_str_args(a, " ", false));
return Nil;
} );
static public MalFunction mal_readline = new MalFunction(
a => {
var line = readline.Readline(((MalString)a[0]).getValue());
if (line == null) { return types.Nil; }
else { return new MalString(line); }
} );
static public MalFunction read_string = new MalFunction(
a => reader.read_str(((MalString)a[0]).getValue()));
static public MalFunction slurp = new MalFunction(
a => new MalString(File.ReadAllText(
((MalString)a[0]).getValue())));
// List/Vector functions
static public MalFunction list_Q = new MalFunction(
a => a[0].GetType() == typeof(MalList) ? True : False);
static public MalFunction vector_Q = new MalFunction(
a => a[0].GetType() == typeof(MalVector) ? True : False);
// HashMap functions
static public MalFunction hash_map_Q = new MalFunction(
a => a[0].GetType() == typeof(MalHashMap) ? True : False);
static MalFunction contains_Q = new MalFunction(
a => {
string key = ((MalString)a[1]).getValue();
var dict = ((MalHashMap)a[0]).getValue();
return dict.ContainsKey(key) ? True : False;
});
static MalFunction assoc = new MalFunction(
a => {
var new_hm = ((MalHashMap)a[0]).copy();
return new_hm.assoc_BANG((MalList)a.slice(1));
});
static MalFunction dissoc = new MalFunction(
a => {
var new_hm = ((MalHashMap)a[0]).copy();
return new_hm.dissoc_BANG((MalList)a.slice(1));
});
static MalFunction get = new MalFunction(
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 MalFunction keys = new MalFunction(
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 MalFunction vals = new MalFunction(
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 MalFunction sequential_Q = new MalFunction(
a => a[0] is MalList ? True : False);
static MalFunction cons = new MalFunction(
a => {
var lst = new List<MalVal>();
lst.Add(a[0]);
lst.AddRange(((MalList)a[1]).getValue());
return (MalVal)new MalList(lst);
});
static MalFunction concat = new MalFunction(
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 MalFunction nth = new MalFunction(
a => ((MalList)a[0])[ ((MalInteger)a[1]).getValue() ]);
static MalFunction first = new MalFunction(
a => ((MalList)a[0])[0]);
static MalFunction rest = new MalFunction(
a => ((MalList)a[0]).rest());
static MalFunction empty_Q = new MalFunction(
a => ((MalList)a[0]).size() == 0 ? True : False);
static MalFunction count = new MalFunction(
a => new MalInteger(((MalList)a[0]).size()));
static MalFunction conj = new MalFunction(
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 MalFunction apply = new MalFunction(
a => {
var f = (MalFunction)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 MalFunction map = new MalFunction(
a => {
MalFunction f = (MalFunction) 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 MalFunction meta = new MalFunction(
a => a[0].getMeta());
static MalFunction with_meta = new MalFunction(
a => ((MalVal)a[0]).copy().setMeta(a[1]));
// Atom functions
static MalFunction atom_Q = new MalFunction(
a => a[0] is MalAtom ? True : False);
static MalFunction deref = new MalFunction(
a => ((MalAtom)a[0]).getValue());
static MalFunction reset_BANG = new MalFunction(
a => ((MalAtom)a[0]).setValue(a[1]));
static MalFunction swap_BANG = new MalFunction(
a => {
MalAtom atm = (MalAtom)a[0];
MalFunction f = (MalFunction)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 MalFunction(
a => Mal.types._equal_Q(a[0], a[1]) ? True : False)},
{"throw", mal_throw},
{"nil?", nil_Q},
{"true?", true_Q},
{"false?", false_Q},
{"symbol?", symbol_Q},
{"pr-str", pr_str},
{"str", str},
{"prn", prn},
{"println", println},
{"readline", mal_readline},
{"read-string", read_string},
{"slurp", slurp},
{"<", new MalFunction(a => (MalInteger)a[0] < (MalInteger)a[1])},
{"<=", new MalFunction(a => (MalInteger)a[0] <= (MalInteger)a[1])},
{">", new MalFunction(a => (MalInteger)a[0] > (MalInteger)a[1])},
{">=", new MalFunction(a => (MalInteger)a[0] >= (MalInteger)a[1])},
{"+", new MalFunction(a => (MalInteger)a[0] + (MalInteger)a[1])},
{"-", new MalFunction(a => (MalInteger)a[0] - (MalInteger)a[1])},
{"*", new MalFunction(a => (MalInteger)a[0] * (MalInteger)a[1])},
{"/", new MalFunction(a => (MalInteger)a[0] / (MalInteger)a[1])},
{"time-ms", time_ms},
{"list", new MalFunction(a => new MalList(a.getValue()))},
{"list?", list_Q},
{"vector", new MalFunction(a => new MalVector(a.getValue()))},
{"vector?", vector_Q},
{"hash-map", new MalFunction(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 MalFunction(a => new MalAtom(a[0]))},
{"atom?", atom_Q},
{"deref", deref},
{"reset!", reset_BANG},
{"swap!", swap_BANG},
};
}
}
|