conditional compilation \#cond
This commit is contained in:
@@ -220,7 +220,8 @@ class Macros {
|
|||||||
macros["#when"] = bodyIf.bind("#when", "#if", false);
|
macros["#when"] = bodyIf.bind("#when", "#if", false);
|
||||||
macros["#unless"] = bodyIf.bind("#unless", "#if", true);
|
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
|
// (or... ) uses (cond... ) under the hood
|
||||||
macros["or"] = (wholeExp:ReaderExp, args:Array<ReaderExp>, k) -> {
|
macros["or"] = (wholeExp:ReaderExp, args:Array<ReaderExp>, k) -> {
|
||||||
@@ -740,16 +741,16 @@ class Macros {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// cond expands telescopically into a nested if expression
|
// 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...])");
|
wholeExp.checkNumArgs(1, null, "(cond [cases...])");
|
||||||
var b = wholeExp.expBuilder();
|
var b = wholeExp.expBuilder();
|
||||||
return switch (exps[0].def) {
|
return switch (exps[0].def) {
|
||||||
case CallExp(condition, body):
|
case CallExp(condition, body):
|
||||||
b.call(b.symbol("if"), [
|
b.call(b.symbol(underlyingIf), [
|
||||||
condition,
|
condition,
|
||||||
b.begin(body),
|
b.begin(body),
|
||||||
if (exps.length > 1) {
|
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 {
|
} else {
|
||||||
b.symbol("null");
|
b.symbol("null");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,4 +33,18 @@ class ConditionalCompilationTestCase extends Test {
|
|||||||
Assert.equals(12, number2());
|
Assert.equals(12, number2());
|
||||||
#end
|
#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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,4 +13,12 @@
|
|||||||
(#unless interp
|
(#unless interp
|
||||||
(+= num 5)
|
(+= num 5)
|
||||||
(-= num 8))
|
(-= num 8))
|
||||||
num))
|
num))
|
||||||
|
|
||||||
|
(defvar targetLanguage
|
||||||
|
(#cond
|
||||||
|
(cpp "C++")
|
||||||
|
(interp "Haxe")
|
||||||
|
(hxnodejs "NodeJS")
|
||||||
|
(js "JavaScript")
|
||||||
|
(py "Python")))
|
||||||
Reference in New Issue
Block a user