diff --git a/src/kiss/Macros.hx b/src/kiss/Macros.hx index da8c5b0..6826012 100644 --- a/src/kiss/Macros.hx +++ b/src/kiss/Macros.hx @@ -176,9 +176,10 @@ class Macros { macros["#if"] = (wholeExp:ReaderExp, exps:Array, k) -> { wholeExp.checkNumArgs(2, 3, '(#if [cond] [then] [?else])'); + var b = wholeExp.expBuilder(); var conditionExp = exps.shift(); var thenExp = exps.shift(); - var elseExp = if (exps.length > 0) exps.shift(); else null; + var elseExp = if (exps.length > 0) exps.shift(); else b.symbol("null"); var parser = new Parser(); var conditionInterp = new KissInterp(true); @@ -198,7 +199,7 @@ class Macros { } }; - function bodyIf(formName:String, negated:Bool, wholeExp:ReaderExp, args:Array, k) { + function bodyIf(formName:String, underlyingIf:String, negated:Bool, wholeExp:ReaderExp, args:Array, k) { wholeExp.checkNumArgs(2, null, '($formName [condition] [body...])'); var b = wholeExp.expBuilder(); var condition = if (negated) { @@ -209,13 +210,15 @@ class Macros { } else { args[0]; } - return b.call(b.symbol("if"), [ + return b.call(b.symbol(underlyingIf), [ condition, b.begin(args.slice(1)) ]); } - macros["when"] = bodyIf.bind("when", false); - macros["unless"] = bodyIf.bind("unless", true); + macros["when"] = bodyIf.bind("when", "if", false); + macros["unless"] = bodyIf.bind("unless", "if", true); + macros["#when"] = bodyIf.bind("#when", "#if", false); + macros["#unless"] = bodyIf.bind("#unless", "#if", true); macros["cond"] = cond; diff --git a/src/test/cases/ConditionalCompilationTestCase.hx b/src/test/cases/ConditionalCompilationTestCase.hx index a7d9c11..6f7306b 100644 --- a/src/test/cases/ConditionalCompilationTestCase.hx +++ b/src/test/cases/ConditionalCompilationTestCase.hx @@ -17,4 +17,20 @@ class ConditionalCompilationTestCase extends Test { Assert.isTrue(runningInPyOrJs); #end } + + function testWhen() { + #if interp + Assert.equals(6, number()); + #else + Assert.equals(5, number()); + #end + } + + function testUnless() { + #if !interp + Assert.equals(9, number2()); + #else + Assert.equals(12, number2()); + #end + } } diff --git a/src/test/cases/ConditionalCompilationTestCase.kiss b/src/test/cases/ConditionalCompilationTestCase.kiss index 8564fa2..9530d76 100644 --- a/src/test/cases/ConditionalCompilationTestCase.kiss +++ b/src/test/cases/ConditionalCompilationTestCase.kiss @@ -1,3 +1,16 @@ (defvar runningInHaxe (#if interp true false)) (defvar runningInPyOrJs (#if (or py js) true false)) +(defun number [] + (let [&mut num 5] + (#when interp + (+= num 5) + (-= num 4)) + num)) + +(defun number2 [] + (let [&mut num 12] + (#unless interp + (+= num 5) + (-= num 8)) + num)) \ No newline at end of file