fix an (if... ) bug

This commit is contained in:
2020-11-27 22:15:49 -07:00
parent 7f5d352b37
commit 7642ca3b36
4 changed files with 13 additions and 5 deletions

View File

@@ -7,6 +7,7 @@ import kiss.CompileError;
import kiss.Types; import kiss.Types;
using kiss.Reader; using kiss.Reader;
using kiss.Helpers;
using StringTools; using StringTools;
class Helpers { class Helpers {
@@ -85,10 +86,7 @@ class Helpers {
default: default:
throw CompileError.fromExp(argList, 'expected an argument list'); throw CompileError.fromExp(argList, 'expected an argument list');
}, },
expr: { expr: EReturn(convert(CallExp(Symbol("begin").withPos(body[0].pos), body).withPos(body[0].pos))).withContextPos()
pos: Context.currentPos(),
expr: EReturn(convert(CallExp(Symbol("begin").withPos(body[0].pos), body).withPos(body[0].pos)))
}
} }
} }
} }

View File

@@ -190,7 +190,13 @@ class SpecialForms {
var condition = macro Prelude.truthy(${convert(args[0])}); var condition = macro Prelude.truthy(${convert(args[0])});
var thenExp = convert(args[1]); var thenExp = convert(args[1]);
var elseExp = if (args.length > 2) convert(args[2]) else null; var elseExp = if (args.length > 2) {
convert(args[2]);
} else {
// Kiss (if... ) expressions all need to generate a Haxe else block
// to make sure they always return something
macro null;
};
macro if ($condition) macro if ($condition)
$thenExp $thenExp

View File

@@ -120,6 +120,8 @@ class BasicTestCase extends Test {
Assert.equals(false, BasicTestCase.myIf8); Assert.equals(false, BasicTestCase.myIf8);
Assert.equals(false, BasicTestCase.myIf9); Assert.equals(false, BasicTestCase.myIf9);
Assert.equals(true, BasicTestCase.myIf10); Assert.equals(true, BasicTestCase.myIf10);
Assert.equals(5, BasicTestCase.myIf11);
Assert.equals(null, BasicTestCase.myIf12);
} }
function testMacros() { function testMacros() {

View File

@@ -64,6 +64,8 @@
(defvar myIf8 (if "" true false)) (defvar myIf8 (if "" true false))
(defvar myIf9 (if [] true false)) (defvar myIf9 (if [] true false))
(defvar myIf10 (if [1] true false)) (defvar myIf10 (if [1] true false))
(defvar myIf11 (if true 5))
(defvar myIf12 (if false 5))
(defvar :Int myInt 8) (defvar :Int myInt 8)