conditional compilation \#cond

This commit is contained in:
2021-07-12 13:46:53 -06:00
parent 79108fd28d
commit 59c6b59e37
3 changed files with 28 additions and 5 deletions

View File

@@ -220,7 +220,8 @@ class Macros {
macros["#when"] = bodyIf.bind("#when", "#if", false);
macros["#unless"] = bodyIf.bind("#unless", "#if", true);
macros["cond"] = cond;
macros["cond"] = cond.bind("if");
macros["#cond"] = cond.bind("#if");
// (or... ) uses (cond... ) under the hood
macros["or"] = (wholeExp:ReaderExp, args:Array<ReaderExp>, k) -> {
@@ -740,16 +741,16 @@ class Macros {
}
// cond expands telescopically into a nested if expression
static function cond(wholeExp:ReaderExp, exps:Array<ReaderExp>, k:KissState) {
static function cond(underlyingIf:String, wholeExp:ReaderExp, exps:Array<ReaderExp>, k:KissState) {
wholeExp.checkNumArgs(1, null, "(cond [cases...])");
var b = wholeExp.expBuilder();
return switch (exps[0].def) {
case CallExp(condition, body):
b.call(b.symbol("if"), [
b.call(b.symbol(underlyingIf), [
condition,
b.begin(body),
if (exps.length > 1) {
cond(b.call(b.symbol("cond"), exps.slice(1)), exps.slice(1), k);
cond(underlyingIf, b.call(b.symbol("cond"), exps.slice(1)), exps.slice(1), k);
} else {
b.symbol("null");
}

View File

@@ -33,4 +33,18 @@ class ConditionalCompilationTestCase extends Test {
Assert.equals(12, number2());
#end
}
function testCond() {
#if cpp
Assert.equals("C++", targetLanguage);
#elseif interp
Assert.equals("Haxe", targetLanguage);
#elseif js
Assert.equals("JavaScript", targetLanguage);
#elseif hxnodejs
Assert.equals("NodeJS", targetLanguage);
#elseif py
Assert.equals("Python", targetLanguage);
#end
}
}

View File

@@ -13,4 +13,12 @@
(#unless interp
(+= num 5)
(-= num 8))
num))
num))
(defvar targetLanguage
(#cond
(cpp "C++")
(interp "Haxe")
(hxnodejs "NodeJS")
(js "JavaScript")
(py "Python")))