withFunctions. close #15

This commit is contained in:
2022-12-06 01:50:59 +00:00
parent 4811395d43
commit a66202b7e9
3 changed files with 39 additions and 1 deletions

View File

@@ -1412,6 +1412,31 @@ class Macros {
].concat([for (savedVar in savedVarList) b.set(savedVar, uuids.shift())]));
};
k.doc("withFunctions", 2, null, "(withFunctions [(<funcName1> [<args...>] <body...>) <more functions...>] <body...>)");
macros["withFunctions"] = (wholeExp:ReaderExp, args:Array<ReaderExp>, k:KissState) -> {
var b = wholeExp.expBuilder();
var funcList = Helpers.argList(args[0], "withFunctions");
if (funcList.length == 0) throw KissError.fromExp(args[0], "withFunctions requires at least one function definition");
var localFunctions = [];
do {
var funcExp = funcList.shift();
switch (funcExp.def) {
case CallExp(nameExp, argsAndBody) if (argsAndBody.length >= 2):
localFunctions.push(b.callSymbol("localFunction", [
nameExp, argsAndBody.shift()
].concat(argsAndBody)));
default:
throw KissError.fromExp(funcExp, "withFunctions function definition should follow this form: (<funcName> [<args...>] <body...>)");
}
} while (funcList.length > 0);
var exp = b.begin(localFunctions.concat(args.slice(1)));
Prelude.print(Reader.toString(exp.def));
exp;
};
return macros;
}

View File

@@ -391,6 +391,10 @@ class BasicTestCase extends Test {
function testHaxeProperties() {
_testHaxeProperties();
}
function testWithFunctions() {
_testWithFunctions();
}
}
class BasicObject {

View File

@@ -737,4 +737,13 @@ From:[(assert false (+ \"false \" \"should \" \"have \" \"been \" \"true\"))]" m
(Assert.equals 5 staticProp)
(Assert.equals 9 (set staticProp 9))
(Assert.equals 9 staticProp)
(Assert.equals 9 _staticProp))
(Assert.equals 9 _staticProp))
(function _testWithFunctions []
(withFunctions
[
(a [val] (+ val 1))
(b [val] (+ val 2))
]
(localFunction c [val] (+ (a val) (b val)))
(Assert.equals 5 (c 1))))