/
CatPrimitives.cs
1950 lines (1717 loc) · 70.5 KB
/
CatPrimitives.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
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/// Dedicated to the public domain by Christopher Diggins
/// http://creativecommons.org/licenses/publicdomain/
using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Threading;
using System.Text;
using System.Text.RegularExpressions;
namespace Cat
{
public class CatException : Exception
{
object data;
public CatException(object o)
{
data = o;
}
public object GetObject()
{
return data;
}
}
public class Primitives
{
#region conversion functions
public class EvalFun : PrimitiveFunction
{
public EvalFun()
: base("eval", "(list string ~> list)", "evaluates a string as a function using the list as a stack", "level2,misc")
{ }
public override void Eval(Executor exec)
{
string s = exec.PopString();
Executor aux = new Executor(exec);
aux.Execute(s);
exec.Push(aux.GetStackAsList());
}
}
public class Str : PrimitiveFunction
{
public Str()
: base("str", "(any -> string)", "converts any value into a string representation.", "level2,strings")
{ }
public override void Eval(Executor exec)
{
exec.Push(Output.ObjectToString(exec.Pop()));
}
}
public class MakeByte : PrimitiveFunction
{
public MakeByte()
: base("int_to_byte", "(int -> byte)", "converts an integer into a byte, throwing away sign and ignoring higher bits", "level1,math,conversion")
{ }
public override void Eval(Executor exec)
{
int n = exec.PopInt();
byte b = (byte)n;
exec.Push(b);
}
}
public class BinStr : PrimitiveFunction
{
public BinStr()
: base("bin_str", "(int -> string)", "converts a number into a binary string representation.", "level2,strings,math,conversion")
{ }
public override void Eval(Executor exec)
{
int n = exec.PopInt();
string s = "";
if (n == 0) s = "0";
while (n > 0)
{
if (n % 2 == 1)
{
s = "1" + s;
}
else
{
s = "0" + s;
}
n /= 2;
}
exec.PushString(n.ToString(s));
}
}
public class HexStr : PrimitiveFunction
{
public HexStr()
: base("hex_str", "(int -> string)", "converts a number into a hexadecimal string representation.", "level2,strings,math,conversion")
{ }
public override void Eval(Executor exec)
{
int n = exec.PopInt();
exec.PushString(n.ToString("x"));
}
}
#endregion
#region primitive function classes
public class Halt : PrimitiveFunction
{
public Halt()
: base("halt", "(int ~> )", "halts the program with an error code", "level2,application")
{ }
public override void Eval(Executor exec)
{
int n = exec.PopInt();
throw new Exception("Program halted with error code " + n.ToString());
}
}
public class Id : PrimitiveFunction
{
public Id()
: base("id", "('a -> 'a)", "does nothing, but requires one item on the stack.", "level1,misc")
{ }
public override void Eval(Executor exec)
{
}
}
public class Eq : PrimitiveFunction
{
public Eq()
: base("eq", "('a 'a -> bool)", "returns true if both items on stack have the same value", "level1,comparison")
{ }
public override void Eval(Executor exec)
{
Object x = exec.Pop();
Object y = exec.Pop();
exec.PushBool(x.Equals(y));
}
}
public class Dup : PrimitiveFunction
{
public Dup()
: base("dup", "('a -> 'a 'a)", "duplicate the top item on the stack", "level0,stack")
{ }
public override void Eval(Executor exec)
{
exec.Dup();
}
}
public class Pop : PrimitiveFunction
{
public Pop()
: base("pop", "('a -> )", "removes the top item from the stack", "level0,stack")
{ }
public override void Eval(Executor exec)
{
exec.Pop();
}
}
public class Swap : PrimitiveFunction
{
public Swap()
: base("swap", "('a 'b -> 'b 'a)", "swap the top two items on the stack", "level0,stack")
{ }
public override void Eval(Executor exec)
{
exec.Swap();
}
}
#endregion
#region function functions
public class ApplyFxn : PrimitiveFunction
{
public ApplyFxn()
: base("apply", "('A ('A -> 'B) -> 'B)", "applies a function to the stack (i.e. executes an instruction)", "level0,functions")
{ }
public override void Eval(Executor exec)
{
Function f = exec.TypedPop<Function>();
f.Eval(exec);
}
}
public class ApplyOneFxn : PrimitiveFunction
{
public ApplyOneFxn()
: base("A", "('a ('a -> 'b) -> 'b)", "applies a unary function to its argument", "functions")
{ }
public override void Eval(Executor exec)
{
Function f = exec.TypedPop<Function>();
f.Eval(exec);
}
}
public class PartialApplyFxn : PrimitiveFunction
{
public PartialApplyFxn()
: base("papply", "('C 'a ('A 'a -> 'B) -> 'C ('A -> 'B))", "partial application: binds the top argument to the top value in the stack", "level0,functions")
{ }
public override void Eval(Executor exec)
{
exec.Execute("swap quote swap compose");
}
}
public class Dip : PrimitiveFunction
{
public Dip()
: base("dip", "('A 'b ('A -> 'C) -> 'C 'b)", "evaluates a function, temporarily removing second item", "level0,functions")
{ }
public override void Eval(Executor exec)
{
Function f = exec.TypedPop<Function>();
Object o = exec.Pop();
f.Eval(exec);
exec.Push(o);
}
}
public class Compose : PrimitiveFunction
{
public Compose()
: base("compose", "(('A -> 'B) ('B -> 'C) -> ('A -> 'C))",
"creates a function by composing (concatenating) two existing functions", "level0,functions")
{ }
public override void Eval(Executor exec)
{
QuotedFunction right = exec.TypedPop<QuotedFunction>();
QuotedFunction left = exec.TypedPop<QuotedFunction>();
QuotedFunction f = new QuotedFunction(left, right);
exec.PushFxn(f);
}
}
public class Pull : PrimitiveFunction
{
public Pull()
: base("pull", "(( -> 'A 'b) -> ( -> 'A) 'b)",
"deconstructs a function", "experimental")
{ }
public override void Eval(Executor exec)
{
QuotedFunction f = exec.TypedPeek<QuotedFunction>();
int n = f.GetSubFxns().Count;
Function g = f.GetSubFxns()[n - 1];
if (((g is PushFunction) || (g is QuotedFunction) || (g is QuotedValue)) || (g is PushValueBase))
{
f.GetSubFxns().RemoveAt(n - 1);
g.Eval(exec);
}
else if (g is PushStack)
{
Executor e2 = (g as PushStack).GetStack();
exec.Push(e2.Pop());
}
else
{
exec.Pop();
Executor e2 = new Executor();
f.Eval(e2);
Object o = e2.Pop();
Function h = new PushStack(e2);
exec.Push(h);
exec.Push(o);
}
}
}
public class Quote : PrimitiveFunction
{
public Quote()
: base("quote", "('a -> ( -> 'a))",
"creates a constant generating function from the top value on the stack", "level0,functions")
{ }
public override void Eval(Executor exec)
{
Object o = exec.Pop();
QuotedValue q = new QuotedValue(o);
exec.PushFxn(q);
}
}
public class Dispatch1 : PrimitiveFunction
{
public Dispatch1()
: base("dispatch1", "('a list -> any)", "dynamically dispatches a function depending on the type on top of the stack", "level1,functions")
{ }
public override void Eval(Executor exec)
{
CatList fs = exec.TypedPop<CatList>();
Object o = exec.Peek();
for (int i = 0; i < fs.Count / 2; ++i)
{
Type t = fs[i * 2 + 1] as Type;
Function f = fs[i * 2] as Function;
if (t.IsInstanceOfType(o))
{
f.Eval(exec);
return;
}
}
throw new Exception("could not dispatch function");
}
}
public class Dispatch2 : PrimitiveFunction
{
public Dispatch2()
: base("dispatch2", "('a 'b list -> any)", "dynamically dispatches a function depending on the type on top of the stack", "level1,functions")
{ }
public override void Eval(Executor exec)
{
CatList fs = exec.TypedPop<CatList>();
Object o = exec.Peek();
for (int i = 0; i < fs.Count / 2; ++i)
{
Type t = fs[i * 2 + 1] as Type;
Function f = fs[i * 2] as Function;
if (t.IsInstanceOfType(o))
{
f.Eval(exec);
return;
}
}
throw new Exception("could not dispatch function");
}
}
#endregion
#region reflection api
public class Explode : PrimitiveFunction
{
public Explode()
: base("explode", "('A -> 'B) -> list)",
"breaks a function up into a list of instructions", "level2,functions")
{ }
public CatList FxnsToList(CatExpr fxns)
{
CatList list = new CatList();
foreach (Function f in fxns )
{
if (f is PushFunction)
{
list.Add(FxnsToList(f.GetSubFxns()));
}
else if (f is DefinedFunction)
{
DefinedFunction def = f as DefinedFunction;
if (f.GetSubFxns().Count > 0)
{
foreach (Function g in f.GetSubFxns())
list.Add(g);
}
else
{
list.Add(f);
}
}
else
{
list.Add(f);
}
}
return list;
}
public override void Eval(Executor exec)
{
QuotedFunction f = exec.TypedPop<QuotedFunction>();
exec.Push(FxnsToList(f.GetSubFxns()));
}
}
#endregion
#region control flow primitives
public class Default : PrimitiveFunction
{
public Default()
: base("default", "('A -> 'B) -> ('A int -> 'B)", "used to construct a default 'case' statement", "level1,control,functions")
{ }
public override void Eval(Executor exec)
{
QuotedFunction f = exec.TypedPop<QuotedFunction>();
f.GetSubFxns().Insert(0, new Pop());
JumpTable jt = new JumpTable(f);
exec.Push(jt);
}
}
public class Case : PrimitiveFunction
{
public Case()
: base("case", "('A int -> 'B) ('A -> 'B) int -> ('A int -> 'B)", "used to construct a 'case' statement member of a switch statement",
"level1,control,functions")
{ }
public override void Eval(Executor exec)
{
int n = exec.PopInt();
Function f = exec.TypedPop<Function>();
Function g = exec.TypedPeek<Function>();
if (g is JumpTable)
{
JumpTable jt = g as JumpTable;
jt.AddCase(n, f);
}
else
{
exec.Pop();
JumpTable jt = new JumpTable(g);
exec.Push(jt);
}
}
}
/*
public class CallCC : PrimitiveFunction
{
public CallCC()
: base("callcc", "('A ('A ('B -> 'C) -> 'B) ~> 'B)", "calls a function with the current continuation", "experimental")
{ }
public override void Eval(Executor exec)
{
throw new Exception("unimplemented");
// TODO: make a copy of the stack, and a pointer to the current instruction.
// this implies that I need to make a copy of the index stream.
}
}*/
public class While : PrimitiveFunction
{
public While()
: base("while", "('A ('A -> 'A) ('A -> 'A bool) -> 'A)",
"executes a block of code repeatedly until the condition returns true", "level1,control")
{ }
public override void Eval(Executor exec)
{
Function cond = exec.TypedPop<Function>();
Function body = exec.TypedPop<Function>();
cond.Eval(exec);
while ((bool)exec.Pop())
{
body.Eval(exec);
cond.Eval(exec);
}
}
}
public class If : PrimitiveFunction
{
public If()
: base("if", "('A bool ('A -> 'B) ('A -> 'B) -> 'B)",
"executes one predicate or another whether the condition is true", "level0,control")
{ }
public override void Eval(Executor exec)
{
Function onfalse = exec.TypedPop<Function>();
Function ontrue = exec.TypedPop<Function>();
if ((bool)exec.Pop()) {
ontrue.Eval(exec);
}
else {
onfalse.Eval(exec);
}
}
}
public class BinRec : PrimitiveFunction
{
// The fact that it takes 'b instead of 'B is a minor optimization for untyped implementations
// I may ignore it later on.
public BinRec()
: base("bin_rec", "('a ('a -> bool) ('a -> 'b) ('a -> 'C 'a 'a) ('C 'b 'b -> 'b) -> 'b)",
"execute a binary recursion process", "level1,control")
{ }
public class BinRecHelper
{
Executor mExec;
Function mResultRelation;
Function mArgRelation;
Function mBaseCase;
Function mCondition;
public BinRecHelper(Executor exec, Function fResultRelation, Function fArgRelation, Function fBaseCase, Function fCondition)
{
mExec = exec;
mResultRelation = fResultRelation;
mArgRelation = fArgRelation;
mBaseCase = fBaseCase;
mCondition = fCondition;
}
public void LocalExec()
{
mCondition.Eval(mExec);
if (mExec.PopBool())
{
mBaseCase.Eval(mExec);
}
else
{
mArgRelation.Eval(mExec);
Object o = mExec.Pop();
LocalExec();
mExec.Push(o);
LocalExec();
mResultRelation.Eval(mExec);
}
}
static public void LaunchThread(Object o)
{
BinRecHelper h = o as BinRecHelper;
try
{
h.LocalExec();
}
finally
{
mWait.Set();
}
}
static EventWaitHandle mWait = new EventWaitHandle(false, EventResetMode.AutoReset);
public void Exec()
{
if (!Config.gbMultiThreadBinRec)
{
LocalExec();
return;
}
mCondition.Eval(mExec);
if (mExec.PopBool())
{
mBaseCase.Eval(mExec);
}
else
{
mArgRelation.Eval(mExec);
Executor e2;
e2 = new Executor();
e2.Push(mExec.Pop());
BinRecHelper h2 = new BinRecHelper(e2, mResultRelation, mArgRelation, mBaseCase, mCondition);
Thread t = new Thread(new ParameterizedThreadStart(LaunchThread));
t.Start(h2);
LocalExec();
mWait.WaitOne();
mExec.Push(e2.Pop());
mResultRelation.Eval(mExec);
}
}
}
public override void Eval(Executor exec)
{
Function fResultRelation = exec.PopFxn();
Function fArgRelation = exec.PopFxn();
Function fBaseCase = exec.PopFxn();
Function fCondition = exec.PopFxn();
BinRecHelper h = new BinRecHelper(exec, fResultRelation, fArgRelation, fBaseCase, fCondition);
h.Exec();
}
}
public class Throw : PrimitiveFunction
{
public Throw()
: base("throw", "(any -> )", "throws an exception", "level2,control")
{ }
public override void Eval(Executor exec)
{
object o = exec.Pop();
throw new CatException(o);
}
}
public class TryCatch : PrimitiveFunction
{
public TryCatch()
: base("try_catch", "(( -> 'A) (exception -> 'A) -> 'A)", "evaluates a function, and catches any exceptions", "level2,control")
{ }
public override void Eval(Executor exec)
{
Function c = exec.TypedPop<Function>();
Function t = exec.TypedPop<Function>();
int n = exec.Count();
try
{
t.Eval(exec);
}
catch (CatException e)
{
exec.ClearTo(n);
Output.WriteLine("exception caught");
exec.Push(e.GetObject());
c.Eval(exec);
}
}
}
#endregion
#region boolean functions
public class True : PrimitiveFunction
{
public True()
: base("true", "( -> bool)", "pushes the boolean value true on the stack", "level0,boolean")
{ }
public override void Eval(Executor exec)
{
exec.PushBool(true);
}
}
public class False : PrimitiveFunction
{
public False()
: base("false", "( -> bool)", "pushes the boolean value false on the stack", "level0,boolean")
{ }
public override void Eval(Executor exec)
{
exec.PushBool(false);
}
}
public class And : PrimitiveFunction
{
public And()
: base("and", "(bool bool -> bool)", "returns true if both of the top two values on the stack are true", "level0,boolean")
{ }
public override void Eval(Executor exec)
{
bool x = (bool)exec.Pop();
bool y = (bool)exec.Pop();
exec.PushBool(x && y);
}
}
public class Or : PrimitiveFunction
{
public Or()
: base("or", "(bool bool -> bool)", "returns true if either of the top two values on the stack are true", "level0,boolean")
{ }
public override void Eval(Executor exec)
{
bool x = (bool)exec.Pop();
bool y = (bool)exec.Pop();
exec.PushBool(x || y);
}
}
public class Not : PrimitiveFunction
{
public Not()
: base("not", "(bool -> bool)", "returns true if the top value on the stack is false", "level0,boolean")
{ }
public override void Eval(Executor exec)
{
exec.PushBool(!(bool)exec.Pop());
}
}
#endregion
#region type functions
public class TypeName : PrimitiveFunction
{
public TypeName()
: base("typename", "(any -> string)", "returns the name of the type of an object", "level1,types")
{ }
public override void Eval(Executor exec)
{
Object o = exec.Pop();
exec.PushString(CatKind.TypeNameFromObject(o));
}
}
public class TypeId : PrimitiveFunction
{
public TypeId()
: base("typeof", "(any -> any type)", "returns a type tag for an object", "level1,types")
{ }
public override void Eval(Executor exec)
{
// TODO: fix this some day
Object o = exec.Peek();
if (o is CatList)
{
// HACK: this is not the correct type!
exec.Push(typeof(CatList));
}
else if (o is Function)
{
// HACK: this is not the correct type!
exec.Push((o as Function).GetFxnType());
}
else
{
// HACK: this is not the correct type!
exec.Push(o.GetType());
}
}
}
public class TypeType : PrimitiveFunction
{
public TypeType()
: base("type_type", "( -> type)", "pushes a value representing the type of a type", "level1,types")
{ }
public override void Eval(Executor exec)
{
exec.Push(typeof(Type));
}
}
public class IntType : PrimitiveFunction
{
public IntType()
: base("int_type", "( -> type)", "pushes a value representing the type of an int", "level1,types")
{ }
public override void Eval(Executor exec)
{
exec.Push(typeof(int));
}
}
public class StrType : PrimitiveFunction
{
public StrType()
: base("string_type", "( -> type)", "pushes a value representing the type of a string", "level1,types")
{ }
public override void Eval(Executor exec)
{
exec.Push(typeof(string));
}
}
public class DblType : PrimitiveFunction
{
public DblType()
: base("double_type", "( -> type)", "pushes a value representing the type of a double", "level1,types")
{ }
public override void Eval(Executor exec)
{
exec.Push(typeof(double));
}
}
public class ByteType : PrimitiveFunction
{
public ByteType()
: base("byte_type", "( -> type)", "pushes a value representing the type of a byte", "level1,types")
{ }
public override void Eval(Executor exec)
{
exec.Push(typeof(byte));
}
}
public class BitType : PrimitiveFunction
{
public BitType()
: base("bit_type", "( -> type)", "pushes a value representing the type of a bit", "level1,types")
{ }
public override void Eval(Executor exec)
{
exec.Push(typeof(Bit));
}
}
public class BoolType : PrimitiveFunction
{
public BoolType()
: base("bool_type", "( -> type)", "pushes a value representing the type of a boolean", "level1,types")
{ }
public override void Eval(Executor exec)
{
exec.Push(typeof(Bit));
}
}
public class TypeEq : PrimitiveFunction
{
public TypeEq()
: base("type_eq", "(type type -> bool)", "returns true if either type is assignable to the other", "level1,types")
{ }
public override void Eval(Executor exec)
{
Type t = exec.TypedPop<Type>();
Type u = exec.TypedPop<Type>();
exec.PushBool(t.Equals(u) || u.Equals(t));
}
}
// TODO: complete the type functions
#endregion
#region date-time functions
public class Now : PrimitiveFunction
{
public Now() : base("now", "( ~> date_time)", "pushes a value representing the current date and time", "level2,datetime") { }
public override void Eval(Executor exec) { exec.Push(DateTime.Now); }
}
public class SubTime : PrimitiveFunction
{
public SubTime() : base("sub_time", "(date_time date_time -> time_span)", "computes the time period between two dates", "level2,datetime") { }
public override void Eval(Executor exec) { DateTime x = exec.TypedPop<DateTime>(); DateTime y = exec.TypedPop<DateTime>(); exec.Push(y - x); }
}
public class AddTime : PrimitiveFunction
{
public AddTime() : base("add_time", "(date_time time_span -> date_time)", "computes a date by adding a time period to a date", "level2,datetime") { }
public override void Eval(Executor exec) { TimeSpan x = exec.TypedPop<TimeSpan>(); DateTime y = exec.TypedPop<DateTime>(); exec.Push(y + x); }
}
public class ToMsec : PrimitiveFunction
{
public ToMsec() : base("to_msec", "(time_span -> int)", "computes the length of a time span in milliseconds", "level2,datetime") { }
public override void Eval(Executor exec) { exec.Push(exec.TypedPop<TimeSpan>().TotalMilliseconds); }
}
#endregion
#region int functions
public class AddInt : PrimitiveFunction
{
public AddInt() : base("add_int", "(int int -> int)", "adds two integers", "level0,math") { }
public override void Eval(Executor exec) { exec.PushInt(exec.PopInt() + exec.PopInt()); }
}
public class MulInt : PrimitiveFunction
{
public MulInt() : base("mul_int", "(int int -> int)", "multiplies two integers", "level0,math") { }
public override void Eval(Executor exec) { exec.PushInt(exec.PopInt() * exec.PopInt()); }
}
public class DivInt : PrimitiveFunction
{
public DivInt() : base("div_int", "(int int -> int)", "divides the top value (an integer) from the second value (an integer)", "level0,math") { }
public override void Eval(Executor exec) { int x = exec.PopInt(); int y = exec.PopInt(); exec.PushInt(y / x); }
}
public class SubInt : PrimitiveFunction
{
public SubInt() : base("sub_int", "(int int -> int)", "subtracts the top value (an integer) from the second value (an integer)", "level0,math") { }
public override void Eval(Executor exec) { int x = exec.PopInt(); int y = exec.PopInt(); exec.PushInt(y - x); }
}
public class ModInt : PrimitiveFunction
{
public ModInt() : base("mod_int", "(int int -> int)", "computes the remainder of dividing the top value (an integer) from the second value (an integer)", "level0,math") { }
public override void Eval(Executor exec) { int x = exec.PopInt(); int y = exec.PopInt(); exec.PushInt(y % x); }
}
public class NegInt : PrimitiveFunction
{
public NegInt() : base("neg_int", "(int -> int)", "negates the top value", "level0,math") { }
public override void Eval(Executor exec) { exec.PushInt(-exec.PopInt()); }
}
public class ComplInt : PrimitiveFunction
{
public ComplInt() : base("compl_int", "(int -> int)", "performs a bitwise complement of the top value", "level0,math") { }
public override void Eval(Executor exec) { exec.PushInt(~exec.PopInt()); }
}
public class ShlInt : PrimitiveFunction
{
public ShlInt() : base("shl_int", "(int int -> int)", "shifts the second value left by the number of bits indicated on the top of the stack", "level0,math") { }
public override void Eval(Executor exec) { exec.Swap(); exec.PushInt(exec.PopInt() << exec.PopInt()); }
}
public class ShrInt : PrimitiveFunction
{
public ShrInt() : base("shr_int", "(int int -> int)", "shifts the second value left by the number of bits indicated on the top of the stack", "level0,math") { }
public override void Eval(Executor exec) { exec.Swap(); exec.PushInt(exec.PopInt() >> exec.PopInt()); }
}
public class GtInt : PrimitiveFunction
{
public GtInt() : base("gt_int", "(int int -> bool)", "replaces the top two values (integers) with true, if the second value is greater than the top value, otherwise pushes false", "level0,math") { }
public override void Eval(Executor exec) { exec.Swap(); exec.PushBool(exec.PopInt() > exec.PopInt()); }
}
public class LtInt : PrimitiveFunction
{
public LtInt() : base("lt_int", "(int int -> bool)", "replaces the top two values (integers) with true, if the second value is less than the top value, otherwise pushes false", "level0,math") { }
public override void Eval(Executor exec) { exec.Swap(); exec.PushBool(exec.PopInt() < exec.PopInt()); }
}
public class GtEqInt : PrimitiveFunction
{
public GtEqInt() : base("gteq_int", "(int int -> bool)", "replaces the top two values (integers) with true, if the second value is greater than or equal to the top value, otherwise pushes false", "level0,math") { }
public override void Eval(Executor exec) { exec.Swap(); exec.PushBool(exec.PopInt() >= exec.PopInt()); }
}
public class LtEqInt : PrimitiveFunction
{
public LtEqInt() : base("lteq_int", "(int int -> bool)", "replaces the top two values (integers) with true, if the second value is less than or equal to the top value, otherwise pushes false", "level0,math") { }
public override void Eval(Executor exec) { exec.Swap(); exec.PushBool(exec.PopInt() <= exec.PopInt()); }
}
#endregion
// Notice at this point I use static functions instead of declaring objects.
// this is simply because I am lazy, and these few dozen operations don't really merit documentation.
#region byte functions
public static byte add_byte(byte x, byte y) { return (byte)(x + y); }
public static byte sub_byte(byte x, byte y) { return (byte)(x - y); }
public static byte div_byte(byte x, byte y) { return (byte)(x / y); }
public static byte mul_byte(byte x, byte y) { return (byte)(x * y); }
public static byte mod_byte(byte x, byte y) { return (byte)(x % y); }
public static byte compl_byte(byte x) { return (byte)(~x); }
public static byte shl_byte(byte x, byte y) { return (byte)(x << y); }
public static byte shr_byte(byte x, byte y) { return (byte)(x >> y); }
public static bool gt_byte(byte x, byte y) { return x > y; }
public static bool lt_byte(byte x, byte y) { return x < y; }
public static bool gteq_byte(byte x, byte y) { return x >= y; }
public static bool lteq_byte(byte x, byte y) { return x <= y; }
#endregion
#region char functions
public static int char_to_int(char c) { return (int)c; }
public static char int_to_char(int n) { return (char)n; }
public static string char_to_str(char c) { return c.ToString(); }
#endregion
#region bit functions
public struct Bit
{
public bool m;
public Bit(int n) { m = n != 0; }
public Bit(bool x) { m = x; }
public Bit add(Bit x) { return new Bit(m ^ x.m); }
public Bit sub(Bit x) { return new Bit(m && !x.m); }
public Bit mul(Bit x) { return new Bit(m && !x.m); }
public Bit div(Bit x) { return new Bit(m && !x.m); }
public Bit mod(Bit x) { return new Bit(m && !x.m); }
public bool lteq(Bit x) { return !m || x.m; }
public bool eq(Bit x) { return m == x.m; }
public override bool Equals(object obj)
{
return (obj is Bit) && (((Bit)obj).m == m);
}
public override int GetHashCode()
{
return m.GetHashCode();
}
public override string ToString()
{
return m ? "0b1" : "0b0";
}
}
public static Bit add_bit(Bit x, Bit y) { return x.add(y); }
public static Bit sub_bit(Bit x, Bit y) { return x.sub(y); }
public static Bit mul_bit(Bit x, Bit y) { return x.mul(y); }
public static Bit div_bit(Bit x, Bit y) { return x.div(y); }
public static Bit mod_bit(Bit x, Bit y) { return x.mod(y); }
public static Bit compl_bit(Bit x) { return new Bit(!x.m); }
public static bool neq_bit(Bit x, Bit y) { return !x.eq(y); }