From 4352bcf2f463db214cb3ebe50cafe91287eabc85 Mon Sep 17 00:00:00 2001 From: Nat Quayle Nelson Date: Mon, 4 Jan 2021 13:02:50 -0700 Subject: [PATCH] arithmetic assignment --- src/kiss/Macros.hx | 13 +++++++++++++ src/test/cases/BasicTestCase.hx | 4 ++++ src/test/cases/BasicTestCase.kiss | 15 +++++++++++++++ 3 files changed, 32 insertions(+) diff --git a/src/kiss/Macros.hx b/src/kiss/Macros.hx index b5d42aa..01b4f4b 100644 --- a/src/kiss/Macros.hx +++ b/src/kiss/Macros.hx @@ -19,6 +19,13 @@ class Macros { public static function builtins() { var macros:Map = []; + function destructiveVersion(op:String, assignOp:String) { + macros[assignOp] = (wholeExp:ReaderExp, exps:Array, k) -> { + wholeExp.checkNumArgs(2, null, '($assignOp [var] [v1] [values...])'); + CallExp(Symbol("set").withPosOf(wholeExp), [exps[0], CallExp(Symbol(op).withPosOf(wholeExp), exps).withPosOf(wholeExp)]).withPosOf(wholeExp); + }; + } + macros["%"] = (wholeExp:ReaderExp, exps:Array, k) -> { wholeExp.checkNumArgs(2, 2, '(% [divisor] [dividend])'); CallExp(Symbol("kiss.Operand.toDynamic").withPosOf(wholeExp), [ @@ -28,6 +35,7 @@ class Macros { ]).withPosOf(wholeExp) ]).withPosOf(wholeExp); }; + destructiveVersion("%", "%="); macros["^"] = (wholeExp:ReaderExp, exps:Array, k) -> { wholeExp.checkNumArgs(2, 2, '(^ [base] [exponent])'); @@ -38,14 +46,19 @@ class Macros { ]).withPosOf(wholeExp) ]).withPosOf(wholeExp); }; + destructiveVersion("^", "^="); macros["+"] = variadicMacro("Prelude.add"); + destructiveVersion("+", "+="); macros["-"] = variadicMacro("Prelude.subtract"); + destructiveVersion("-", "-="); macros["*"] = variadicMacro("Prelude.multiply"); + destructiveVersion("*", "*="); macros["/"] = variadicMacro("Prelude.divide"); + destructiveVersion("/", "/="); macros["min"] = variadicMacro("Prelude.min"); macros["max"] = variadicMacro("Prelude.max"); diff --git a/src/test/cases/BasicTestCase.hx b/src/test/cases/BasicTestCase.hx index d55e5fc..df82a90 100644 --- a/src/test/cases/BasicTestCase.hx +++ b/src/test/cases/BasicTestCase.hx @@ -265,6 +265,10 @@ class BasicTestCase extends Test { function testLoadedFunction() { Assert.equals("loaded", BasicTestCase.loadedFunction()); } + + function testAssignArith() { + _testAssignArith(); + } } class BasicObject { diff --git a/src/test/cases/BasicTestCase.kiss b/src/test/cases/BasicTestCase.kiss index 0b13ae3..d77fe71 100644 --- a/src/test/cases/BasicTestCase.kiss +++ b/src/test/cases/BasicTestCase.kiss @@ -422,3 +422,18 @@ (defun _testCallAlias [] (let [map [=>"hey" "you"]] (Assert.equals "you" (dictGet map "hey")))) + +(defun _testAssignArith [] + (deflocal &mut num 5) + (+= num 5 6) + (Assert.equals 16 num) + (%= num 5) + (Assert.equals 1 num) + (^= num 3) + (Assert.equals 1 num) + (*= num 25 2) + (Assert.equals 50 num) + (/= num 25 2) + (Assert.equals 1 num) + (-= num 5 6) + (Assert.equals -10 num)) \ No newline at end of file