From 8371dc8536ed9eb3fdce05fa2c0225a7c4256900 Mon Sep 17 00:00:00 2001 From: redxef Date: Tue, 19 Nov 2024 21:57:34 +0100 Subject: [PATCH] feat(lisp): add string comparison methods. --- .../Lisp/Expression.cs | 26 ++++++++++++++++++- .../Lisp/Interpreter.cs | 11 +++++--- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/Jellyfin.Plugin.SmartPlaylist/Lisp/Expression.cs b/Jellyfin.Plugin.SmartPlaylist/Lisp/Expression.cs index a5c3979..7becb79 100644 --- a/Jellyfin.Plugin.SmartPlaylist/Lisp/Expression.cs +++ b/Jellyfin.Plugin.SmartPlaylist/Lisp/Expression.cs @@ -157,11 +157,35 @@ namespace Jellyfin.Plugin.SmartPlaylist.Lisp { return new List(); } } - public class String: Scalar { + public class String: Scalar, ISortable { public String(string value) : base(value) {} public override string? ToString() { return $"\"{base.ToString()}\""; } + public static Boolean operator <(String a, String b) { + return (a.Value().CompareTo(b.Value()) < 0) ? Boolean.TRUE : Boolean.FALSE; + } + public static Boolean operator >(String a, String b) { + return b < a; + } + public static Boolean operator <=(String a, String b) { + return (a.Value().CompareTo(b.Value()) <= 0) ? Boolean.TRUE : Boolean.FALSE; + } + public static Boolean operator >=(String a, String b) { + return b <= a; + } + public override int GetHashCode() { + return base.GetHashCode(); + } + public override bool Equals(object? other) { + return base.Equals(other); + } + public static Boolean operator ==(String a, String b) { + return (a._value == b._value) ? Boolean.TRUE : Boolean.FALSE; + } + public static Boolean operator !=(String a, String b) { + return (a._value != b._value) ? Boolean.TRUE : Boolean.FALSE; + } } public class Cons: Expression { public Expression Item1; diff --git a/Jellyfin.Plugin.SmartPlaylist/Lisp/Interpreter.cs b/Jellyfin.Plugin.SmartPlaylist/Lisp/Interpreter.cs index 8cb7631..1aa7fca 100644 --- a/Jellyfin.Plugin.SmartPlaylist/Lisp/Interpreter.cs +++ b/Jellyfin.Plugin.SmartPlaylist/Lisp/Interpreter.cs @@ -127,16 +127,21 @@ namespace Jellyfin.Plugin.SmartPlaylist.Lisp { 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((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); 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) => { return (x.First() == Boolean.FALSE) ? Boolean.TRUE : Boolean.FALSE; }; + 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); this["haskeys"] = _haskeys;