forked from kanaka/mal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes.d
455 lines (385 loc) · 9.82 KB
/
types.d
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
import std.algorithm;
import std.array;
import std.conv;
import std.functional;
import std.range;
import env;
abstract class MalType
{
string print(bool readable) const;
bool is_truthy() const { return true; }
}
interface MalMeta
{
MalType meta();
MalType with_meta(MalType new_meta);
}
interface HasSeq
{
MalType seq();
}
class MalNil : MalType, HasSeq
{
override string print(bool readable) const { return "nil"; }
override bool is_truthy() const { return false; }
override bool opEquals(Object o) { return (cast(MalNil)(o) !is null); }
override MalType seq() { return this; }
}
class MalFalse : MalType
{
override string print(bool readable) const { return "false"; }
override bool is_truthy() const { return false; }
override bool opEquals(Object o) { return (cast(MalFalse)(o) !is null); }
}
class MalTrue : MalType
{
override string print(bool readable) const { return "true"; }
override bool opEquals(Object o) { return (cast(MalTrue)(o) !is null); }
}
MalNil mal_nil;
MalFalse mal_false;
MalTrue mal_true;
static this()
{
mal_nil = new MalNil;
mal_false = new MalFalse;
mal_true = new MalTrue;
}
MalType bool_to_mal(in bool b)
{
return b ? mal_true : mal_false;
}
class MalSymbol : MalType
{
const string name;
this(in string token) { name = token; }
override string print(bool readable) const { return name; }
override size_t toHash()
{
return typeid(name).getHash(&name);
}
override int opCmp(Object other)
{
MalSymbol o = cast(MalSymbol) other;
return cmp(name, o.name);
}
override bool opEquals(Object other)
{
auto o = cast(MalSymbol) other;
return (o !is null && name == o.name);
}
}
class MalInteger : MalType
{
const long val;
this(string token) { val = to!long(token); }
this(long v) { val = v; }
override string print(bool readable) const { return to!string(val); }
override bool opEquals(Object o)
{
auto oint = cast(MalInteger)(o);
return (oint !is null && val == oint.val);
}
}
class MalString : MalType, HasSeq
{
const string val;
this(in string token) { val = token; }
override string print(bool readable) const
{
if (is_keyword()) return ":" ~ val[2..$];
if (readable)
{
string escaped = val.replace("\\", "\\\\")
.replace("\"", "\\\"")
.replace("\n", "\\n");
return "\"" ~ escaped ~ "\"";
}
else
{
return val;
}
}
bool is_keyword() const
{
return val.length > 1 && val[0..2] == "\u029e";
}
override bool opEquals(Object o)
{
auto ostr = cast(MalString)(o);
return (ostr !is null && val == ostr.val);
}
override MalType seq() {
if (is_keyword() || val.length == 0) return mal_nil;
auto chars = val.map!(c => cast(MalType)(new MalString(to!string(c))));
return new MalList(array(chars));
}
}
abstract class MalSequential : MalType, HasSeq, MalMeta
{
MalType[] elements;
MalType meta_val;
this(MalType[] lst) {
elements = lst;
meta_val = mal_nil;
}
override bool opEquals(Object o)
{
auto oseq = cast(MalSequential)(o);
return (oseq !is null && elements == oseq.elements);
}
MalSequential conj(MalType element);
MalType seq() {
if (elements.length == 0) return mal_nil;
return new MalList(elements);
}
}
class MalList : MalSequential, MalMeta
{
this(MalType[] lst) { super(lst); }
this(MalList that, MalType new_meta)
{
super(that.elements);
meta_val = new_meta;
}
override string print(bool readable) const
{
auto items_strs = elements.map!(e => e.print(readable));
return "(" ~ array(items_strs).join(" ") ~ ")";
}
override MalSequential conj(MalType element)
{
return new MalList([element] ~ elements);
}
override MalType meta() { return meta_val; }
override MalType with_meta(MalType new_meta)
{
return new MalList(this, new_meta);
}
}
class MalVector : MalSequential, MalMeta
{
this(MalType[] lst) { super(lst); }
this(MalVector that, MalType new_meta)
{
super(that.elements);
meta_val = new_meta;
}
override string print(bool readable) const
{
auto items_strs = elements.map!(e => e.print(readable));
return "[" ~ array(items_strs).join(" ") ~ "]";
}
override MalSequential conj(MalType element)
{
return new MalVector(elements ~ [element]);
}
override MalType meta() { return meta_val; }
override MalType with_meta(MalType new_meta)
{
return new MalVector(this, new_meta);
}
}
class MalHashmap : MalType, MalMeta
{
MalType[string] data;
MalType meta_val;
this(MalType[string] map)
{
data = map;
meta_val = mal_nil;
}
this(MalType[] lst)
{
put_kv_list(lst);
meta_val = mal_nil;
}
this(MalHashmap that, MalType new_meta)
{
data = that.data;
meta_val = new_meta;
}
bool contains(in MalType key)
{
auto valp = (make_hash_key(key) in data);
return valp !is null;
}
MalType get(in MalType key)
{
auto valp = (make_hash_key(key) in data);
return valp is null ? mal_nil : *valp;
}
void remove(in MalType key)
{
data.remove(make_hash_key(key));
}
void put(in MalType key, MalType val)
{
data[make_hash_key(key)] = val;
}
void put_kv_list(MalType[] lst)
{
foreach (kv; chunks(lst, 2))
{
if (kv.length < 2) throw new Exception("requires even number of elements");
put(kv[0], kv[1]);
}
}
private string make_hash_key(in MalType key)
{
return verify_cast!MalString(key).val;
}
override string print(bool readable) const
{
string[] parts;
foreach (k, v; data)
{
parts ~= (new MalString(k)).print(readable);
parts ~= v.print(readable);
}
return "{" ~ parts.join(" ") ~ "}";
}
override bool opEquals(Object o)
{
auto ohm = cast(MalHashmap)(o);
return (ohm !is null && data == ohm.data);
}
override MalType meta() { return meta_val; }
override MalType with_meta(MalType new_meta)
{
return new MalHashmap(this, new_meta);
}
}
alias BuiltinStaticFuncType = MalType function(MalType[] a ...);
alias BuiltinFuncType = MalType delegate(MalType[] a ...);
class MalBuiltinFunc : MalType, MalMeta
{
const BuiltinFuncType fn;
const string name;
MalType meta_val;
this(in BuiltinFuncType fn_v, in string name_v)
{
fn = fn_v;
name = name_v;
meta_val = mal_nil;
}
this(in BuiltinStaticFuncType static_fn_v, in string name_v)
{
fn = toDelegate(static_fn_v);
name = name_v;
meta_val = mal_nil;
}
this(MalBuiltinFunc that, MalType new_meta)
{
fn = that.fn;
name = that.name;
meta_val = new_meta;
}
override string print(bool readable) const
{
return "<BuiltinFunc:" ~ name ~ ">";
}
override MalType meta() { return meta_val; }
override MalType with_meta(MalType new_meta)
{
return new MalBuiltinFunc(this, new_meta);
}
}
class MalFunc : MalType, MalMeta
{
MalType[] arg_names;
MalType func_body;
Env def_env;
bool is_macro;
MalType meta_val;
this(MalType[] arg_names_v, MalType func_body_v, Env def_env_v)
{
arg_names = arg_names_v;
func_body = func_body_v;
def_env = def_env_v;
is_macro = false;
meta_val = mal_nil;
}
this(MalFunc that, MalType new_meta)
{
arg_names = that.arg_names;
func_body = that.func_body;
def_env = that.def_env;
is_macro = that.is_macro;
meta_val = new_meta;
}
override string print(bool readable) const
{
return "<Function:args=" ~ array(arg_names.map!(e => e.print(true))).join(",") ~ ">";
}
override MalType meta() { return meta_val; }
override MalType with_meta(MalType new_meta)
{
return new MalFunc(this, new_meta);
}
}
class MalAtom : MalType, MalMeta
{
MalType val;
MalType meta_val;
this(MalType v)
{
val = v;
meta_val = mal_nil;
}
this(MalAtom that, MalType new_meta)
{
val = that.val;
meta_val = new_meta;
}
override string print(bool readable) const
{
return "(atom " ~ val.print(readable) ~ ")";
}
override bool opEquals(Object other)
{
auto o = cast(MalAtom) other;
return (o !is null && val == o.val);
}
override MalType meta() { return meta_val; }
override MalType with_meta(MalType new_meta)
{
return new MalAtom(this, new_meta);
}
}
class MalException : Exception
{
MalType data;
this(MalType val)
{
super("MalException");
data = val;
}
}
T verify_cast(T)(in MalType v)
{
T res = cast(T) v;
if (res is null) throw new Exception("Expected " ~ typeid(T).name);
return res;
}
MalType mal_type_q(T)(in MalType[] a)
{
verify_args_count(a, 1);
T res = cast(T) a[0];
return bool_to_mal(res !is null);
}
inout(MalType[]) verify_args_count(inout MalType[] args, in int expected_length)
{
if (args.length != expected_length)
{
throw new Exception("Expected " ~ to!string(expected_length) ~ " arguments");
}
return args;
}
void verify_min_args_count(in MalType[] args, in int min_expected_length)
{
if (args.length < min_expected_length)
{
throw new Exception("Expected at least " ~ to!string(min_expected_length) ~ " arguments");
}
}