Add AugAssign handling
Signed-off-by: Julien Danjou <julien@danjou.info>
This commit is contained in:
parent
60a9003b0c
commit
c05ae5fcbc
@ -751,6 +751,45 @@ class HyASTCompiler(object):
|
|||||||
left = calc
|
left = calc
|
||||||
return 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):
|
def compile_dotted_expression(self, expr):
|
||||||
ofn = expr.pop(0) # .join
|
ofn = expr.pop(0) # .join
|
||||||
|
|
||||||
|
@ -63,3 +63,75 @@
|
|||||||
(defn test-bitand []
|
(defn test-bitand []
|
||||||
"NATIVE: test lshift"
|
"NATIVE: test lshift"
|
||||||
(assert (= (& 1 2) 0)))
|
(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))))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user