arithmetic assignment

This commit is contained in:
2021-01-04 13:02:50 -07:00
parent 3e81f23028
commit 0c3427cf84
3 changed files with 32 additions and 0 deletions

View File

@@ -19,6 +19,13 @@ class Macros {
public static function builtins() {
var macros:Map<String, MacroFunction> = [];
function destructiveVersion(op:String, assignOp:String) {
macros[assignOp] = (wholeExp:ReaderExp, exps:Array<ReaderExp>, 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<ReaderExp>, 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<ReaderExp>, 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");

View File

@@ -265,6 +265,10 @@ class BasicTestCase extends Test {
function testLoadedFunction() {
Assert.equals("loaded", BasicTestCase.loadedFunction());
}
function testAssignArith() {
_testAssignArith();
}
}
class BasicObject {

View File

@@ -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))