C#: add step5_tco
[jackhill/mal.git] / cs / core.cs
CommitLineData
afdf531e
JM
1using System;
2using System.Collections.Generic;
b18969c0 3using MalVal = Mal.types.MalVal;
afdf531e 4using MalConstant = Mal.types.MalConstant;
b18969c0 5using MalInteger = Mal.types.MalInteger;
afdf531e 6using MalString = Mal.types.MalString;
b18969c0
JM
7using MalList = Mal.types.MalList;
8using MalFunction = Mal.types.MalFunction;
9
10namespace Mal {
11 public class core {
afdf531e
JM
12 static MalConstant Nil = Mal.types.Nil;
13 static MalConstant True = Mal.types.True;
14 static MalConstant False = Mal.types.False;
15
16 // String functions
17 static public MalFunction pr_str = new MalFunction(
18 a => new MalString(printer._pr_str_args(a, " ", true)) );
19
20 static public MalFunction str = new MalFunction(
21 a => new MalString(printer._pr_str_args(a, "", false)) );
22
23 static public MalFunction prn = new MalFunction(
24 a => {
25 Console.WriteLine(printer._pr_str_args(a, " ", true));
26 return Nil;
27 } );
28
29 static public MalFunction println = new MalFunction(
30 a => {
31 Console.WriteLine(printer._pr_str_args(a, " ", false));
32 return Nil;
33 } );
34
35 // Sequence functions
36 static public MalFunction list_Q = new MalFunction(
37 a => a[0].GetType() == typeof(MalList) ? True : False);
38
39
40
41 static public Dictionary<string, MalVal> ns =
42 new Dictionary<string, MalVal> {
43 {"=", new MalFunction(
44 a => Mal.types._equal_Q(a[0], a[1]) ? True : False)},
45 {"pr-str", pr_str},
46 {"str", str},
47 {"prn", prn},
48 {"println", println},
49 {"<", new MalFunction(a => (MalInteger)a[0] < (MalInteger)a[1])},
50 {"<=", new MalFunction(a => (MalInteger)a[0] <= (MalInteger)a[1])},
51 {">", new MalFunction(a => (MalInteger)a[0] > (MalInteger)a[1])},
52 {">=", new MalFunction(a => (MalInteger)a[0] >= (MalInteger)a[1])},
53 {"+", new MalFunction(a => (MalInteger)a[0] + (MalInteger)a[1])},
54 {"-", new MalFunction(a => (MalInteger)a[0] - (MalInteger)a[1])},
55 {"*", new MalFunction(a => (MalInteger)a[0] * (MalInteger)a[1])},
56 {"/", new MalFunction(a => (MalInteger)a[0] / (MalInteger)a[1])},
57
58 {"list", new MalFunction(a => new MalList(a.getValue()))},
59 {"list?", list_Q},
60 {"first", new MalFunction(a => ((MalList)a[0]).nth(0))},
61 {"rest", new MalFunction(a => ((MalList)a[0]).rest())},
62 {"count", new MalFunction(
63 a => new MalInteger(((MalList)a[0]).size()))},
64 {"empty?", new MalFunction(
65 a => ((MalList)a[0]).size() == 0 ? True : False)},
66 };
b18969c0
JM
67 }
68}