2024-06-29 18:29:40 +02:00
|
|
|
using System.Reflection;
|
|
|
|
|
2024-06-27 01:47:44 +02:00
|
|
|
using Jellyfin.Plugin.SmartPlaylist.Lisp.Compiler;
|
|
|
|
|
|
|
|
namespace Jellyfin.Plugin.SmartPlaylist.Lisp {
|
2024-11-07 00:48:56 +01:00
|
|
|
using Function = Func<IEnumerable<Expression>, Expression>;
|
|
|
|
using FunctionLater = Func<Executor, IEnumerable<Expression>, Expression>;
|
2024-10-26 03:49:52 +02:00
|
|
|
|
|
|
|
public interface IEnvironment<K, V> {
|
2024-11-11 18:00:55 +01:00
|
|
|
public V? Get(K k);
|
2024-10-26 03:49:52 +02:00
|
|
|
public void Set(K k, V v);
|
|
|
|
public IEnvironment<K, V>? Find(K k);
|
2024-11-07 00:48:56 +01:00
|
|
|
public IEnvironment<K, V> Parent(bool recursive);
|
2024-10-26 03:49:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public class Environment : Dictionary<string, Expression>, IEnvironment<string, Expression> {
|
|
|
|
public Expression? Get(string k) {
|
2024-11-07 00:48:56 +01:00
|
|
|
if (TryGetValue(k, out Expression? v)) {
|
2024-10-26 03:49:52 +02:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
public void Set(string k, Expression v) {
|
2024-10-27 00:52:43 +02:00
|
|
|
this[k] = v;
|
2024-10-26 03:49:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public IEnvironment<string, Expression>? Find(string k) {
|
|
|
|
if (ContainsKey(k)) {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2024-11-07 00:48:56 +01:00
|
|
|
|
|
|
|
public IEnvironment<string, Expression> Parent(bool recursive) {
|
|
|
|
return this;
|
|
|
|
}
|
2024-10-26 03:49:52 +02:00
|
|
|
}
|
|
|
|
|
2024-10-27 00:54:40 +02:00
|
|
|
public class DefaultEnvironment: Environment {
|
|
|
|
public DefaultEnvironment() {
|
2024-11-07 00:48:56 +01:00
|
|
|
var e = new Executor();
|
|
|
|
this["null"] = new Symbol("not");
|
|
|
|
this["list"] = e.eval("(lambda (. args) args)");
|
|
|
|
this["find"] = e.eval("(lambda (item list_) (if (null list_) nil (if (= item (car list_)) (car list_) (find item (cdr list_)))))");
|
|
|
|
this["map"] = e.eval("(lambda (fc l) (if (null l) nil (cons (fc (car l)) (map fc (cdr l)))))");
|
2024-11-08 03:08:29 +01:00
|
|
|
this["fold"] = e.eval("(lambda (fc i l) (if (null l) i (fold fc (fc i (car l)) (cdr l))))");
|
|
|
|
this["any"] = e.eval("(lambda (fc l) (apply or (map fc l)))");
|
|
|
|
this["all"] = e.eval("(lambda (fc l) (apply and (map fc l)))");
|
2024-11-19 21:15:41 +01:00
|
|
|
this["append"] = e.eval("(lambda (l i) (if (null l) i (cons (car l) (append (cdr l) i))))");
|
|
|
|
this["qsort"] = e.eval(
|
|
|
|
"""
|
|
|
|
(lambda
|
|
|
|
(fc list00)
|
|
|
|
(let
|
|
|
|
(getpivot
|
|
|
|
(lambda
|
|
|
|
(list0)
|
|
|
|
(car list0)))
|
|
|
|
(split
|
|
|
|
(lambda
|
|
|
|
(list0 pivot fc h0 h1)
|
|
|
|
(cond
|
|
|
|
((null list0) (list list0 pivot fc h0 h1))
|
|
|
|
((fc (car list0) pivot) (split (cdr list0) pivot fc h0 (cons (car list0) h1)))
|
|
|
|
(t (split (cdr list0) pivot fc (cons (car list0) h0) h1)))))
|
|
|
|
(sort
|
|
|
|
(lambda
|
|
|
|
(fc list0)
|
|
|
|
(cond
|
|
|
|
((null list0) nil)
|
|
|
|
((null (cdr list0)) list0)
|
|
|
|
(t
|
|
|
|
(let*
|
|
|
|
(halves (split list0 (getpivot list0) fc nil nil))
|
|
|
|
(h0 (car (cdr (cdr (cdr halves)))))
|
|
|
|
(h1 (car (cdr (cdr (cdr (cdr halves))))))
|
|
|
|
(append (sort fc h0) (sort fc h1)))))))
|
|
|
|
(sort fc list00)))
|
|
|
|
"""
|
|
|
|
);
|
2024-10-27 00:54:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-26 03:49:52 +02:00
|
|
|
public class SubEnvironment : Dictionary<string, Expression>, IEnvironment<string, Expression> {
|
|
|
|
private IEnvironment<string, Expression> _super;
|
|
|
|
public SubEnvironment(IEnvironment<string, Expression> super) {
|
|
|
|
_super = super;
|
|
|
|
}
|
|
|
|
public Expression? Get(string k) {
|
2024-11-07 00:48:56 +01:00
|
|
|
if (TryGetValue(k, out Expression? v)) {
|
2024-10-26 03:49:52 +02:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
public void Set(string k, Expression v) {
|
|
|
|
Add(k, v);
|
|
|
|
}
|
|
|
|
|
|
|
|
public IEnvironment<string, Expression>? Find(string k) {
|
|
|
|
if (ContainsKey(k)) {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
return _super.Find(k);
|
|
|
|
}
|
2024-11-07 00:48:56 +01:00
|
|
|
|
|
|
|
public IEnvironment<string, Expression> Parent(bool recursive) {
|
|
|
|
if (recursive) {
|
|
|
|
return this._super.Parent(recursive);
|
|
|
|
}
|
|
|
|
return this._super;
|
|
|
|
}
|
2024-10-26 03:49:52 +02:00
|
|
|
}
|
|
|
|
|
2024-06-29 18:29:40 +02:00
|
|
|
public class Builtins : Dictionary<string, Function> {
|
2024-12-17 17:56:14 +01:00
|
|
|
|
|
|
|
private static Dictionary<string, Assembly> AssemblyMapping = new Dictionary<string, Assembly>();
|
|
|
|
|
2024-06-29 18:29:40 +02:00
|
|
|
public Builtins() : base() {
|
2024-11-07 00:48:56 +01:00
|
|
|
this["atom"] = _atom;
|
|
|
|
this["eq"] = _eq;
|
2024-06-29 18:29:40 +02:00
|
|
|
this["car"] = _car;
|
|
|
|
this["cdr"] = _cdr;
|
|
|
|
this["cons"] = _cons;
|
2024-11-07 00:48:56 +01:00
|
|
|
|
|
|
|
this["begin"] = _begin;
|
|
|
|
|
|
|
|
this["+"] = (x) => _agg((Integer a, Integer b) => a + b, x);
|
|
|
|
this["-"] = (x) => _agg((Integer a, Integer b) => a - b, x);
|
|
|
|
this["*"] = (x) => _agg((Integer a, Integer b) => a * b, x);
|
|
|
|
this["/"] = (x) => _agg((Integer a, Integer b) => a / b, x);
|
|
|
|
this["%"] = (x) => _agg((Integer a, Integer b) => a % b, x);
|
|
|
|
|
2024-11-19 21:57:34 +01:00
|
|
|
this["="] = (x) => _cmp((Atom a, Atom b) => (a == b)? Boolean.TRUE : Boolean.FALSE, x);
|
|
|
|
this["!="] = (x) => _cmp((Atom a, Atom b) => (a != b)? Boolean.TRUE : Boolean.FALSE, x);
|
2024-11-07 00:48:56 +01:00
|
|
|
this["<"] = (x) => _cmp((Integer a, Integer b) => a < b, x);
|
|
|
|
this["<="] = (x) => _cmp((Integer a, Integer b) => a <= b, x);
|
|
|
|
this[">"] = (x) => _cmp((Integer a, Integer b) => a > b, x);
|
|
|
|
this[">="] = (x) => _cmp((Integer a, Integer b) => a >= b, x);
|
2024-11-08 03:08:29 +01:00
|
|
|
this["not"] = (x) => {
|
|
|
|
return (x.First() == Boolean.FALSE) ? Boolean.TRUE : Boolean.FALSE;
|
|
|
|
};
|
2024-11-19 21:57:34 +01:00
|
|
|
this["string="] = (x) => _cmp((String a, String b) => a == b, x);
|
|
|
|
this["string!="] = (x) => _cmp((String a, String b) => a != b, x);
|
|
|
|
this["string>"] = (x) => _cmp((String a, String b) => a > b, x);
|
|
|
|
this["string>="] = (x) => _cmp((String a, String b) => a >= b, x);
|
|
|
|
this["string<"] = (x) => _cmp((String a, String b) => a < b, x);
|
|
|
|
this["string<="] = (x) => _cmp((String a, String b) => a <= b, x);
|
2024-11-08 03:08:29 +01:00
|
|
|
|
2024-11-07 00:48:56 +01:00
|
|
|
|
2024-06-29 18:29:40 +02:00
|
|
|
this["haskeys"] = _haskeys;
|
|
|
|
this["getitems"] = _getitems;
|
2024-10-25 02:18:13 +02:00
|
|
|
this["invoke"] = _invoke;
|
2024-12-17 17:56:14 +01:00
|
|
|
this["invoke-generic"] = _invoke_generic;
|
2024-06-27 01:47:44 +02:00
|
|
|
}
|
2024-11-07 00:48:56 +01:00
|
|
|
private static T _agg<T>(Func<T, T, T> op, IEnumerable<Expression> args) where T : Expression {
|
|
|
|
T agg = (T) args.First();
|
2024-06-27 01:47:44 +02:00
|
|
|
foreach (var arg in args.Skip(1)) {
|
2024-11-07 00:48:56 +01:00
|
|
|
var arg_ = (T) arg;
|
|
|
|
agg = op(agg, arg_);
|
2024-06-27 01:47:44 +02:00
|
|
|
}
|
|
|
|
return agg;
|
|
|
|
}
|
2024-11-07 00:48:56 +01:00
|
|
|
private static E _cmp<T, E>(Func<T, T, E> op, IEnumerable<Expression> args) where T : Expression where E : Expression {
|
|
|
|
return op((T) args.First(), (T) args.Skip(1).First());
|
2024-06-27 01:47:44 +02:00
|
|
|
}
|
2024-11-07 00:48:56 +01:00
|
|
|
private static Expression _atom(IEnumerable<Expression> args) {
|
|
|
|
return (args.First() is Atom) ? Boolean.TRUE : Boolean.FALSE;
|
2024-06-27 01:47:44 +02:00
|
|
|
}
|
2024-11-07 00:48:56 +01:00
|
|
|
private static Expression _eq(IEnumerable<Expression> args) {
|
|
|
|
return args.First().Equals(args.Skip(1).First()) ? Boolean.TRUE : Boolean.FALSE;
|
2024-06-27 01:47:44 +02:00
|
|
|
}
|
2024-11-07 00:48:56 +01:00
|
|
|
private static Expression _car(IEnumerable<Expression> args) {
|
|
|
|
return ((Cons)args.First()).Item1;
|
2024-06-27 01:47:44 +02:00
|
|
|
}
|
2024-11-07 00:48:56 +01:00
|
|
|
private static Expression _cdr(IEnumerable<Expression> args) {
|
|
|
|
return ((Cons)args.First()).Item2;
|
2024-06-27 01:47:44 +02:00
|
|
|
}
|
2024-11-07 00:48:56 +01:00
|
|
|
private static Expression _cons(IEnumerable<Expression> args) {
|
|
|
|
return new Cons(args.First(), args.Skip(1).First());
|
2024-06-27 01:47:44 +02:00
|
|
|
}
|
2024-11-07 00:48:56 +01:00
|
|
|
private static Expression _begin(IEnumerable<Expression> args) {
|
2024-06-29 18:29:40 +02:00
|
|
|
return args.Last();
|
|
|
|
}
|
2024-11-07 00:48:56 +01:00
|
|
|
private static Expression _haskeys(IEnumerable<Expression> args) {
|
2024-11-08 22:39:56 +01:00
|
|
|
Object o = new Object(((IInner) args.First()).Inner());
|
2024-06-29 18:29:40 +02:00
|
|
|
foreach (var e in args.Skip(1)) {
|
2024-11-07 00:48:56 +01:00
|
|
|
String s = (String) e;
|
|
|
|
PropertyInfo? pi = o.Value().GetType().GetProperty(s.Value());
|
2024-06-29 18:29:40 +02:00
|
|
|
if (pi != null) {
|
|
|
|
continue;
|
|
|
|
}
|
2024-11-07 00:48:56 +01:00
|
|
|
MethodInfo? mi = o.Value().GetType().GetMethod(s.Value());
|
2024-06-29 18:29:40 +02:00
|
|
|
if (mi != null) {
|
|
|
|
continue;
|
|
|
|
}
|
2024-11-07 00:48:56 +01:00
|
|
|
FieldInfo? fi = o.Value().GetType().GetField(s.Value());
|
2024-06-29 18:29:40 +02:00
|
|
|
if (fi != null) {
|
|
|
|
continue;
|
|
|
|
}
|
2024-11-07 00:48:56 +01:00
|
|
|
return Boolean.FALSE;
|
2024-06-29 18:29:40 +02:00
|
|
|
}
|
2024-11-07 00:48:56 +01:00
|
|
|
return Boolean.TRUE;
|
2024-06-29 18:29:40 +02:00
|
|
|
}
|
2024-11-07 00:48:56 +01:00
|
|
|
private static Expression _getitems(IEnumerable<Expression> args) {
|
2024-11-08 03:08:29 +01:00
|
|
|
Object o = new Object(((IInner) args.First()).Inner());
|
2024-06-29 18:29:40 +02:00
|
|
|
IList<Expression> r = new List<Expression>();
|
|
|
|
foreach (var e in args.Skip(1)) {
|
2024-11-07 00:48:56 +01:00
|
|
|
String s = (String) e;
|
|
|
|
PropertyInfo? pi = o.Value().GetType().GetProperty(s.Value());
|
2024-06-29 18:29:40 +02:00
|
|
|
if (pi != null) {
|
2024-11-07 00:48:56 +01:00
|
|
|
r.Add(Object.FromBase(pi.GetValue(o.Value())));
|
2024-06-29 18:29:40 +02:00
|
|
|
continue;
|
|
|
|
}
|
2024-11-07 00:48:56 +01:00
|
|
|
FieldInfo? fi = o.Value().GetType().GetField(s.Value());
|
2024-06-29 18:29:40 +02:00
|
|
|
if (fi != null) {
|
2024-11-07 00:48:56 +01:00
|
|
|
r.Add(Object.FromBase(fi.GetValue(o.Value())));
|
2024-06-29 18:29:40 +02:00
|
|
|
continue;
|
|
|
|
}
|
2024-11-07 00:48:56 +01:00
|
|
|
throw new ApplicationException($"{o.Value()} has no property or field {s.Value()}");
|
2024-06-29 18:29:40 +02:00
|
|
|
}
|
2024-11-07 00:48:56 +01:00
|
|
|
return Cons.FromList(r);
|
2024-06-29 18:29:40 +02:00
|
|
|
}
|
2024-10-25 02:18:13 +02:00
|
|
|
|
2024-11-07 00:48:56 +01:00
|
|
|
private static Expression _invoke(IEnumerable<Expression> args) {
|
2024-11-08 03:08:29 +01:00
|
|
|
Object o = new Object(((IInner) args.First()).Inner());
|
2024-11-07 00:48:56 +01:00
|
|
|
String s = (String) args.Skip(1).First();
|
2024-11-07 22:26:33 +01:00
|
|
|
IEnumerable<Expression> l;
|
|
|
|
if (args.Skip(2).First() is Boolean lb && lb == Boolean.FALSE) {
|
|
|
|
l = new List<Expression>();
|
|
|
|
} else if (args.Skip(2).First() is Cons lc) {
|
|
|
|
l = lc.ToList();
|
|
|
|
} else {
|
|
|
|
throw new ApplicationException($"Expected a list of arguments, got {args.Skip(2).First()}");
|
|
|
|
}
|
2024-11-08 03:08:29 +01:00
|
|
|
object[]? l_ = l.Select<Expression, object>(x => {
|
2024-11-07 22:20:19 +01:00
|
|
|
switch (x) {
|
|
|
|
case Integer s:
|
|
|
|
return s.Value();
|
|
|
|
case Boolean b:
|
|
|
|
return b.Value();
|
2024-11-08 03:08:29 +01:00
|
|
|
case String s:
|
|
|
|
return s.Value();
|
2024-11-07 22:20:19 +01:00
|
|
|
case Object o:
|
|
|
|
return o.Value();
|
|
|
|
case Cons c:
|
|
|
|
return c.ToList().ToList();
|
|
|
|
}
|
2024-11-08 03:08:29 +01:00
|
|
|
throw new ApplicationException($"Unhandled value {x} (type {x.GetType()})");
|
|
|
|
}).ToArray();
|
|
|
|
Type[] l_types = l_.Select( x => {
|
|
|
|
return x.GetType();
|
2024-11-07 22:20:19 +01:00
|
|
|
}).ToArray();
|
2024-11-08 03:08:29 +01:00
|
|
|
|
|
|
|
IList<Expression> r = new List<Expression>();
|
|
|
|
MethodInfo? mi = o.Value().GetType().GetMethod(s.Value(), l_types);
|
|
|
|
if (mi == null) {
|
2024-12-17 17:56:14 +01:00
|
|
|
throw new ApplicationException($"{o.Value()} has no method {s.Value()}");
|
|
|
|
}
|
|
|
|
|
|
|
|
return Object.FromBase(mi.Invoke(o.Value(), l_));
|
|
|
|
}
|
|
|
|
|
|
|
|
private static Type? GetType(string name) {
|
|
|
|
return Type.GetType(
|
|
|
|
name,
|
|
|
|
(name) => { return AppDomain.CurrentDomain.GetAssemblies().Where(z => z.FullName == name.FullName).FirstOrDefault(); },
|
|
|
|
null,
|
|
|
|
true
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static Expression _invoke_generic(IEnumerable<Expression> args) {
|
|
|
|
Object o = new Object(((IInner) args.First()).Inner());
|
|
|
|
String s = (String) args.Skip(1).First();
|
|
|
|
IEnumerable<Expression> l;
|
|
|
|
if (args.Skip(2).First() is Boolean lb && lb == Boolean.FALSE) {
|
|
|
|
l = new List<Expression>();
|
|
|
|
} else if (args.Skip(2).First() is Cons lc) {
|
|
|
|
l = lc.ToList();
|
|
|
|
} else {
|
|
|
|
throw new ApplicationException($"Expected a list of arguments, got {args.Skip(2).First()}");
|
|
|
|
}
|
|
|
|
IEnumerable<Type> types;
|
|
|
|
if (args.Skip(3).First() is Boolean lb_ && lb_ == Boolean.FALSE) {
|
|
|
|
types = new List<Type>();
|
|
|
|
} else if (args.Skip(3).First() is Cons lc) {
|
|
|
|
types = lc.ToList().Select(x => GetType(((String) x).Value())).ToList();
|
|
|
|
} else {
|
|
|
|
throw new ApplicationException($"Expected a list of arguments, got {args.Skip(3).First()}");
|
|
|
|
}
|
|
|
|
object[]? l_ = l.Select<Expression, object>(x => {
|
|
|
|
switch (x) {
|
|
|
|
case Integer s:
|
|
|
|
return s.Value();
|
|
|
|
case Boolean b:
|
|
|
|
return b.Value();
|
|
|
|
case String s:
|
|
|
|
return s.Value();
|
|
|
|
case Object o:
|
|
|
|
return o.Value();
|
|
|
|
case Cons c:
|
|
|
|
return c.ToList().ToList();
|
|
|
|
}
|
|
|
|
throw new ApplicationException($"Unhandled value {x} (type {x.GetType()})");
|
|
|
|
}).ToArray();
|
|
|
|
Type[] l_types = l_.Select( x => {
|
|
|
|
return x.GetType();
|
|
|
|
}).ToArray();
|
|
|
|
|
|
|
|
IList<Expression> r = new List<Expression>();
|
|
|
|
MethodInfo? mi = o.Value().GetType().GetMethod(s.Value(), l_types);
|
|
|
|
if (mi == null) {
|
|
|
|
throw new ApplicationException($"{o.Value()} has no method {s.Value()}");
|
2024-11-08 03:08:29 +01:00
|
|
|
}
|
2024-12-17 17:56:14 +01:00
|
|
|
mi = mi.MakeGenericMethod(types.ToArray());
|
2024-11-08 03:08:29 +01:00
|
|
|
|
2024-11-07 22:20:19 +01:00
|
|
|
return Object.FromBase(mi.Invoke(o.Value(), l_));
|
2024-10-25 02:18:13 +02:00
|
|
|
}
|
2024-06-29 18:29:40 +02:00
|
|
|
}
|
2024-06-27 01:47:44 +02:00
|
|
|
|
2024-06-29 18:29:40 +02:00
|
|
|
public class BuiltinsLater : Dictionary<string, FunctionLater> {
|
|
|
|
public BuiltinsLater() : base() {
|
2024-11-07 00:48:56 +01:00
|
|
|
this["quote"] = _quote;
|
|
|
|
this["eval"] = _eval;
|
|
|
|
this["cond"] = _cond;
|
2024-11-08 03:08:29 +01:00
|
|
|
this["if"] = _if;
|
2024-06-29 18:29:40 +02:00
|
|
|
this["define"] = _define;
|
2024-11-07 00:48:56 +01:00
|
|
|
this["let"] = _let;
|
|
|
|
this["let*"] = _let_star;
|
2024-10-26 03:49:52 +02:00
|
|
|
this["lambda"] = _lambda;
|
2024-11-07 00:48:56 +01:00
|
|
|
this["lambda*"] = _lambda_star;
|
2024-11-08 03:08:29 +01:00
|
|
|
this["apply"] = _apply;
|
|
|
|
|
|
|
|
this["and"] = (e, x) => {
|
|
|
|
Expression? r = null;
|
|
|
|
foreach (var xi in x) {
|
|
|
|
r = e.eval(xi);
|
|
|
|
if (r == Boolean.FALSE) {
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (r is null) {
|
|
|
|
return Boolean.FALSE;
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
};
|
|
|
|
this["or"] = (e, x) => {
|
|
|
|
foreach (var xi in x) {
|
|
|
|
var r = e.eval(xi);
|
|
|
|
if (r != Boolean.FALSE) {
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Boolean.FALSE;
|
|
|
|
};
|
2024-11-07 00:48:56 +01:00
|
|
|
}
|
|
|
|
private static Expression _quote(Executor e, IEnumerable<Expression> args) {
|
|
|
|
return args.First();
|
2024-06-29 18:29:40 +02:00
|
|
|
}
|
2024-11-07 00:48:56 +01:00
|
|
|
private static Expression _eval(Executor e, IEnumerable<Expression> args) {
|
|
|
|
return e.eval(e.eval(args.First()));
|
2024-06-29 18:29:40 +02:00
|
|
|
}
|
2024-11-07 00:48:56 +01:00
|
|
|
private static Expression _cond(Executor e, IEnumerable<Expression> args) {
|
|
|
|
foreach (var a in args) {
|
|
|
|
if (a is Cons a_cons) {
|
|
|
|
var a_ = a_cons.ToList();
|
|
|
|
if (!e.eval(a_.First()).Equals(Boolean.FALSE)) {
|
|
|
|
return e.eval(a_.Skip(1).First());
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
throw new ApplicationException($"Incorrect arguments to cond, expected list: {args}");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Boolean.FALSE;
|
2024-06-29 18:29:40 +02:00
|
|
|
}
|
2024-11-08 03:08:29 +01:00
|
|
|
private static Expression _if(Executor e, IEnumerable<Expression> args) {
|
|
|
|
if (e.eval(args.First()).Equals(Boolean.FALSE)) {
|
|
|
|
return e.eval(args.Skip(2).First());
|
|
|
|
}
|
|
|
|
return e.eval(args.Skip(1).First());
|
|
|
|
}
|
2024-11-07 00:48:56 +01:00
|
|
|
private static Expression _define(Executor e, IEnumerable<Expression> args) {
|
|
|
|
Symbol refname = (Symbol) args.First();
|
|
|
|
e.environment.Parent(true).Set(refname.Name(), args.Skip(1).Select(x => e.eval(x)).First());
|
|
|
|
return Boolean.TRUE;
|
2024-10-26 03:49:52 +02:00
|
|
|
}
|
2024-11-07 00:48:56 +01:00
|
|
|
private static Expression _let_star(Executor e, IEnumerable<Expression> args) {
|
|
|
|
Executor new_e = new Executor(new SubEnvironment(e.environment), e.builtins, e.builtinsLater);
|
|
|
|
foreach (var pair in args.SkipLast(1)) {
|
|
|
|
if (pair is not Cons pair_cons) {
|
|
|
|
throw new ApplicationException("No expression for let*");
|
|
|
|
}
|
|
|
|
Symbol refname = (Symbol) pair_cons.Item1;
|
|
|
|
Expression exp = ((Cons) pair_cons.Item2).Item1;
|
|
|
|
new_e.environment.Set(refname.Name(), new_e.eval(exp));
|
2024-06-27 01:47:44 +02:00
|
|
|
}
|
2024-11-07 00:48:56 +01:00
|
|
|
return new_e.eval(args.Last());
|
|
|
|
}
|
|
|
|
private static Expression _let(Executor e, IEnumerable<Expression> args) {
|
|
|
|
Executor new_e = new Executor(new SubEnvironment(e.environment), e.builtins, e.builtinsLater);
|
|
|
|
List<(Symbol, Expression)> vars = new List<(Symbol, Expression)>();
|
|
|
|
foreach (var pair in args.SkipLast(1)) {
|
|
|
|
if (pair is not Cons pair_cons) {
|
|
|
|
throw new ApplicationException("");
|
|
|
|
}
|
|
|
|
Symbol refname = (Symbol) pair_cons.Item1;
|
|
|
|
Expression exp_ = ((Cons) pair_cons.Item2).Item1;
|
2024-11-08 03:08:29 +01:00
|
|
|
vars.Add((refname, e.eval(exp_)));
|
2024-06-27 01:47:44 +02:00
|
|
|
}
|
2024-11-07 00:48:56 +01:00
|
|
|
foreach (var pair in vars) {
|
|
|
|
new_e.environment.Set(pair.Item1.Name(), pair.Item2);
|
|
|
|
}
|
|
|
|
return new_e.eval(args.Last());
|
|
|
|
}
|
|
|
|
private static Expression _lambda(Executor e, IEnumerable<Expression> args) {
|
|
|
|
IEnumerable<Symbol> proc_args;
|
|
|
|
if (args.First() is Cons proc_args_) { proc_args = proc_args_.ToList().Select(x => (Symbol) x); }
|
|
|
|
else if (args.First() == Boolean.FALSE) { proc_args = new List<Symbol>(); }
|
|
|
|
else {
|
|
|
|
throw new ApplicationException("");
|
2024-10-25 20:20:18 +02:00
|
|
|
}
|
2024-11-07 00:48:56 +01:00
|
|
|
return new Procedure(proc_args, args.Skip(1).First(), true);
|
2024-10-25 20:20:18 +02:00
|
|
|
}
|
2024-11-07 00:48:56 +01:00
|
|
|
private static Expression _lambda_star(Executor e, IEnumerable<Expression> args) {
|
|
|
|
IEnumerable<Symbol> proc_args;
|
|
|
|
if (args.First() is Cons proc_args_) { proc_args = proc_args_.ToList().Select(x => (Symbol) x); }
|
|
|
|
else if (args.First() == Boolean.FALSE) { proc_args = new List<Symbol>(); }
|
|
|
|
else {
|
|
|
|
throw new ApplicationException("");
|
2024-10-25 20:20:18 +02:00
|
|
|
}
|
2024-11-07 00:48:56 +01:00
|
|
|
return new Procedure(proc_args, args.Skip(1).First(), false);
|
2024-10-25 20:20:18 +02:00
|
|
|
}
|
2024-11-08 03:08:29 +01:00
|
|
|
private static Expression _apply(Executor e, IEnumerable<Expression> args) {
|
|
|
|
return e.eval(new Cons(args.First(), e.eval(args.Skip(1).First())));
|
|
|
|
}
|
2024-06-29 18:29:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public class Executor {
|
2024-10-26 03:49:52 +02:00
|
|
|
IEnvironment<string, Expression> _environment;
|
2024-06-29 18:29:40 +02:00
|
|
|
Builtins _builtins;
|
|
|
|
BuiltinsLater _builtinsLater;
|
2024-10-26 03:49:52 +02:00
|
|
|
public Executor(IEnvironment<string, Expression> environment, Builtins builtins, BuiltinsLater builtinsLater) {
|
2024-06-29 18:29:40 +02:00
|
|
|
_environment = environment;
|
|
|
|
_builtins = builtins;
|
|
|
|
_builtinsLater = builtinsLater;
|
|
|
|
}
|
2024-10-27 00:54:40 +02:00
|
|
|
public Executor(IEnvironment<string, Expression> environment) {
|
|
|
|
_environment = environment;
|
|
|
|
_builtins = new Builtins();
|
|
|
|
_builtinsLater = new BuiltinsLater();
|
|
|
|
}
|
2024-06-29 18:29:40 +02:00
|
|
|
public Executor() {
|
|
|
|
_environment = new Environment();
|
|
|
|
_builtins = new Builtins();
|
|
|
|
_builtinsLater = new BuiltinsLater();
|
2024-06-27 01:47:44 +02:00
|
|
|
}
|
|
|
|
|
2024-10-26 03:49:52 +02:00
|
|
|
public IEnvironment<string, Expression> environment { get => _environment; }
|
2024-06-29 18:29:40 +02:00
|
|
|
public Builtins builtins { get => _builtins; }
|
|
|
|
public BuiltinsLater builtinsLater { get => _builtinsLater; }
|
|
|
|
|
2024-11-07 00:48:56 +01:00
|
|
|
public Expression? EvalFunction(Symbol fcname, IEnumerable<Expression> args) {
|
|
|
|
if (builtins.ContainsKey(fcname.Name())) {
|
|
|
|
return builtins[fcname.Name()](args.Select(x => eval(x)).ToList()); // call ToList for sideeffect
|
2024-06-29 18:29:40 +02:00
|
|
|
}
|
2024-11-07 00:48:56 +01:00
|
|
|
if (builtinsLater.ContainsKey(fcname.Name())) {
|
|
|
|
return builtinsLater[fcname.Name()](this, args);
|
2024-06-29 18:29:40 +02:00
|
|
|
}
|
2024-11-07 00:48:56 +01:00
|
|
|
return null;
|
2024-06-27 01:47:44 +02:00
|
|
|
}
|
|
|
|
|
2024-06-29 18:29:40 +02:00
|
|
|
public Expression eval(Expression expression) {
|
2024-06-27 01:47:44 +02:00
|
|
|
switch (expression) {
|
|
|
|
case Symbol s:
|
2024-11-07 00:48:56 +01:00
|
|
|
if (_environment.Find(s.Name()) is not IEnvironment<string, Expression> env) {
|
|
|
|
throw new ApplicationException($"Could not find '{s.Name()}'");
|
2024-10-30 21:15:16 +01:00
|
|
|
}
|
2024-11-11 18:00:55 +01:00
|
|
|
var r_ = env.Get(s.Name());
|
|
|
|
if (r_ is null) {
|
|
|
|
throw new ApplicationException($"Could not find '{s.Name()}'");
|
|
|
|
}
|
|
|
|
return r_;
|
2024-11-07 00:48:56 +01:00
|
|
|
case Boolean b:
|
2024-06-29 18:29:40 +02:00
|
|
|
return b;
|
2024-06-27 01:47:44 +02:00
|
|
|
case Integer i:
|
2024-06-29 18:29:40 +02:00
|
|
|
return i;
|
2024-11-07 00:48:56 +01:00
|
|
|
case String s:
|
2024-06-29 18:29:40 +02:00
|
|
|
return s;
|
2024-11-07 00:48:56 +01:00
|
|
|
case Object o:
|
2024-10-27 00:53:42 +02:00
|
|
|
return o;
|
2024-10-26 03:49:52 +02:00
|
|
|
case Procedure p:
|
|
|
|
return p;
|
2024-11-07 00:48:56 +01:00
|
|
|
case Cons cons:
|
|
|
|
var l = cons.ToList();
|
|
|
|
if (cons.Item1 is Symbol cons_item1_symbol) {
|
|
|
|
Expression? r = EvalFunction(cons_item1_symbol, l.Skip(1));
|
|
|
|
if (r is not null) { return r; }
|
2024-10-26 03:49:52 +02:00
|
|
|
}
|
2024-11-07 00:48:56 +01:00
|
|
|
var eval_Item1 = eval(cons.Item1);
|
|
|
|
if (eval_Item1 is Symbol eval_item1_symbol1) {
|
|
|
|
Expression? r = EvalFunction(eval_item1_symbol1, l.Skip(1));
|
|
|
|
if (r is not null) { return r; }
|
2024-10-26 03:49:52 +02:00
|
|
|
}
|
2024-11-07 00:48:56 +01:00
|
|
|
if (eval_Item1 is Procedure eval_item1_procedure) {
|
|
|
|
return eval_item1_procedure.Call(this, l.Skip(1).Select(x => x).ToList());
|
2024-06-27 01:47:44 +02:00
|
|
|
}
|
2024-11-07 00:48:56 +01:00
|
|
|
throw new ApplicationException($"Not handled case (type = {eval_Item1.GetType()}) '{cons}'");
|
2024-06-27 01:47:44 +02:00
|
|
|
}
|
2024-10-26 03:49:52 +02:00
|
|
|
throw new ApplicationException($"Not handled case '{expression}'");
|
2024-06-27 01:47:44 +02:00
|
|
|
}
|
2024-06-29 18:29:40 +02:00
|
|
|
public Expression eval(Parser p) {
|
2024-06-27 01:47:44 +02:00
|
|
|
return eval(p.parse());
|
|
|
|
}
|
2024-06-29 18:29:40 +02:00
|
|
|
public Expression eval(StringTokenStream sts) {
|
2024-06-27 01:47:44 +02:00
|
|
|
return eval(new Parser(sts));
|
|
|
|
}
|
2024-06-29 18:29:40 +02:00
|
|
|
public Expression eval(string p) {
|
2024-06-27 01:47:44 +02:00
|
|
|
return eval(StringTokenStream.generate(p));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|