Test macro list expansion

This commit is contained in:
2021-04-02 12:51:15 -06:00
parent 1e3fe69bb4
commit 72887e7b34
3 changed files with 27 additions and 2 deletions

View File

@@ -292,7 +292,18 @@ class Helpers {
switch (l[idx].def) {
case UnquoteList(exp):
l.splice(idx, 1);
var newElements:Array<ReaderExp> = runAtCompileTime(exp, k, args);
var listToInsert:Dynamic = runAtCompileTime(exp, k, args);
// listToInsert could be either an array (from &rest) or a ListExp (from [list syntax])
var newElements:Array<ReaderExp> = if (Std.isOfType(listToInsert, Array)) {
listToInsert;
} else {
switch (listToInsert.def) {
case ListExp(elements):
elements;
default:
throw CompileError.fromExp(l[idx], ",@ can only be used with lists");
};
};
for (el in newElements) {
l.insert(idx++, el);
}

View File

@@ -14,4 +14,9 @@ class MacroTestCase extends Test {
Assert.equals(5, myVar);
Assert.equals(6, myFunc());
}
function testExpandList() {
Assert.equals(6, sum1());
Assert.equals(6, sum2());
}
}

View File

@@ -3,4 +3,13 @@
(defvar ,varName 5)
(defun ,funcName [] 6)})
(defMultiple myVar myFunc)
(defMultiple myVar myFunc)
(defmacro variadicPlus [&rest l]
`(+ ,@l))
(defmacro listPlus [l]
`(+ ,@l))
// Both forms of passing expression lists to macros should work:
(defun sum1 [] (variadicPlus 1 2 3))
(defun sum2 [] (listPlus [1 2 3]))