feat: add single quote quoting.

'(a b c) = (quote (a b c))
This commit is contained in:
redxef 2024-12-17 23:29:55 +01:00
parent db5a659dfc
commit f7cbebdd9c
Signed by: redxef
GPG key ID: 7DAC3AA211CBD921
3 changed files with 24 additions and 2 deletions

View file

@ -40,11 +40,20 @@ namespace Jellyfin.Plugin.SmartPlaylist.Lisp.Compiler {
return new String(r); return new String(r);
} }
Expression parse_quote(GroupingToken start, GroupingToken? end) {
Debug.Assert(end == null);
Debug.Assert("'".Contains(start.value));
return Cons.FromList(new Expression[]{ new Symbol("quote"), parse()});
}
Expression parse_grouping(GroupingToken start, GroupingToken? end) { Expression parse_grouping(GroupingToken start, GroupingToken? end) {
Debug.Assert(end != null);
if ("\"".Contains(start.value)) { if ("\"".Contains(start.value)) {
return parse_string(start, end); return parse_string(start, end);
} }
if ("'".Contains(start.value)) {
return parse_quote(start, end);
}
Debug.Assert(end != null);
IList<Expression> expressions = new List<Expression>(); IList<Expression> expressions = new List<Expression>();
while (_sts.Available() > 0) { while (_sts.Available() > 0) {
Token<string> t = _sts.Get(); Token<string> t = _sts.Get();

View file

@ -43,7 +43,7 @@ namespace Jellyfin.Plugin.SmartPlaylist.Lisp.Compiler {
return null; return null;
} }
char t = program.Get(); char t = program.Get();
if ("()\"".Contains(t)) { if ("()\"'".Contains(t)) {
return new GroupingToken(t.ToString()); return new GroupingToken(t.ToString());
} }
return null; return null;
@ -53,6 +53,8 @@ namespace Jellyfin.Plugin.SmartPlaylist.Lisp.Compiler {
return new GroupingToken(")"); return new GroupingToken(")");
} else if (_value == ")") { } else if (_value == ")") {
return null; return null;
} else if (_value == "'") {
return null;
} }
return new GroupingToken(_value); return new GroupingToken(_value);
} }

View file

@ -66,6 +66,16 @@ namespace Tests
sts = StringTokenStream.generate(program); sts = StringTokenStream.generate(program);
p = new Parser(sts); p = new Parser(sts);
Assert.Equal(program, string.Format("{0}", p.parse())); Assert.Equal(program, string.Format("{0}", p.parse()));
program = "(abc '(1 2 3))";
sts = StringTokenStream.generate(program);
p = new Parser(sts);
Assert.Equal("(abc (quote (1 2 3)))", string.Format("{0}", p.parse()));
program = "(abc \"'(1 2 3)\")";
sts = StringTokenStream.generate(program);
p = new Parser(sts);
Assert.Equal(program, string.Format("{0}", p.parse()));
} }
[Fact] [Fact]
@ -236,6 +246,7 @@ namespace Tests
//Assert.Equal("", e.eval("(rand)").ToString()); //Assert.Equal("", e.eval("(rand)").ToString());
//Assert.Equal("", e.eval("(shuf (list 0 1 2 3 4 5 6))").ToString()); //Assert.Equal("", e.eval("(shuf (list 0 1 2 3 4 5 6))").ToString());
Assert.Equal("(1 2 3 4 5 6 7)", e.eval("(qsort (lambda (a b) (> a b)) '(5 4 7 3 2 6 1))").ToString());
} }
} }
} }