diff --git a/Jellyfin.Plugin.SmartPlaylist/Lisp/Interpreter.cs b/Jellyfin.Plugin.SmartPlaylist/Lisp/Interpreter.cs index e1c5f26..0cbc2ad 100644 --- a/Jellyfin.Plugin.SmartPlaylist/Lisp/Interpreter.cs +++ b/Jellyfin.Plugin.SmartPlaylist/Lisp/Interpreter.cs @@ -76,6 +76,12 @@ namespace Jellyfin.Plugin.SmartPlaylist.Lisp { } } + public class DefaultEnvironment: Environment { + public DefaultEnvironment() { + this["find"] = new Parser("(lambda (item list) (if (= list ()) nil (if (= item (car list)) (car list) (find item (cdr list)))))").parse(); + } + } + public class SubEnvironment : Dictionary, IEnvironment { private IEnvironment _super; public SubEnvironment(IEnvironment super) { @@ -375,6 +381,11 @@ namespace Jellyfin.Plugin.SmartPlaylist.Lisp { _builtins = builtins; _builtinsLater = builtinsLater; } + public Executor(IEnvironment environment) { + _environment = environment; + _builtins = new Builtins(); + _builtinsLater = new BuiltinsLater(); + } public Executor() { _environment = new Environment(); _builtins = new Builtins(); diff --git a/Jellyfin.Plugin.SmartPlaylist/ScheduledTasks/GeneratePlaylist.cs b/Jellyfin.Plugin.SmartPlaylist/ScheduledTasks/GeneratePlaylist.cs index d8b176a..7632827 100644 --- a/Jellyfin.Plugin.SmartPlaylist/ScheduledTasks/GeneratePlaylist.cs +++ b/Jellyfin.Plugin.SmartPlaylist/ScheduledTasks/GeneratePlaylist.cs @@ -104,7 +104,7 @@ namespace Jellyfin.Plugin.SmartPlaylist.ScheduledTasks { private IEnumerable FilterPlaylistItems(IEnumerable items, User user, SmartPlaylistDto smartPlaylist) { List results = new List(); Expression expression = new Parser(StringTokenStream.generate(smartPlaylist.Program)).parse(); - Executor executor = new Executor(); + Executor executor = new Executor(new DefaultEnvironment()); executor.environment.Set("user", new Lisp_Object(user)); foreach (var i in items) { executor.environment.Set("item", new Lisp_Object(i)); diff --git a/Tests/Tests.cs b/Tests/Tests.cs index 4b13199..23ea543 100644 --- a/Tests/Tests.cs +++ b/Tests/Tests.cs @@ -172,5 +172,12 @@ namespace Tests r = e.eval("(begin (define find (lambda (item list) (if (= list ()) nil (if (= item (car list)) (car list) (find item (cdr list)))))) (find 0 (1 2 3 4)))"); Assert.Equal(string.Format("{0}", r), "nil"); } + + [Fact] + public static void DefaultEnvironmentTest() { + Executor e = new Executor(new DefaultEnvironment()); + Assert.Equal("nil", e.eval("(find 0 (1 2 3 4))").ToString()); + Assert.Equal("3", e.eval("(find 3 (1 2 3 4))").ToString()); + } } }