add when, unless to portable macros

This commit is contained in:
2025-11-14 13:54:56 -06:00
parent e481120950
commit 57b61fea7e
2 changed files with 21 additions and 15 deletions

View File

@@ -241,21 +241,7 @@ class Macros {
function addBodyIf(keywordName:String, underlyingIf:String, negated:Bool) {
k.doc(keywordName, 2, null, '($keywordName <condition> <body...>)');
macros[keywordName] = (wholeExp:ReaderExp, args:Array<ReaderExp>, k) -> {
var b = wholeExp.expBuilder();
var condition = if (negated) {
b.call(
b.symbol("not"), [
args[0]
]);
} else {
args[0];
}
return b.call(b.symbol(underlyingIf), [
condition,
b.begin(args.slice(1))
]);
};
macros[keywordName] = wrapPortable(PortableMacros.bodyIf(underlyingIf, negated));
}
addBodyIf("when", "if", false);
addBodyIf("unless", "if", true);

View File

@@ -13,6 +13,8 @@ class PortableMacros {
m["-="] = destructiveVersion("-");
m["*="] = destructiveVersion("*");
m["/="] = destructiveVersion("/");
m["when"] = bodyIf("if", false);
m["unless"] = bodyIf("if", true);
return m;
}
@@ -28,4 +30,22 @@ class PortableMacros {
]);
};
}
public static function bodyIf(underlyingIf:String, negated:Bool) {
return (wholeExp:ReaderExp, args:Array<ReaderExp>) -> {
var b = wholeExp.expBuilder();
var condition = if (negated) {
b.call(
b.symbol("not"), [
args[0]
]);
} else {
args[0];
}
return b.call(b.symbol(underlyingIf), [
condition,
b.begin(args.slice(1))
]);
};
}
}