Variadic comparisons

This commit is contained in:
2020-11-14 17:55:33 -07:00
parent 0fe8cbd28b
commit a8d740d117
5 changed files with 61 additions and 6 deletions

View File

@@ -33,8 +33,14 @@ class Macros {
macros["min"] = foldMacro("Prelude.minInclusive");
macros["_min"] = foldMacro("Prelude._minExclusive");
macros["max"] = foldMacro("Prelude.maxInclusive");
macros["_max"] = foldMacro("Prelude._maxExclusive");
macros["_eq"] = foldMacro("Prelude.areEqual");
return macros;
}

View File

@@ -29,19 +29,19 @@ class Prelude {
return Math.min(a, b);
}
public static function minExclusive(a, b) {
return if (a == b) null else Math.min(a, b);
public static function _minExclusive(a, b) {
return if (a == b) Math.NEGATIVE_INFINITY else Math.min(a, b);
}
public static function maxInclusive(a, b) {
return Math.max(a, b);
}
public static function maxExclusive(a, b) {
return if (a == b) null else Math.max(a, b);
public static function _maxExclusive(a, b) {
return if (a == b) Math.POSITIVE_INFINITY else Math.max(a, b);
}
public static function areEqual(a, b) {
return if (a == b) a else null;
return if (a == b) a else Math.NaN;
}
}

View File

@@ -22,6 +22,19 @@ class SpecialForms {
expr: EArray(convert(args[0]), convert(args[1]))
};
map["<"] = foldComparison("_min");
map["<="] = foldComparison("min");
map[">"] = foldComparison("_max");
map[">="] = foldComparison("max");
map["="] = foldComparison("_eq");
return map;
}
static function foldComparison(func:String) {
return (args:Array<ReaderExp>, convert:ExprConversion) -> {
pos: Context.currentPos(),
expr: EBinop(OpEq, convert(args[0]), convert(CallExp(Symbol(func), args)))
};
}
}

View File

@@ -82,4 +82,29 @@ class BasicTestCase extends Test {
function testMax() {
Assert.equals(9, BasicTestCase.myMax);
}
function testLessThan() {
Assert.equals(true, BasicTestCase.myComp1);
Assert.equals(false, BasicTestCase.myComp2);
}
function testLesserEqual() {
Assert.equals(true, BasicTestCase.myComp3);
Assert.equals(true, BasicTestCase.myComp4);
}
function testGreaterThan() {
Assert.equals(true, BasicTestCase.myComp5);
Assert.equals(false, BasicTestCase.myComp6);
}
function testGreaterEqual() {
Assert.equals(true, BasicTestCase.myComp7);
Assert.equals(true, BasicTestCase.myComp8);
}
function testEqual() {
Assert.equals(true, BasicTestCase.myComp9);
Assert.equals(false, BasicTestCase.myComp10);
}
}

View File

@@ -41,4 +41,15 @@
(defvar myInc ++_myNum)
(defvar myMin (min 9 3 7 1))
(defvar myMax (max 9 3 7 1))
(defvar myMax (max 9 3 7 1))
(defvar myComp1 (< 1 2 3 4))
(defvar myComp2 (< 1 1 3 4))
(defvar myComp3 (<= 1 2 3 4))
(defvar myComp4 (<= 1 1 3 4))
(defvar myComp5 (> 4 3 2 1))
(defvar myComp6 (> 4 4 2 1))
(defvar myComp7 (>= 4 3 2 1))
(defvar myComp8 (>= 4 4 2 1))
(defvar myComp9 (= 1 1 1 1))
(defvar myComp10 (= 1 2 1 1))