diff --git a/hy/compiler.py b/hy/compiler.py index fe3fa64..3f1baf3 100644 --- a/hy/compiler.py +++ b/hy/compiler.py @@ -751,6 +751,45 @@ class HyASTCompiler(object): left = calc return calc + @builds("+=") + @builds("/=") + @builds("//=") + @builds("*=") + @builds("_=") + @builds("%=") + @builds("**=") + @builds("<<=") + @builds(">>=") + @builds("|=") + @builds("^=") + @builds("&=") + @checkargs(2) + def compile_augassign_expression(self, expression): + ops = {"+=": ast.Add, + "/=": ast.Div, + "//=": ast.FloorDiv, + "*=": ast.Mult, + "_=": ast.Sub, + "%=": ast.Mod, + "**=": ast.Pow, + "<<=": ast.LShift, + ">>=": ast.RShift, + "|=": ast.BitOr, + "^=": ast.BitXor, + "&=": ast.BitAnd} + + op = ops[expression[0]] + + target = self._storeize(self.compile(expression[1])) + value = self.compile(expression[2]) + + return ast.AugAssign( + target=target, + value=value, + op=op(), + lineno=expression.start_line, + col_offset=expression.start_column) + def compile_dotted_expression(self, expr): ofn = expr.pop(0) # .join diff --git a/tests/native_tests/math.hy b/tests/native_tests/math.hy index dbaabc4..471e8e4 100644 --- a/tests/native_tests/math.hy +++ b/tests/native_tests/math.hy @@ -63,3 +63,75 @@ (defn test-bitand [] "NATIVE: test lshift" (assert (= (& 1 2) 0))) + +(defn test-augassign-add [] + "NATIVE: test augassign add" + (let [[x 1]] + (+= x 41) + (assert (= x 42)))) + +(defn test-augassign-sub [] + "NATIVE: test augassign sub" + (let [[x 1]] + (-= x 41) + (assert (= x -40)))) + +(defn test-augassign-mult [] + "NATIVE: test augassign mult" + (let [[x 1]] + (*= x 41) + (assert (= x 41)))) + +(defn test-augassign-div [] + "NATIVE: test augassign div" + (let [[x 42]] + (/= x 2) + (assert (= x 21)))) + +(defn test-augassign-floordiv [] + "NATIVE: test augassign floordiv" + (let [[x 42]] + (//= x 2) + (assert (= x 21)))) + +(defn test-augassign-mod [] + "NATIVE: test augassign mod" + (let [[x 42]] + (%= x 2) + (assert (= x 0)))) + +(defn test-augassign-pow [] + "NATIVE: test augassign pow" + (let [[x 2]] + (**= x 3) + (assert (= x 8)))) + +(defn test-augassign-lshift [] + "NATIVE: test augassign lshift" + (let [[x 2]] + (<<= x 2) + (assert (= x 8)))) + +(defn test-augassign-rshift [] + "NATIVE: test augassign rshift" + (let [[x 8]] + (>>= x 1) + (assert (= x 4)))) + +(defn test-augassign-bitand [] + "NATIVE: test augassign bitand" + (let [[x 8]] + (&= x 1) + (assert (= x 0)))) + +(defn test-augassign-bitor [] + "NATIVE: test augassign bitand" + (let [[x 0]] + (|= x 2) + (assert (= x 2)))) + +(defn test-augassign-bitxor [] + "NATIVE: test augassign bitand" + (let [[x 1]] + (^= x 1) + (assert (= x 0))))