diff options
| author | Joel Martin <github@martintribe.org> | 2014-04-07 00:17:13 -0500 |
|---|---|---|
| committer | Joel Martin <github@martintribe.org> | 2014-04-07 00:17:13 -0500 |
| commit | afdf531eba459a7a7b6505b037dbe48a363c2c79 (patch) | |
| tree | b899e21b373ac8338756140eaf7afcb399d46bb9 /cs/types.cs | |
| parent | b18969c0b8d47d67d4b73b5b20742a0bc3179e72 (diff) | |
| download | mal-afdf531eba459a7a7b6505b037dbe48a363c2c79.tar.gz mal-afdf531eba459a7a7b6505b037dbe48a363c2c79.zip | |
CS: add step4_if_fn_do
Diffstat (limited to 'cs/types.cs')
| -rw-r--r-- | cs/types.cs | 92 |
1 files changed, 70 insertions, 22 deletions
diff --git a/cs/types.cs b/cs/types.cs index 8f6755f..ae9d4bf 100644 --- a/cs/types.cs +++ b/cs/types.cs @@ -4,6 +4,9 @@ using Mal; namespace Mal { public class types { + // + // Exceptiosn/Errors + // public class MalThrowable : Exception { public MalThrowable() : base() { } public MalThrowable(string msg) : base(msg) { } @@ -25,6 +28,41 @@ namespace Mal { 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 MalInteger) { + return ((MalInteger)a).getValue() == + ((MalInteger)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 { // Default is just to call regular toString() @@ -63,29 +101,29 @@ namespace Mal { public override string ToString(bool print_readably) { return value.ToString(); } - public MalInteger add(MalInteger other) { - return new MalInteger(value + other.getValue()); + public static MalConstant operator <(MalInteger a, MalInteger b) { + return a.getValue() < b.getValue() ? True : False; } - public MalInteger subtract(MalInteger other) { - return new MalInteger(value - other.getValue()); + public static MalConstant operator <=(MalInteger a, MalInteger b) { + return a.getValue() <= b.getValue() ? True : False; } - public MalInteger multiply(MalInteger other) { - return new MalInteger(value * other.getValue()); + public static MalConstant operator >(MalInteger a, MalInteger b) { + return a.getValue() > b.getValue() ? True : False; } - public MalInteger divide(MalInteger other) { - return new MalInteger(value / other.getValue()); + public static MalConstant operator >=(MalInteger a, MalInteger b) { + return a.getValue() >= b.getValue() ? True : False; } - public MalConstant lt(MalInteger other) { - return (value < other.getValue()) ? True : False; + public static MalInteger operator +(MalInteger a, MalInteger b) { + return new MalInteger(a.getValue() + b.getValue()); } - public MalConstant lte(MalInteger other) { - return (value <= other.getValue()) ? True : False; + public static MalInteger operator -(MalInteger a, MalInteger b) { + return new MalInteger(a.getValue() - b.getValue()); } - public MalConstant gt(MalInteger other) { - return (value > other.getValue()) ? True : False; + public static MalInteger operator *(MalInteger a, MalInteger b) { + return new MalInteger(a.getValue() * b.getValue()); } - public MalConstant gte(MalInteger other) { - return (value >= other.getValue()) ? True : False; + public static MalInteger operator /(MalInteger a, MalInteger b) { + return new MalInteger(a.getValue() / b.getValue()); } } @@ -114,8 +152,9 @@ namespace Mal { } public override string ToString(bool print_readably) { if (print_readably) { - //return "\"" + printer.escapeString(value) + "\""; - return "\"" + value + "\""; + return "\"" + value.Replace("\\", "\\\\") + .Replace("\"", "\\\"") + .Replace("\n", "\\n") + "\""; } else { return value; } @@ -160,6 +199,9 @@ namespace Mal { public int size() { return value.Count; } public MalVal nth(int idx) { return value[idx]; } + public MalVal this[int idx] { + get { return value[idx]; } + } public MalList rest() { if (size() > 0) { return new MalList(value.GetRange(1, value.Count-1)); @@ -229,7 +271,7 @@ namespace Mal { public override string ToString() { return "{" + printer.join(value, " ", true) + "}"; } - public override string ToString(Boolean print_readably) { + public override string ToString(bool print_readably) { return "{" + printer.join(value, " ", print_readably) + "}"; } @@ -247,9 +289,15 @@ namespace Mal { } } - public abstract class MalFunction : MalVal { - public abstract MalVal apply(MalList args); - } + public class MalFunction : MalVal { + Func<MalList, MalVal> value = null; + public MalFunction(Func<MalList, MalVal> f) { + value = f; + } + public MalVal apply(MalList args) { + return value(args); + } + } } } |
