feat: add default implementations of procedures.

This commit is contained in:
redxef 2024-10-27 00:54:40 +02:00
parent 55537b9dd5
commit d74f9a8074
Signed by: redxef
GPG key ID: 7DAC3AA211CBD921
3 changed files with 19 additions and 1 deletions

View file

@ -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<string, Expression>, IEnvironment<string, Expression> { public class SubEnvironment : Dictionary<string, Expression>, IEnvironment<string, Expression> {
private IEnvironment<string, Expression> _super; private IEnvironment<string, Expression> _super;
public SubEnvironment(IEnvironment<string, Expression> super) { public SubEnvironment(IEnvironment<string, Expression> super) {
@ -375,6 +381,11 @@ namespace Jellyfin.Plugin.SmartPlaylist.Lisp {
_builtins = builtins; _builtins = builtins;
_builtinsLater = builtinsLater; _builtinsLater = builtinsLater;
} }
public Executor(IEnvironment<string, Expression> environment) {
_environment = environment;
_builtins = new Builtins();
_builtinsLater = new BuiltinsLater();
}
public Executor() { public Executor() {
_environment = new Environment(); _environment = new Environment();
_builtins = new Builtins(); _builtins = new Builtins();

View file

@ -104,7 +104,7 @@ namespace Jellyfin.Plugin.SmartPlaylist.ScheduledTasks {
private IEnumerable<Guid> FilterPlaylistItems(IEnumerable<BaseItem> items, User user, SmartPlaylistDto smartPlaylist) { private IEnumerable<Guid> FilterPlaylistItems(IEnumerable<BaseItem> items, User user, SmartPlaylistDto smartPlaylist) {
List<Guid> results = new List<Guid>(); List<Guid> results = new List<Guid>();
Expression expression = new Parser(StringTokenStream.generate(smartPlaylist.Program)).parse(); 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)); executor.environment.Set("user", new Lisp_Object(user));
foreach (var i in items) { foreach (var i in items) {
executor.environment.Set("item", new Lisp_Object(i)); executor.environment.Set("item", new Lisp_Object(i));

View file

@ -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)))"); 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"); 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());
}
} }
} }