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> {
|
|
|
|
public V Get(K k);
|
|
|
|
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["if"] = e.eval("(lambda* (condition a b) ( cond ((eval condition) (eval a)) (t (eval b))))");
|
|
|
|
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)))))");
|
|
|
|
this["and"] = e.eval("(lambda (l) (if (null l) t (if (car l) (and (cdr l)) nil)))");
|
|
|
|
this["or"] = e.eval("(lambda (l) (if (null l) nil (if (car l) t (or (cdr l)))))");
|
|
|
|
this["any"] = e.eval("(lambda (fc l) (or (map fc l)))");
|
|
|
|
this["all"] = e.eval("(lambda (fc l) (and (map fc l)))");
|
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> {
|
|
|
|
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);
|
|
|
|
|
|
|
|
this["="] = (x) => _cmp((Integer a, Integer b) => a == b, x);
|
|
|
|
this["eq?"] = (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);
|
|
|
|
this[">="] = (x) => _cmp((Integer a, Integer b) => a >= b, x);
|
|
|
|
this["!="] = (x) => _cmp((Integer a, Integer b) => a != b, x);
|
|
|
|
this["not"] = (x) => (x.First() == Boolean.FALSE) ? Boolean.TRUE : Boolean.FALSE;
|
|
|
|
|
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-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) {
|
|
|
|
Object o = (Object) args.First();
|
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) {
|
|
|
|
Object o = (Object) args.First();
|
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) {
|
|
|
|
Object o = (Object) args.First();
|
|
|
|
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-10-25 02:18:13 +02:00
|
|
|
IList<Expression> r = new List<Expression>();
|
2024-11-07 00:48:56 +01:00
|
|
|
MethodInfo? mi = o.Value().GetType().GetMethod(s.Value());
|
2024-10-25 02:18:13 +02:00
|
|
|
if (mi == null) {
|
2024-11-07 00:48:56 +01:00
|
|
|
throw new ApplicationException($"{o.Value()} has not method {s.Value()}");
|
2024-10-25 02:18:13 +02:00
|
|
|
}
|
2024-11-07 22:26:33 +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();
|
|
|
|
case Object o:
|
|
|
|
return o.Value();
|
|
|
|
case Cons c:
|
|
|
|
return c.ToList().ToList();
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}).ToArray();
|
|
|
|
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-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;
|
|
|
|
}
|
|
|
|
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-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;
|
|
|
|
vars.Add((refname, 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-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-07 00:48:56 +01:00
|
|
|
return env.Get(s.Name());
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|