-
Notifications
You must be signed in to change notification settings - Fork 0
/
Value.cs
555 lines (447 loc) · 15.8 KB
/
Value.cs
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
using System;
using System.Collections.Generic;
using System.Linq;
public interface IterableValue {
IEnumerable<List<Value>> GetIter();
}
public interface Callable {
Value Execute(List<Value> parameters);
}
public interface Indexable {
Value this[Value index] {get;}
}
public class Value
{
public Position posStart;
public Position posEnd;
public Context context;
protected Value(Position start, Position end, Context ctx)
{
posStart = start;
posEnd = end;
context = ctx;
}
virtual public Value this[Value index] {
get {
return ((Indexable)this)[index];
}
}
public Value setPos(Position start, Position end)
{
this.posStart = start;
this.posEnd = end;
return this;
}
public double GetDouble()
{
if(this is Number) return ((Number)this).num;
else throw new RuntimeError(posStart, posEnd, string.Format("Expected a Number, got {0}", this.GetType()));
}
public string GetString()
{
if(this is Str) return ((Str)this).str;
else throw new RuntimeError(posStart, posEnd, string.Format("Expected a Str, got {0}", this.GetType()));
}
public bool GetBool()
{
if(this is Bool) return ((Bool)this).boolean;
else throw new RuntimeError(posStart, posEnd, string.Format("Expected a Bool, got {0}", this.GetType()));
}
public IEnumerable<IEnumerable<Value>> GetIterator()
{
if(this is IterableValue) return ((IterableValue)this).GetIter();
else throw new RuntimeError(posStart, posEnd, string.Format("Expected an Iterable, got {0}", this.GetType()));
}
public IEnumerable<Value> GetList()
{
if(this is IteratorValue) return ((IteratorValue)this).elems;
else throw new RuntimeError(posStart, posEnd, string.Format("Expected a IteratorValue, got {0}", this.GetType()));
}
public IEnumerable<Value> GetObject()
{
if(this is ObjectValue) return ((ObjectValue)this).GetObject();
else throw new RuntimeError(posStart, posEnd, string.Format("Expected a ObjectValue, got {0}", this.GetType()));
}
public Value Call(List<Value> vals)
{
if(this is Callable) return ((Callable)this).Execute(vals);
else throw new RuntimeError(posStart, posEnd, string.Format("Cannot Call {0}", this.GetType()));
}
virtual public Value LookupString(string prop)
{
throw new RuntimeError(posStart, posEnd, string.Format("Cannot perform a lookup on {0}", this.GetType()));
}
public static Number Construct(double i)
{
return Number.Construct(i);
}
public static Str Construct(string s)
{
return Str.Construct(s);
}
public static Bool Construct(bool s)
{
return Bool.Construct(s);
}
public static IteratorValue Construct(IEnumerable<Value> vals)
{
return IteratorValue.Construct(vals);
}
public static ObjectValue Construct(Dictionary<Value, Value> vals)
{
return ObjectValue.Construct(vals);
}
public static FunctionValue Construct(int? paramsCount, Func<List<Value>, Value> f)
{
return FunctionValue.Construct(paramsCount, f);
}
public static FunctionValue Construct(Func<List<Value>, Value> f)
{
return FunctionValue.Construct(null, f);
}
public Value setContext(Context ctx)
{
this.context = ctx;
return this;
}
public override bool Equals(object obj)
{
if(!(obj is Value)) return false;
return this.ee((Value)obj).IsTrue();
}
public override int GetHashCode()
{
return 0;
}
public virtual Value add(Value other)
{
throw new RuntimeError(this.posStart, other.posEnd, string.Format("Cannot add {0} and {1}", this.GetType(), other.GetType()));
}
public virtual Value sub(Value other)
{
throw new RuntimeError(this.posStart, other.posEnd, string.Format("Cannot subtract {0} and {1}", this.GetType(), other.GetType()));
}
public virtual Value mul(Value other)
{
throw new RuntimeError(this.posStart, other.posEnd, string.Format("Cannot multiply {0} and {1}", this.GetType(), other.GetType()));
}
public virtual Value div(Value other)
{
throw new RuntimeError(this.posStart, other.posEnd, string.Format("Cannot divide {0} and {1}", this.GetType(), other.GetType()));
}
public virtual Value ee(Value other)
{
return new Bool(false, this.posStart, other.posEnd, context);
}
public virtual Value ne(Value other)
{
return new Bool(true, this.posStart, other.posEnd, context);
}
public virtual Value gte(Value other)
{
throw new RuntimeError(this.posStart, other.posEnd, string.Format("Cannot use >= with {0} and {1}", this.GetType(), other.GetType()));
}
public virtual Value gt(Value other)
{
throw new RuntimeError(this.posStart, other.posEnd, string.Format("Cannot use > with {0} and {1}", this.GetType(), other.GetType()));
}
public virtual Value lt(Value other)
{
throw new RuntimeError(this.posStart, other.posEnd, string.Format("Cannot use < with {0} and {1}", this.GetType(), other.GetType()));
}
public virtual Value lte(Value other)
{
throw new RuntimeError(this.posStart, other.posEnd, string.Format("Cannot use <= with {0} and {1}", this.GetType(), other.GetType()));
}
public virtual Value anded(Value other)
{
return new Bool(this.IsTrue() && other.IsTrue(), this.posStart, other.posEnd, context);
}
public virtual Value ored(Value other)
{
return new Bool(this.IsTrue() || other.IsTrue(), this.posStart, other.posEnd, context);
}
public virtual bool IsTrue()
{
throw new RuntimeError(this.posStart, this.posEnd, string.Format("{0} has no intrinsic truth value", this.GetType()));
}
public static Value operator+ (Value a, Value b) {
return a.add(b);
}
public static Value operator- (Value a, Value b) {
return a.sub(b);
}
public static Value operator* (Value a, Value b) {
return a.mul(b);
}
public static Value operator/ (Value a, Value b) {
return a.div(b);
}
public static Value operator> (Value a, Value b) {
return a.gt(b);
}
public static Value operator>= (Value a, Value b) {
return a.gte(b);
}
public static Value operator< (Value a, Value b) {
return a.lt(b);
}
public static Value operator<= (Value a, Value b) {
return a.lte(b);
}
public static Value operator& (Value a, Value b) {
return a.anded(b);
}
public static Value operator| (Value a, Value b) {
return a.ored(b);
}
public static implicit operator double(Value a) => a.GetDouble();
public static implicit operator Value(double a) => Value.Construct(a);
public static implicit operator int(Value a) => System.Convert.ToInt32(a.GetDouble());
public static implicit operator Value(int a) => Value.Construct(a);
public static implicit operator string(Value a) => a.GetString();
public static implicit operator Value(string a) => Value.Construct(a);
public static implicit operator bool(Value a) => a.IsTrue();
public static implicit operator Value(bool a) => Value.Construct(a);
public static implicit operator Value(List<Value> a) => Value.Construct(a);
public static implicit operator Value(Dictionary<Value, Value> a) => Value.Construct(a);
}
sealed public class Number : Value
{
public double num;
public Number(double given, Position start, Position end, Context ctx) : base(start, end, ctx)
{
num = given;
}
public static new Number Construct(double given)
{
return new Number(given, Position.Nothing(), Position.Nothing(), new Context());
}
public override int GetHashCode()
{
return this.num.GetHashCode();
}
public override string ToString()
{
return num.ToString();
}
override public Value add(Value other)
{
if (!(other is Number)) base.add(other);
return new Number(this.num + ((Number)other).num, this.posStart, other.posEnd, other.context);
}
override public Value sub(Value other)
{
if (!(other is Number)) base.sub(other);
return new Number(this.num - ((Number)other).num, this.posStart, other.posEnd, other.context);
}
override public Value mul(Value other)
{
if (!(other is Number)) base.sub(other);
return new Number(this.num * ((Number)other).num, this.posStart, other.posEnd, other.context);
}
override public Value div(Value other)
{
if (!(other is Number)) base.div(other);
if (((Number)other).num == 0) throw new RuntimeError(other.posStart, other.posEnd, "Division by zero is undefined");
return new Number(this.num / ((Number)other).num, this.posStart, other.posEnd, other.context);
}
override public Value gte(Value other)
{
if (!(other is Number)) base.gte(other);
return new Bool(this.num >= ((Number)other).num, this.posStart, other.posEnd, other.context);
}
override public Value ee(Value other)
{
if (!(other is Number)) return base.ee(other);
System.Console.WriteLine();
return new Bool(this.num == ((Number)other).num, this.posStart, other.posEnd, other.context);
}
override public Value ne(Value other)
{
if (!(other is Number)) return base.ne(other);
return new Bool(this.num != ((Number)other).num, this.posStart, other.posEnd, other.context);
}
override public Value gt(Value other)
{
if (!(other is Number)) base.gt(other);
return new Bool(this.num > ((Number)other).num, this.posStart, other.posEnd, other.context);
}
override public Value lt(Value other)
{
if (!(other is Number)) base.lt(other);
return new Bool(this.num < ((Number)other).num, this.posStart, other.posEnd, other.context);
}
override public Value lte(Value other)
{
if (!(other is Number)) base.lte(other);
return new Bool(this.num <= ((Number)other).num, this.posStart, other.posEnd, other.context);
}
}
sealed public class Str : Value, IterableValue
{
public string str;
public Str(string val, Position start, Position end, Context ctx) : base(start, end, ctx)
{
str = val;
}
public IEnumerable<List<Value>> GetIter()
{
foreach(var ch in str) yield return new List<Value>(){new Str(ch.ToString(), posStart, posEnd, context)};
}
public override int GetHashCode()
{
return this.str.GetHashCode();
}
override public Value add(Value other)
{
if (!(other is Str)) base.add(other);
return new Str(this.str + ((Str)other).str, this.posStart, other.posEnd, other.context);
}
public new static Str Construct(string given)
{
return new Str(given, Position.Nothing(), Position.Nothing(), new Context());
}
override public Value ee(Value other)
{
if (!(other is Str)) return base.ee(other);
return new Bool(this.str == ((Str)other).str, this.posStart, other.posEnd, other.context);
}
override public Value ne(Value other)
{
if (!(other is Str)) return base.ne(other);
return new Bool(this.str != ((Str)other).str, this.posStart, other.posEnd, other.context);
}
public override string ToString()
{
return str;
}
}
sealed public class Bool : Value
{
public bool boolean;
public Bool(bool b, Position start, Position end, Context ctx) : base(start, end, ctx)
{
boolean = b;
}
public override int GetHashCode()
{
return this.boolean.GetHashCode();
}
public new static Bool Construct(bool given)
{
return new Bool(given, Position.Nothing(), Position.Nothing(), new Context());
}
override public Value ee(Value other)
{
if (!(other is Bool)) return base.ee(other);
return new Bool(this.boolean == ((Bool)other).boolean, this.posStart, other.posEnd, other.context);
}
override public Value ne(Value other)
{
if (!(other is Bool)) return base.ne(other);
return new Bool(this.boolean != ((Bool)other).boolean, this.posStart, other.posEnd, other.context);
}
public override bool IsTrue()
{
return boolean;
}
public override string ToString()
{
return boolean ? "true" : "false";
}
}
sealed public class IteratorValue : Value, IterableValue, Indexable
{
public IEnumerable<Value> elems;
override public Value this[Value i] => elems.ElementAt(i);
public IteratorValue(IEnumerable<Value> nodes, Position start, Position end, Context ctx) : base(start, end, ctx)
{
elems = nodes;
}
public IEnumerable<List<Value>> GetIter()
{
return elems.Select(a => new List<Value>(){a});
}
public new static IteratorValue Construct(IEnumerable<Value> given)
{
return new IteratorValue(given, Position.Nothing(), Position.Nothing(), new Context());
}
public override string ToString()
{
return "[" + string.Join(", ", elems.Select(a => a.ToString())) + "]";
}
}
public class ObjectValue : Value, IterableValue, Indexable
{
public Dictionary<Value, Value> dict;
override public Value this[Value i] => dict[i];
public ObjectValue(Dictionary<Value, Value> vals, Position start, Position end, Context ctx) : base(start, end, ctx)
{
dict = vals;
}
override public Value LookupString(string prop)
{
var str = new Str(prop, posStart, posEnd, context);
if (!this.dict.ContainsKey(str)) throw new RuntimeError(posStart, posEnd, string.Format("{0} does not contain {1}", this.ToString(), prop));
return this.dict[str];
}
public IEnumerable<List<Value>> GetIter()
{
return dict.Select(kv => new List<Value>(){kv.Key, kv.Value});
}
public new static ObjectValue Construct(Dictionary<Value, Value> given)
{
return new ObjectValue(given, Position.Nothing(), Position.Nothing(), new Context());
}
public override string ToString()
{
return "{" + string.Join(", ", dict.Select(kv => kv.Key.ToString() + ": " + kv.Value.ToString())) + "}";
}
}
sealed public class FunctionValue : Value, Callable {
int? paramCount;
Func<List<Value>, Value> function;
public FunctionValue(int? count, Func<List<Value>, Value> f) : base(Position.Nothing(), Position.Nothing(), new Context())
{
paramCount = count;
function = f;
}
public FunctionValue(Func<List<Value>, Value> f) : base(Position.Nothing(), Position.Nothing(), new Context())
{
function = f;
}
public static new FunctionValue Construct(int? paramCount, Func<List<Value>, Value> f)
{
return new FunctionValue(paramCount, f);
}
public Value Execute(List<Value> vals)
{
if(paramCount != null && paramCount != vals.Count) throw new RuntimeError(Position.Nothing(), Position.Nothing(), string.Format("Expected {0}, got {1} arguments", paramCount, vals.Count));
return function(vals);
}
public override string ToString()
{
return "<function>";
}
}
sealed public class BlockValue : Value, IterableValue
{
public IEnumerable<Value> values;
public BlockValue(IEnumerable<Value> nodes, Position start, Position end, Context ctx) : base(start, end, ctx)
{
values = nodes;
}
public IEnumerable<List<Value>> GetIter()
{
return values.Select(a => new List<Value>(){a});
}
public new static BlockValue Construct(IEnumerable<Value> given)
{
return new BlockValue(given, Position.Nothing(), Position.Nothing(), new Context());
}
public override string ToString()
{
return string.Concat(values.Select(a => a.ToString()));
}
}