Operators + and * work without args, fixes #372

Like other lisps, operators `+` and `*` return their identity values
when called with no arguments. Also with a single operand they return
the operand.

This fixes #372
This commit is contained in:
Abhishek L 2013-12-15 11:42:57 +05:30
parent c11b231c1c
commit f72ff53f41
2 changed files with 23 additions and 4 deletions

View File

@ -1343,11 +1343,9 @@ class HyASTCompiler(object):
lineno=e.start_line, lineno=e.start_line,
col_offset=e.start_column) col_offset=e.start_column)
@builds("+")
@builds("%") @builds("%")
@builds("/") @builds("/")
@builds("//") @builds("//")
@builds("*")
@builds("**") @builds("**")
@builds("<<") @builds("<<")
@builds(">>") @builds(">>")
@ -1384,6 +1382,23 @@ class HyASTCompiler(object):
col_offset=child.start_column) col_offset=child.start_column)
return ret 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("-") @builds("-")
@checkargs(min=1) @checkargs(min=1)
def compile_maths_expression_sub(self, expression): def compile_maths_expression_sub(self, expression):

View File

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