(setMacroVar) instead of (defMacroVar)

This commit is contained in:
2021-11-11 13:31:04 -07:00
parent db3402f885
commit 7518838777
4 changed files with 16 additions and 3 deletions

View File

@@ -406,6 +406,10 @@ class Helpers {
for (name => value in k.macroVars) {
interp.variables.set(name, value);
}
interp.variables.set("_setMacroVar", (name, value) -> {
k.macroVars[name] = value;
interp.variables.set(name, value);
});
function innerRunAtCompileTimeDynamic(innerExp:ReaderExp) {
// in case macroVars have changed

View File

@@ -407,7 +407,7 @@ class Kiss {
case Symbol(varName) if (k.macroVars.exists(varName)):
var b = wholeExp.expBuilder();
// have this throw during macroEXPANSION, not before (so assertThrows will catch it)
copy.convert(b.throwCompileError('If you intend to change macroVar $varName, use defMacroVar instead. If not, rename your local variable for clarity.'));
copy.convert(b.throwCompileError('If you intend to change macroVar $varName, use setMacroVar instead. If not, rename your local variable for clarity.'));
default:
setLocal(wholeExp, exps, copy);
};

View File

@@ -798,6 +798,15 @@ class Macros {
return null;
};
macros["setMacroVar"] = (wholeExp:ReaderExp, exps:Array<ReaderExp>, k:KissState) -> {
wholeExp.checkNumArgs(2, 2, "(setMacroVar <name> <value>)");
var name = exps[0].symbolName().withPosOf(exps[0]);
var b = wholeExp.expBuilder();
return b.callSymbol("_setMacroVar", [name, exps[1]]);
};
macros["defMacroFunction"] = (wholeExp:ReaderExp, exps:Array<ReaderExp>, k:KissState) -> {
wholeExp.checkNumArgs(3, null, "(defMacroFunction <name> [<args>] <body...>)");
var b = wholeExp.expBuilder();

View File

@@ -60,7 +60,7 @@
(_testPrintAtMacroTimeMacro)
(Assert.pass))
// Calling (set) on a macroVar is a faux-pas, but calling defMacroVar again is not
// Calling (set) on a macroVar is a faux-pas, because (setMacroVar) is the right way
(defMacroVar count 0)
(defMacro _testSetMacroVarMacro []
(assertThrows (set count (+ count 1)))
@@ -71,7 +71,7 @@
(Assert.pass))
(defMacro _testRedefineMacroVarMacro []
(defMacroVar count (+ count 1))
(setMacroVar count (+ count 1))
(symbol (Std.string count)))
(function _testRedefineMacroVar []