Merge branch 'fix/arith-identity' of https://github.com/theanalyst/hy into theanalyst-fix/arith-identity

This commit is contained in:
Nicolas Dandrimont 2013-12-22 19:32:10 +01:00
commit ceb612f385
2 changed files with 23 additions and 4 deletions

View File

@ -1361,11 +1361,9 @@ class HyASTCompiler(object):
lineno=e.start_line,
col_offset=e.start_column)
@builds("+")
@builds("%")
@builds("/")
@builds("//")
@builds("*")
@builds("**")
@builds("<<")
@builds(">>")
@ -1402,6 +1400,23 @@ class HyASTCompiler(object):
col_offset=child.start_column)
return ret
@builds("+")
@builds("*")
def compile_maths_expression_mul(self, expression):
if len(expression) > 2:
return self.compile_maths_expression(expression)
else:
id_op = {"+": HyInteger(0), "*": HyInteger(1)}
op = expression.pop(0)
arg = expression.pop(0) if expression else id_op[op]
expr = HyExpression([
HySymbol(op),
id_op[op],
arg
]).replace(expression)
return self.compile_maths_expression(expr)
@builds("-")
@checkargs(min=1)
def compile_maths_expression_sub(self, expression):

View File

@ -8,7 +8,9 @@
(setv test_mult (fn []
"NATIVE: Test multiplication."
(assert (= 4 (square 2)))))
(assert (= 4 (square 2)))
(assert (= 8 (* 8)))
(assert (= 1 (*)))))
(setv test_sub (fn []
@ -19,7 +21,9 @@
(setv test_add (fn []
"NATIVE: Test addition"
(assert (= 4 (+ 1 1 1 1)))))
(assert (= 4 (+ 1 1 1 1)))
(assert (= 8 (+ 8)))
(assert (= 0 (+)))))
(setv test_div (fn []