From 60a9003b0c1a05919bd72872fb0ea90aa5a4cb3d Mon Sep 17 00:00:00 2001 From: Julien Danjou Date: Sun, 14 Apr 2013 17:37:18 +0200 Subject: [PATCH] Add missing bits operator and power Signed-off-by: Julien Danjou --- hy/compiler.py | 18 +++++++++++++----- hy/lex/states.py | 2 +- tests/native_tests/math.hy | 24 ++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 6 deletions(-) diff --git a/hy/compiler.py b/hy/compiler.py index 318174b..fe3fa64 100644 --- a/hy/compiler.py +++ b/hy/compiler.py @@ -716,18 +716,26 @@ class HyASTCompiler(object): @builds("/") @builds("//") @builds("*") + @builds("**") + @builds("<<") + @builds(">>") + @builds("|") + @builds("^") + @builds("&") @checkargs(min=2) def compile_maths_expression(self, expression): - # operator = Mod | Pow | LShift | RShift | BitOr | - # BitXor | BitAnd | FloorDiv - # (to implement list) XXX - ops = {"+": ast.Add, "/": ast.Div, "//": ast.FloorDiv, "*": ast.Mult, "-": ast.Sub, - "%": ast.Mod} + "%": ast.Mod, + "**": ast.Pow, + "<<": ast.LShift, + ">>": ast.RShift, + "|": ast.BitOr, + "^": ast.BitXor, + "&": ast.BitAnd} inv = expression.pop(0) op = ops[inv] diff --git a/hy/lex/states.py b/hy/lex/states.py index 7070884..d512527 100644 --- a/hy/lex/states.py +++ b/hy/lex/states.py @@ -79,7 +79,7 @@ def _resolve_atom(obj): if obj.startswith(":"): return HyKeyword(obj) - if obj.startswith("*") and obj.endswith("*") and obj != "*": + if obj.startswith("*") and obj.endswith("*") and obj not in ("*", "**"): obj = obj[1:-1].upper() if "-" in obj and obj != "-": diff --git a/tests/native_tests/math.hy b/tests/native_tests/math.hy index d2bcaea..dbaabc4 100644 --- a/tests/native_tests/math.hy +++ b/tests/native_tests/math.hy @@ -39,3 +39,27 @@ (defn test-modulo [] "NATIVE: test mod" (assert (= (% 10 2) 0))) + +(defn test-pow [] + "NATIVE: test pow" + (assert (= (** 10 2) 100))) + +(defn test-lshift [] + "NATIVE: test lshift" + (assert (= (<< 1 2) 4))) + +(defn test-rshift [] + "NATIVE: test lshift" + (assert (= (>> 8 1) 4))) + +(defn test-bitor [] + "NATIVE: test lshift" + (assert (= (| 1 2) 3))) + +(defn test-bitxor [] + "NATIVE: test xor" + (assert (= (^ 1 2) 3))) + +(defn test-bitand [] + "NATIVE: test lshift" + (assert (= (& 1 2) 0)))