1
1
mirror of https://github.com/kanaka/mal.git synced 2024-11-14 11:36:18 +03:00
mal/cs/core.cs

382 lines
13 KiB
C#
Raw Normal View History

2014-04-07 09:17:13 +04:00
using System;
using System.IO;
2014-04-07 09:17:13 +04:00
using System.Collections.Generic;
using MalVal = Mal.types.MalVal;
2014-04-07 09:17:13 +04:00
using MalConstant = Mal.types.MalConstant;
using MalInt = Mal.types.MalInt;
2014-04-10 06:26:35 +04:00
using MalSymbol = Mal.types.MalSymbol;
2014-04-07 09:17:13 +04:00
using MalString = Mal.types.MalString;
using MalList = Mal.types.MalList;
2014-04-10 06:26:35 +04:00
using MalVector = Mal.types.MalVector;
using MalHashMap = Mal.types.MalHashMap;
2014-04-10 06:57:50 +04:00
using MalAtom = Mal.types.MalAtom;
using MalFunc = Mal.types.MalFunc;
namespace Mal {
public class core {
2014-04-07 09:17:13 +04:00
static MalConstant Nil = Mal.types.Nil;
static MalConstant True = Mal.types.True;
static MalConstant False = Mal.types.False;
2014-04-10 06:26:35 +04:00
// Errors/Exceptions
static public MalFunc mal_throw = new MalFunc(
2014-04-10 06:26:35 +04:00
a => { throw new Mal.types.MalException(a[0]); });
// Scalar functions
static MalFunc nil_Q = new MalFunc(
2014-04-10 06:26:35 +04:00
a => a[0] == Nil ? True : False);
static MalFunc true_Q = new MalFunc(
2014-04-10 06:26:35 +04:00
a => a[0] == True ? True : False);
static MalFunc false_Q = new MalFunc(
2014-04-10 06:26:35 +04:00
a => a[0] == False ? True : False);
static MalFunc symbol_Q = new MalFunc(
2014-04-10 06:26:35 +04:00
a => a[0] is MalSymbol ? True : False);
2016-02-12 19:37:03 +03:00
static MalFunc string_Q = new MalFunc(
a => {
if (a[0] is MalString) {
var s = ((MalString)a[0]).getValue();
return (s.Length == 0 || s[0] != '\u029e') ? True : False;
} else {
return False;
}
} );
static MalFunc keyword = new MalFunc(
a => {
if (a[0] is MalString &&
((MalString)a[0]).getValue()[0] == '\u029e') {
return a[0];
} else {
return new MalString("\u029e" + ((MalString)a[0]).getValue());
}
} );
static MalFunc keyword_Q = new MalFunc(
a => {
2016-02-12 19:37:03 +03:00
if (a[0] is MalString) {
var s = ((MalString)a[0]).getValue();
return (s.Length > 0 && s[0] == '\u029e') ? True : False;
} else {
return False;
}
} );
2014-04-10 06:26:35 +04:00
// Number functions
static MalFunc time_ms = new MalFunc(
a => new MalInt(DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond));
2014-04-07 09:17:13 +04:00
// String functions
static public MalFunc pr_str = new MalFunc(
2014-04-07 09:17:13 +04:00
a => new MalString(printer._pr_str_args(a, " ", true)) );
static public MalFunc str = new MalFunc(
2014-04-07 09:17:13 +04:00
a => new MalString(printer._pr_str_args(a, "", false)) );
static public MalFunc prn = new MalFunc(
2014-04-07 09:17:13 +04:00
a => {
Console.WriteLine(printer._pr_str_args(a, " ", true));
return Nil;
} );
static public MalFunc println = new MalFunc(
2014-04-07 09:17:13 +04:00
a => {
Console.WriteLine(printer._pr_str_args(a, " ", false));
return Nil;
} );
static public MalFunc mal_readline = new MalFunc(
a => {
var line = readline.Readline(((MalString)a[0]).getValue());
if (line == null) { return types.Nil; }
else { return new MalString(line); }
} );
static public MalFunc read_string = new MalFunc(
a => reader.read_str(((MalString)a[0]).getValue()));
static public MalFunc slurp = new MalFunc(
a => new MalString(File.ReadAllText(
((MalString)a[0]).getValue())));
2014-04-10 06:26:35 +04:00
// List/Vector functions
static public MalFunc list_Q = new MalFunc(
2014-04-07 09:17:13 +04:00
a => a[0].GetType() == typeof(MalList) ? True : False);
static public MalFunc vector_Q = new MalFunc(
2014-04-10 06:26:35 +04:00
a => a[0].GetType() == typeof(MalVector) ? True : False);
// HashMap functions
static public MalFunc hash_map_Q = new MalFunc(
2014-04-10 06:26:35 +04:00
a => a[0].GetType() == typeof(MalHashMap) ? True : False);
static MalFunc contains_Q = new MalFunc(
2014-04-10 06:26:35 +04:00
a => {
string key = ((MalString)a[1]).getValue();
var dict = ((MalHashMap)a[0]).getValue();
return dict.ContainsKey(key) ? True : False;
});
static MalFunc assoc = new MalFunc(
2014-04-10 06:26:35 +04:00
a => {
var new_hm = ((MalHashMap)a[0]).copy();
return new_hm.assoc_BANG((MalList)a.slice(1));
});
static MalFunc dissoc = new MalFunc(
2014-04-10 06:26:35 +04:00
a => {
var new_hm = ((MalHashMap)a[0]).copy();
return new_hm.dissoc_BANG((MalList)a.slice(1));
});
static MalFunc get = new MalFunc(
2014-04-10 06:26:35 +04:00
a => {
string key = ((MalString)a[1]).getValue();
2014-04-15 10:24:43 +04:00
if (a[0] == Nil) {
return Nil;
} else {
var dict = ((MalHashMap)a[0]).getValue();
return dict.ContainsKey(key) ? dict[key] : Nil;
}
2014-04-10 06:26:35 +04:00
});
static MalFunc keys = new MalFunc(
2014-04-10 06:26:35 +04:00
a => {
var dict = ((MalHashMap)a[0]).getValue();
MalList key_lst = new MalList();
foreach (var key in dict.Keys) {
key_lst.conj_BANG(new MalString(key));
}
return key_lst;
});
static MalFunc vals = new MalFunc(
2014-04-10 06:26:35 +04:00
a => {
var dict = ((MalHashMap)a[0]).getValue();
MalList val_lst = new MalList();
foreach (var val in dict.Values) {
val_lst.conj_BANG(val);
}
return val_lst;
});
// Sequence functions
static public MalFunc sequential_Q = new MalFunc(
2014-04-10 06:26:35 +04:00
a => a[0] is MalList ? True : False);
2014-04-08 07:53:53 +04:00
static MalFunc cons = new MalFunc(
2014-04-08 07:40:30 +04:00
a => {
var lst = new List<MalVal>();
lst.Add(a[0]);
lst.AddRange(((MalList)a[1]).getValue());
return (MalVal)new MalList(lst);
});
static MalFunc concat = new MalFunc(
2014-04-08 07:40:30 +04:00
a => {
if (a.size() == 0) { return new MalList(); }
var lst = new List<MalVal>();
lst.AddRange(((MalList)a[0]).getValue());
for(int i=1; i<a.size(); i++) {
lst.AddRange(((MalList)a[i]).getValue());
}
return (MalVal)new MalList(lst);
});
static MalFunc nth = new MalFunc(
a => {
var idx = (int)((MalInt)a[1]).getValue();
if (idx < ((MalList)a[0]).size()) {
return ((MalList)a[0])[idx];
} else {
throw new Mal.types.MalException(
"nth: index out of range");
}
});
2014-04-10 06:26:35 +04:00
static MalFunc first = new MalFunc(
a => a[0] == Nil ? Nil : ((MalList)a[0])[0]);
2014-04-10 06:26:35 +04:00
static MalFunc rest = new MalFunc(
a => a[0] == Nil ? new MalList() : ((MalList)a[0]).rest());
2014-04-10 06:26:35 +04:00
static MalFunc empty_Q = new MalFunc(
2014-04-10 06:26:35 +04:00
a => ((MalList)a[0]).size() == 0 ? True : False);
static MalFunc count = new MalFunc(
a => {
return (a[0] == Nil)
? new MalInt(0)
:new MalInt(((MalList)a[0]).size());
});
2014-04-10 06:26:35 +04:00
static MalFunc conj = new MalFunc(
2014-04-10 06:26:35 +04:00
a => {
var src_lst = ((MalList)a[0]).getValue();
var new_lst = new List<MalVal>();
new_lst.AddRange(src_lst);
if (a[0] is MalVector) {
for(int i=1; i<a.size(); i++) {
new_lst.Add(a[i]);
}
return new MalVector(new_lst);
} else {
for(int i=1; i<a.size(); i++) {
new_lst.Insert(0, a[i]);
}
return new MalList(new_lst);
}
});
2016-02-12 19:37:03 +03:00
static MalFunc seq = new MalFunc(
a => {
if (a[0] == Nil) {
return Nil;
} else if (a[0] is MalVector) {
return (((MalVector)a[0]).size() == 0)
? (MalVal)Nil
: new MalList(((MalVector)a[0]).getValue());
} else if (a[0] is MalList) {
return (((MalList)a[0]).size() == 0)
? Nil
: a[0];
} else if (a[0] is MalString) {
var s = ((MalString)a[0]).getValue();
if (s.Length == 0) {
return Nil;
}
var chars_list = new List<MalVal>();
foreach (var c in s) {
chars_list.Add(new MalString(c.ToString()));
}
return new MalList(chars_list);
}
return Nil;
});
2014-04-10 06:26:35 +04:00
// General list related functions
static MalFunc apply = new MalFunc(
2014-04-10 06:26:35 +04:00
a => {
var f = (MalFunc)a[0];
2014-04-10 06:26:35 +04:00
var lst = new List<MalVal>();
lst.AddRange(a.slice(1,a.size()-1).getValue());
lst.AddRange(((MalList)a[a.size()-1]).getValue());
return f.apply(new MalList(lst));
});
static MalFunc map = new MalFunc(
2014-04-10 06:26:35 +04:00
a => {
MalFunc f = (MalFunc) a[0];
2014-04-10 06:26:35 +04:00
var src_lst = ((MalList)a[1]).getValue();
var new_lst = new List<MalVal>();
for(int i=0; i<src_lst.Count; i++) {
new_lst.Add(f.apply(new MalList(src_lst[i])));
}
return new MalList(new_lst);
});
2014-04-08 07:40:30 +04:00
2014-04-10 06:57:50 +04:00
// Metadata functions
static MalFunc meta = new MalFunc(
2014-04-10 06:57:50 +04:00
a => a[0].getMeta());
static MalFunc with_meta = new MalFunc(
2014-04-10 06:57:50 +04:00
a => ((MalVal)a[0]).copy().setMeta(a[1]));
// Atom functions
static MalFunc atom_Q = new MalFunc(
2014-04-10 06:57:50 +04:00
a => a[0] is MalAtom ? True : False);
static MalFunc deref = new MalFunc(
2014-04-10 06:57:50 +04:00
a => ((MalAtom)a[0]).getValue());
static MalFunc reset_BANG = new MalFunc(
2014-04-10 06:57:50 +04:00
a => ((MalAtom)a[0]).setValue(a[1]));
static MalFunc swap_BANG = new MalFunc(
2014-04-10 06:57:50 +04:00
a => {
MalAtom atm = (MalAtom)a[0];
MalFunc f = (MalFunc)a[1];
2014-04-10 06:57:50 +04:00
var new_lst = new List<MalVal>();
new_lst.Add(atm.getValue());
new_lst.AddRange(((MalList)a.slice(2)).getValue());
return atm.setValue(f.apply(new MalList(new_lst)));
});
2014-04-07 09:17:13 +04:00
static public Dictionary<string, MalVal> ns =
new Dictionary<string, MalVal> {
{"=", new MalFunc(
2014-04-07 09:17:13 +04:00
a => Mal.types._equal_Q(a[0], a[1]) ? True : False)},
2014-04-10 06:26:35 +04:00
{"throw", mal_throw},
{"nil?", nil_Q},
{"true?", true_Q},
{"false?", false_Q},
{"symbol", new MalFunc(a => new MalSymbol((MalString)a[0]))},
2014-04-10 06:26:35 +04:00
{"symbol?", symbol_Q},
2016-02-12 19:37:03 +03:00
{"string?", string_Q},
{"keyword", keyword},
{"keyword?", keyword_Q},
2014-04-07 09:17:13 +04:00
{"pr-str", pr_str},
{"str", str},
{"prn", prn},
{"println", println},
{"readline", mal_readline},
{"read-string", read_string},
{"slurp", slurp},
{"<", new MalFunc(a => (MalInt)a[0] < (MalInt)a[1])},
{"<=", new MalFunc(a => (MalInt)a[0] <= (MalInt)a[1])},
{">", new MalFunc(a => (MalInt)a[0] > (MalInt)a[1])},
{">=", new MalFunc(a => (MalInt)a[0] >= (MalInt)a[1])},
{"+", new MalFunc(a => (MalInt)a[0] + (MalInt)a[1])},
{"-", new MalFunc(a => (MalInt)a[0] - (MalInt)a[1])},
{"*", new MalFunc(a => (MalInt)a[0] * (MalInt)a[1])},
{"/", new MalFunc(a => (MalInt)a[0] / (MalInt)a[1])},
{"time-ms", time_ms},
2014-04-07 09:17:13 +04:00
{"list", new MalFunc(a => new MalList(a.getValue()))},
2014-04-07 09:17:13 +04:00
{"list?", list_Q},
{"vector", new MalFunc(a => new MalVector(a.getValue()))},
2014-04-10 06:26:35 +04:00
{"vector?", vector_Q},
{"hash-map", new MalFunc(a => new MalHashMap(a))},
2014-04-10 06:26:35 +04:00
{"map?", hash_map_Q},
{"contains?", contains_Q},
{"assoc", assoc},
{"dissoc", dissoc},
{"get", get},
{"keys", keys},
{"vals", vals},
2014-04-08 07:40:30 +04:00
2014-04-10 06:26:35 +04:00
{"sequential?", sequential_Q},
2014-04-08 07:40:30 +04:00
{"cons", cons},
{"concat", concat},
2014-04-08 07:53:53 +04:00
{"nth", nth},
2014-04-10 06:26:35 +04:00
{"first", first},
{"rest", rest},
{"empty?", empty_Q},
{"count", count},
{"conj", conj},
2016-02-12 19:37:03 +03:00
{"seq", seq},
2014-04-10 06:26:35 +04:00
{"apply", apply},
{"map", map},
2014-04-10 06:57:50 +04:00
{"with-meta", with_meta},
{"meta", meta},
{"atom", new MalFunc(a => new MalAtom(a[0]))},
2014-04-10 06:57:50 +04:00
{"atom?", atom_Q},
{"deref", deref},
{"reset!", reset_BANG},
{"swap!", swap_BANG},
2014-04-07 09:17:13 +04:00
};
}
}