diff --git a/src/kiss/Macros.hx b/src/kiss/Macros.hx index 8f729001..1571ac9b 100644 --- a/src/kiss/Macros.hx +++ b/src/kiss/Macros.hx @@ -9,14 +9,34 @@ class Macros { public static function builtins() { var macros:Map = []; - macros["+"] = (exps) -> { - CallExp(Symbol("Lambda.fold"), [ListExp(exps), Symbol("Prelude.add"), Symbol("0")]); + macros["+"] = foldMacro("Prelude.add"); + + macros["-"] = foldMacro("Prelude.subtract"); + + macros["*"] = foldMacro("Prelude.multiply"); + + macros["/"] = foldMacro("Prelude.divide"); + + macros["%"] = (exps:Array) -> { + if (exps.length != 2) { + throw 'Got ${exps.length} arguments for % instead of 2.'; + } + CallExp(Symbol("Prelude.mod"), [exps[1], exps[0]]); }; - macros["-"] = (exps:Array) -> { - CallExp(Symbol("Lambda.fold"), [ListExp(exps.slice(1)), Symbol("Prelude.subtract"), exps[0]]); - } + macros["^"] = (exps:Array) -> { + if (exps.length != 2) { + throw 'Got ${exps.length} arguments for ^ instead of 2.'; + } + CallExp(Symbol("Prelude.pow"), [exps[1], exps[0]]); + }; return macros; } + + static function foldMacro(func:String):MacroFunction { + return (exps) -> { + CallExp(Symbol("Lambda.fold"), [ListExp(exps.slice(1)), Symbol(func), exps[0]]); + }; + } } diff --git a/src/kiss/Prelude.hx b/src/kiss/Prelude.hx index da888ecf..d44032cf 100644 --- a/src/kiss/Prelude.hx +++ b/src/kiss/Prelude.hx @@ -8,4 +8,20 @@ class Prelude { public static function subtract(val, from) { return from - val; } + + public static function multiply(a, b) { + return a * b; + } + + public static function divide(bottom:Float, top:Float) { + return top / bottom; + } + + public static function mod(bottom, top) { + return top % bottom; + } + + public static function pow(exponent, base) { + return Math.pow(base, exponent); + } } diff --git a/src/test/cases/BasicTestCase.hx b/src/test/cases/BasicTestCase.hx index 74a9016e..b6ac3e39 100644 --- a/src/test/cases/BasicTestCase.hx +++ b/src/test/cases/BasicTestCase.hx @@ -50,4 +50,20 @@ class BasicTestCase extends Test { function testVariadicSubtract() { Assert.equals(-2, BasicTestCase.myDifference); } + + function testVariadicMultiply() { + Assert.equals(60, BasicTestCase.myProduct); + } + + function testVariadicDivide() { + Assert.equals(0.5, BasicTestCase.myQuotient); + } + + function testMod() { + Assert.equals(4, BasicTestCase.myRemainder); + } + + function testPow() { + Assert.equals(256, BasicTestCase.myPower); + } } diff --git a/src/test/cases/BasicTestCase.kiss b/src/test/cases/BasicTestCase.kiss index 0263030d..efad68a7 100644 --- a/src/test/cases/BasicTestCase.kiss +++ b/src/test/cases/BasicTestCase.kiss @@ -24,4 +24,12 @@ // Variadic math uses haxe's Lambda.fold under the hood (defvar mySum (+ 1 2 3)) -(defvar myDifference (- 5 4 3)) \ No newline at end of file +(defvar myDifference (- 5 4 3)) + +(defvar myProduct (* 2 5 6)) + +(defvar myQuotient (/ 6 3 2 2)) + +(defvar myRemainder (% 10 6)) + +(defvar myPower (^ 2 8)) \ No newline at end of file