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
337
338
339
340
341
342
343
344
345
|
using System;
using System.Collections.Generic;
using Mal;
namespace Mal {
public class types {
//
// Exceptiosn/Errors
//
public class MalThrowable : Exception {
public MalThrowable() : base() { }
public MalThrowable(string msg) : base(msg) { }
}
public class MalError : MalThrowable {
public MalError(string msg) :base(msg) { }
}
public class MalContinue : MalThrowable { }
// Thrown by throw function
public class MalException : MalThrowable {
MalVal value;
//string Message;
public MalException(MalVal value) {
this.value = value;
}
public MalException(string value) :base(value) {
this.value = new MalString(value);
}
public MalVal getValue() { return value; }
}
//
// General functions
//
public static bool _equal_Q(MalVal a, MalVal b) {
Type ota = a.GetType(), otb = b.GetType();
if (!((ota == otb) ||
(a is MalList && b is MalList))) {
return false;
} else {
if (a is MalInt) {
return ((MalInt)a).getValue() ==
((MalInt)b).getValue();
} else if (a is MalSymbol) {
return ((MalSymbol)a).getName() ==
((MalSymbol)b).getName();
} else if (a is MalString) {
return ((MalString)a).getValue() ==
((MalString)b).getValue();
} else if (a is MalList) {
if (((MalList)a).size() != ((MalList)b).size()) {
return false;
}
for (int i=0; i<((MalList)a).size(); i++) {
if (! _equal_Q(((MalList)a)[i], ((MalList)b)[i])) {
return false;
}
}
return true;
} else {
return a == b;
}
}
}
public abstract class MalVal {
MalVal meta = Nil;
public virtual MalVal copy() {
return (MalVal)this.MemberwiseClone();
}
// Default is just to call regular toString()
public virtual string ToString(bool print_readably) {
return this.ToString();
}
public MalVal getMeta() { return meta; }
public MalVal setMeta(MalVal m) { meta = m; return this; }
public virtual bool list_Q() { return false; }
}
public class MalConstant : MalVal {
string value;
public MalConstant(string name) { value = name; }
public new MalConstant copy() { return this; }
public override string ToString() {
return value;
}
public override string ToString(bool print_readably) {
return value;
}
}
static public MalConstant Nil = new MalConstant("nil");
static public MalConstant True = new MalConstant("true");
static public MalConstant False = new MalConstant("false");
public class MalInt : MalVal {
Int64 value;
public MalInt(Int64 v) { value = v; }
public new MalInt copy() { return this; }
public Int64 getValue() { return value; }
public override string ToString() {
return value.ToString();
}
public override string ToString(bool print_readably) {
return value.ToString();
}
public static MalConstant operator <(MalInt a, MalInt b) {
return a.getValue() < b.getValue() ? True : False;
}
public static MalConstant operator <=(MalInt a, MalInt b) {
return a.getValue() <= b.getValue() ? True : False;
}
public static MalConstant operator >(MalInt a, MalInt b) {
return a.getValue() > b.getValue() ? True : False;
}
public static MalConstant operator >=(MalInt a, MalInt b) {
return a.getValue() >= b.getValue() ? True : False;
}
public static MalInt operator +(MalInt a, MalInt b) {
return new MalInt(a.getValue() + b.getValue());
}
public static MalInt operator -(MalInt a, MalInt b) {
return new MalInt(a.getValue() - b.getValue());
}
public static MalInt operator *(MalInt a, MalInt b) {
return new MalInt(a.getValue() * b.getValue());
}
public static MalInt operator /(MalInt a, MalInt b) {
return new MalInt(a.getValue() / b.getValue());
}
}
public class MalSymbol : MalVal {
string value;
public MalSymbol(string v) { value = v; }
public MalSymbol(MalString v) { value = v.getValue(); }
public new MalSymbol copy() { return this; }
public string getName() { return value; }
public override string ToString() {
return value;
}
public override string ToString(bool print_readably) {
return value;
}
}
public class MalString : MalVal {
string value;
public MalString(string v) { value = v; }
public new MalString copy() { return this; }
public string getValue() { return value; }
public override string ToString() {
return "\"" + value + "\"";
}
public override string ToString(bool print_readably) {
if (value.Length > 0 && value[0] == '\u029e') {
return ":" + value.Substring(1);
} else if (print_readably) {
return "\"" + value.Replace("\\", "\\\\")
.Replace("\"", "\\\"")
.Replace("\n", "\\n") + "\"";
} else {
return value;
}
}
}
public class MalList : MalVal {
public string start = "(", end = ")";
List<MalVal> value;
public MalList() {
value = new List<MalVal>();
}
public MalList(List<MalVal> val) {
value = val;
}
public MalList(params MalVal[] mvs) {
value = new List<MalVal>();
conj_BANG(mvs);
}
public List<MalVal> getValue() { return value; }
public override bool list_Q() { return true; }
public override string ToString() {
return start + printer.join(value, " ", true) + end;
}
public override string ToString(bool print_readably) {
return start + printer.join(value, " ", print_readably) + end;
}
public MalList conj_BANG(params MalVal[] mvs) {
for (int i = 0; i < mvs.Length; i++) {
value.Add(mvs[i]);
}
return this;
}
public int size() { return value.Count; }
public MalVal nth(int idx) {
return value.Count > idx ? value[idx] : Nil;
}
public MalVal this[int idx] {
get { return value.Count > idx ? value[idx] : Nil; }
}
public MalList rest() {
if (size() > 0) {
return new MalList(value.GetRange(1, value.Count-1));
} else {
return new MalList();
}
}
public virtual MalList slice(int start) {
return new MalList(value.GetRange(start, value.Count-start));
}
public virtual MalList slice(int start, int end) {
return new MalList(value.GetRange(start, end-start));
}
}
public class MalVector : MalList {
// Same implementation except for instantiation methods
public MalVector() :base() {
start = "[";
end = "]";
}
public MalVector(List<MalVal> val)
:base(val) {
start = "[";
end = "]";
}
public override bool list_Q() { return false; }
public override MalList slice(int start, int end) {
var val = this.getValue();
return new MalVector(val.GetRange(start, val.Count-start));
}
}
public class MalHashMap : MalVal {
Dictionary<string, MalVal> value;
public MalHashMap(Dictionary<string, MalVal> val) {
value = val;
}
public MalHashMap(MalList lst) {
value = new Dictionary<String, MalVal>();
assoc_BANG(lst);
}
public new MalHashMap copy() {
var new_self = (MalHashMap)this.MemberwiseClone();
new_self.value = new Dictionary<string, MalVal>(value);
return new_self;
}
public Dictionary<string, MalVal> getValue() { return value; }
public override string ToString() {
return "{" + printer.join(value, " ", true) + "}";
}
public override string ToString(bool print_readably) {
return "{" + printer.join(value, " ", print_readably) + "}";
}
public MalHashMap assoc_BANG(MalList lst) {
for (int i=0; i<lst.size(); i+=2) {
value[((MalString)lst[i]).getValue()] = lst[i+1];
}
return this;
}
public MalHashMap dissoc_BANG(MalList lst) {
for (int i=0; i<lst.size(); i++) {
value.Remove(((MalString)lst[i]).getValue());
}
return this;
}
}
public class MalAtom : MalVal {
MalVal value;
public MalAtom(MalVal value) { this.value = value; }
//public MalAtom copy() { return new MalAtom(value); }
public MalVal getValue() { return value; }
public MalVal setValue(MalVal value) { return this.value = value; }
public override string ToString() {
return "(atom " + printer._pr_str(value, true) + ")";
}
public override string ToString(Boolean print_readably) {
return "(atom " + printer._pr_str(value, print_readably) + ")";
}
}
public class MalFunc : MalVal {
Func<MalList, MalVal> fn = null;
MalVal ast = null;
Mal.env.Env env = null;
MalList fparams;
bool macro = false;
public MalFunc(Func<MalList, MalVal> fn) {
this.fn = fn;
}
public MalFunc(MalVal ast, Mal.env.Env env, MalList fparams,
Func<MalList, MalVal> fn) {
this.fn = fn;
this.ast = ast;
this.env = env;
this.fparams = fparams;
}
public override string ToString() {
if (ast != null) {
return "<fn* " + Mal.printer._pr_str(fparams,true) +
" " + Mal.printer._pr_str(ast, true) + ">";
} else {
return "<builtin_function " + fn.ToString() + ">";
}
}
public MalVal apply(MalList args) {
return fn(args);
}
public MalVal getAst() { return ast; }
public Mal.env.Env getEnv() { return env; }
public MalList getFParams() { return fparams; }
public Mal.env.Env genEnv(MalList args) {
return new Mal.env.Env(env, fparams, args);
}
public bool isMacro() { return macro; }
public void setMacro() { macro = true; }
}
}
}
|