&builder arg for defMacro

This commit is contained in:
2021-11-17 14:40:44 -07:00
parent 7b1ba83561
commit 3196f45d4f
2 changed files with 12 additions and 2 deletions

View File

@@ -403,6 +403,7 @@ class Macros {
var macroCallForm = '($name'; var macroCallForm = '($name';
var builderName:String = null;
for (arg in argList) { for (arg in argList) {
if (restIndex != -1) { if (restIndex != -1) {
throw CompileError.fromExp(arg, "macros cannot declare arguments after a &rest or &body argument"); throw CompileError.fromExp(arg, "macros cannot declare arguments after a &rest or &body argument");
@@ -417,6 +418,12 @@ class Macros {
macroCallForm += ' [?$name]'; macroCallForm += ' [?$name]';
} }
++maxArgs; ++maxArgs;
case MetaExp("builder", {pos: _, def: Symbol(name)}):
if (builderName == null) {
builderName = name;
} else {
throw CompileError.fromExp(arg, 'Cannot declare multiple &builder args. Already declared: $builderName');
}
case MetaExp("opt", {pos: _, def: Symbol(name)}): case MetaExp("opt", {pos: _, def: Symbol(name)}):
argNames.push(name); argNames.push(name);
macroCallForm += ' [?$name]'; macroCallForm += ' [?$name]';
@@ -453,6 +460,9 @@ class Macros {
var innerArgNames = argNames.copy(); var innerArgNames = argNames.copy();
var args:Map<String, Dynamic> = []; var args:Map<String, Dynamic> = [];
if (builderName != null) {
args[builderName] = b;
}
for (idx in 0...optIndex) { for (idx in 0...optIndex) {
args[innerArgNames.shift()] = innerExps[idx]; args[innerArgNames.shift()] = innerExps[idx];
} }

View File

@@ -62,9 +62,9 @@
// Calling (set) on a macroVar is a faux-pas, because (setMacroVar) is the right way // Calling (set) on a macroVar is a faux-pas, because (setMacroVar) is the right way
(defMacroVar count 0) (defMacroVar count 0)
(defMacro _testSetMacroVarMacro [] (defMacro _testSetMacroVarMacro [&builder b]
(assertThrows (set count (+ count 1))) (assertThrows (set count (+ count 1)))
(ReaderExp.StrExp (Std.string count))) (b.str (Std.string count)))
(function _testSetMacroVar [] (function _testSetMacroVar []
(_testSetMacroVarMacro) (_testSetMacroVarMacro)