if
This commit is contained in:
13
src/kiss/Helpers.hx
Normal file
13
src/kiss/Helpers.hx
Normal file
@@ -0,0 +1,13 @@
|
||||
package kiss;
|
||||
|
||||
import haxe.macro.Expr;
|
||||
import haxe.macro.Context;
|
||||
|
||||
class Helpers {
|
||||
public static function withPos(e:ExprDef):Expr {
|
||||
return {
|
||||
pos: Context.currentPos(),
|
||||
expr: e
|
||||
};
|
||||
}
|
||||
}
|
@@ -44,4 +44,21 @@ class Prelude {
|
||||
public static function areEqual(a, b) {
|
||||
return if (a == b) a else Math.NaN;
|
||||
}
|
||||
|
||||
public static dynamic function truthy(v:Any) {
|
||||
return switch (Type.typeof(v)) {
|
||||
case TNull: false;
|
||||
case TInt | TFloat: (v : Float) > 0;
|
||||
case TBool: (v : Bool);
|
||||
default:
|
||||
// Empty strings are falsy
|
||||
var str = cast(v, String);
|
||||
if (str != null) {
|
||||
str.length > 0;
|
||||
} else {
|
||||
// Any other value is true by default
|
||||
true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -5,6 +5,8 @@ import haxe.macro.Context;
|
||||
import kiss.Reader;
|
||||
import kiss.Types;
|
||||
|
||||
using kiss.Helpers;
|
||||
|
||||
// Special forms convert Kiss reader expressions into Haxe macro expressions
|
||||
typedef SpecialFormFunction = (args:Array<ReaderExp>, convert:ExprConversion) -> Expr;
|
||||
|
||||
@@ -22,12 +24,26 @@ class SpecialForms {
|
||||
expr: EArray(convert(args[0]), convert(args[1]))
|
||||
};
|
||||
|
||||
// TODO first through tenth
|
||||
|
||||
map["<"] = foldComparison("_min");
|
||||
map["<="] = foldComparison("min");
|
||||
map[">"] = foldComparison("_max");
|
||||
map[">="] = foldComparison("max");
|
||||
map["="] = foldComparison("_eq");
|
||||
|
||||
map["if"] = (args:Array<ReaderExp>, convert:ExprConversion) -> {
|
||||
if (args.length < 2 || args.length > 3) {
|
||||
throw 'if statement has wrong number of arguments: ${args.length}';
|
||||
}
|
||||
|
||||
var condition = macro Prelude.truthy(${convert(args[0])});
|
||||
var thenExp = convert(args[1]);
|
||||
var elseExp = if (args.length > 2) convert(args[2]) else null;
|
||||
|
||||
EIf(condition, thenExp, elseExp).withPos();
|
||||
};
|
||||
|
||||
return map;
|
||||
}
|
||||
|
||||
|
@@ -107,4 +107,15 @@ class BasicTestCase extends Test {
|
||||
Assert.equals(true, BasicTestCase.myComp9);
|
||||
Assert.equals(false, BasicTestCase.myComp10);
|
||||
}
|
||||
|
||||
function testIf() {
|
||||
Assert.equals(true, BasicTestCase.myIf1);
|
||||
Assert.equals(false, BasicTestCase.myIf2);
|
||||
Assert.equals(false, BasicTestCase.myIf3);
|
||||
Assert.equals(false, BasicTestCase.myIf4);
|
||||
Assert.equals(true, BasicTestCase.myIf5);
|
||||
Assert.equals(false, BasicTestCase.myIf6);
|
||||
Assert.equals(true, BasicTestCase.myIf7);
|
||||
Assert.equals(false, BasicTestCase.myIf8);
|
||||
}
|
||||
}
|
||||
|
@@ -52,4 +52,13 @@
|
||||
(defvar myComp7 (>= 4 3 2 1))
|
||||
(defvar myComp8 (>= 4 4 2 1))
|
||||
(defvar myComp9 (= 1 1 1 1))
|
||||
(defvar myComp10 (= 1 2 1 1))
|
||||
(defvar myComp10 (= 1 2 1 1))
|
||||
|
||||
(defvar myIf1 (if 1 true false))
|
||||
(defvar myIf2 (if 0 true false))
|
||||
(defvar myIf3 (if -1 true false))
|
||||
(defvar myIf4 (if null true false))
|
||||
(defvar myIf5 (if true true false))
|
||||
(defvar myIf6 (if false true false))
|
||||
(defvar myIf7 (if "string" true false))
|
||||
(defvar myIf8 (if "" true false))
|
Reference in New Issue
Block a user