using System.Diagnostics; namespace Jellyfin.Plugin.SmartPlaylist.Lisp.Compiler { interface IAddable where T : IAddable { static abstract T operator +(T left, T right); } interface ISubtractable where T : ISubtractable { static abstract T operator -(T left, T right); } interface IMultiplicatable where T : IMultiplicatable { static abstract T operator *(T left, T right); } interface IDivisible where T : IDivisible { static abstract T operator /(T left, T right); static abstract T operator %(T left, T right); } interface ISortable where T : ISortable { static abstract E operator >(T left, T right); static abstract E operator <(T left, T right); static abstract E operator >=(T left, T right); static abstract E operator <=(T left, T right); } interface IComparable where T : IComparable { static abstract E operator ==(T left, T right); static abstract E operator !=(T left, T right); E Equals(T other); } public abstract class Expression: IComparable { public override abstract string ToString(); public abstract override int GetHashCode(); public abstract bool Equals(Expression other); public override bool Equals(object? other) { if (other is Expression other_e) { return Equals(other_e); } return false; } public static bool operator ==(Expression left, Expression right) { return left.Equals(right); } public static bool operator !=(Expression left, Expression right) { return !left.Equals(right); } public abstract object Inner(); } public abstract class Atom : Expression {} public class Symbol : Atom { private readonly string _name; public Symbol(string name) { _name = name; } public string name { get => _name; } public override int GetHashCode() { int hash = 17; hash *= 23; hash += _name.GetHashCode(); return hash; } public override bool Equals(Expression? other) { if (other is Symbol other_s) { return _name == other_s._name; } return false; } public override string ToString() { return _name; } public override object Inner() { return _name; } } public class Boolean : Atom { private readonly bool _value; public Boolean(bool value) { _value = value; } public bool value { get => _value; } public override int GetHashCode() { int hash = 17; hash *= 23; hash += _value.GetHashCode(); return hash; } public override bool Equals(Expression other) { if (other is Boolean other_b) { return _value == other_b.value; } return false; } public override string ToString() { return _value? "t" : "nil"; } public override object Inner() { return _value; } } public class Integer : Atom, IAddable, ISubtractable, IMultiplicatable, IDivisible, ISortable { private readonly int _value; public Integer(int value) { _value = value; } public int value { get => _value; } public override int GetHashCode() { int hash = 17; hash *= 23; hash += _value.GetHashCode(); return hash; } public override bool Equals(Expression other) { if (other is Integer other_i) { return _value == other_i._value; } return false; } public override string ToString() { return _value.ToString(); } public static Integer operator +(Integer a, Integer b) { return new Integer(a.value + b.value); } public static Integer operator -(Integer a, Integer b) { return new Integer(a.value - b.value); } public static Integer operator *(Integer a, Integer b) { return new Integer(a.value * b.value); } public static Integer operator /(Integer a, Integer b) { return new Integer(a.value / b.value); } public static Integer operator %(Integer a, Integer b) { return new Integer(a.value % b.value); } public static Boolean operator >(Integer a, Integer b) { return new Boolean(a.value > b.value); } public static Boolean operator <(Integer a, Integer b) { return new Boolean(a.value < b.value); } public static Boolean operator >=(Integer a, Integer b) { return new Boolean(a.value >= b.value); } public static Boolean operator <=(Integer a, Integer b) { return new Boolean(a.value <= b.value); } public static Boolean operator ==(Integer a, Integer b) { return new Boolean(a.value == b.value); } public static Boolean operator !=(Integer a, Integer b) { return new Boolean(a.value != b.value); } public override object Inner() { return _value; } } public class String : Atom, IAddable { private readonly string _value; public String(string value) { _value = value; } public string value { get => _value; } public override int GetHashCode() { int hash = 17; hash *= 23; hash += _value.GetHashCode(); return hash; } public override bool Equals(Expression other) { if (other is String other_s) { return _value == other_s._value; } return false; } public override string ToString() { return "\"" + _value + "\""; } public static String operator +(String a, String b) { return new String (a.value + b.value); } public override object Inner() { return _value; } } public class Object : Atom { private readonly object _value; public Object(object value) { _value = value; } public object value { get => _value; } public override int GetHashCode() { int hash = 17; hash *= 23; hash += _value.GetHashCode(); return hash; } public override bool Equals(Expression other) { if (other is Object other_o) { return _value == other_o._value; } return false; } public override string ToString() { return _value.ToString(); } public static Expression FromBase(object o) { switch (o) { case bool b: return new Boolean(b); case int i: return new Integer(i); case string s: return new String(s); case IEnumerable e: return new List(e.Select(x => Object.FromBase(x)).ToList()); default: return new Object(o); } } public override object Inner() { return _value; } } public class List : Expression { private IList _expressions; public List(IList expressions) { _expressions = expressions; } public IList expressions { get => _expressions; } public override int GetHashCode() { int hash = 17; foreach (Expression i in _expressions) { hash *= 23; hash += i.GetHashCode(); } return hash; } public override bool Equals(Expression other) { if (other is List other_l) { return _expressions.SequenceEqual(other_l._expressions); } return false; } public override string ToString() { return "(" + string.Join(" ", _expressions.Select(x => x.ToString())) + ")"; } public static List operator +(List a, List b) { List r = new List(); r.AddRange(a.expressions); r.AddRange(b.expressions); return new List(r); } public override object Inner() { return _expressions.Select(x => x.Inner()).ToArray(); } } public class Parser { private StringTokenStream _sts; public Parser(StringTokenStream tokens) { _sts = tokens; } public Parser(string s) { _sts = StringTokenStream.generate(s); } public Expression parse() { Token token = _sts.Get(); switch (token) { case GroupingToken gt: return parse_grouping(gt, gt.closing_value); case AtomToken at: return parse_atom(at); case OperatorToken ot: return parse_operator(ot); case SpaceToken sp: return parse(); } return parse(); } Expression parse_string(GroupingToken start, GroupingToken end) { Debug.Assert(start.value == end.value); Debug.Assert("'\"".Contains(start.value)); string r = ""; while (_sts.Available() > 0) { Token t = _sts.Get(); if (t.value == end.value) { break; } r += t.value; } _sts.Commit(); return new String(r); } Expression parse_grouping(GroupingToken start, GroupingToken end) { if ("'\"".Contains(start.value)) { return parse_string(start, end); } IList expressions = new List(); while (_sts.Available() > 0) { Token t = _sts.Get(); if (t.value == end.value) { _sts.Commit(); break; } _sts.Rewind(1); expressions.Add(parse()); } return new List(expressions); } Expression parse_atom(AtomToken at) { int parsed_value; if (int.TryParse(at.value, out parsed_value)) { _sts.Commit(); return new Integer(parsed_value); } if (at.value.Equals("t")) { return new Boolean(true); } if (at.value.Equals("nil")) { return new Boolean(false); } _sts.Commit(); return new Symbol(at.value); } Expression parse_operator(OperatorToken ot) { string v = ot.value; while (_sts.Available() > 0) { Token t = _sts.Get(); if (t is OperatorToken ot_) { v += ot_.value; continue; } _sts.Rewind(1); break; } return new Symbol(v); } } }