while & until

This commit is contained in:
2021-11-14 22:17:37 -07:00
parent ab07676d58
commit 9890360f55

View File

@@ -243,10 +243,22 @@ class SpecialForms {
};
map["loop"] = (wholeExp:ReaderExp, args:Array<ReaderExp>, k:KissState) -> {
wholeExp.checkNumArgs(1, null, '(loop [body...])');
wholeExp.checkNumArgs(1, null, '(loop <body...>)');
EWhile(macro true, k.convert(wholeExp.expBuilder().begin(args)), true).withMacroPosOf(wholeExp);
};
function whileForm(invert:Bool, wholeExp:ReaderExp, args:Array<ReaderExp>, k:KissState) {
var funcName = if (invert) "until" else "while";
wholeExp.checkNumArgs(2, null, '($funcName <condition> <body...>)');
var b = wholeExp.expBuilder();
var cond = k.convert(b.callSymbol("Prelude.truthy", [args[0]]));
if (invert) cond = macro !$cond;
return EWhile(cond, k.convert(b.begin(args)), true).withMacroPosOf(wholeExp);
}
map["while"] = whileForm.bind(false);
map["until"] = whileForm.bind(true);
map["return"] = (wholeExp:ReaderExp, args:Array<ReaderExp>, k:KissState) -> {
wholeExp.checkNumArgs(0, 1, '(return [?value])');
var returnExpr = if (args.length == 1) k.convert(args[0]) else null;