diff --git a/hy/compiler.py b/hy/compiler.py index 5e847ab..dc46cd0 100644 --- a/hy/compiler.py +++ b/hy/compiler.py @@ -1669,15 +1669,22 @@ class HyASTCompiler(object): @builds("and") @builds("or") - @checkargs(min=2) def compile_logical_or_and_and_operator(self, expression): - ops = {"and": ast.And, - "or" : ast.Or} + ops = {"and": (ast.And, "True"), + "or": (ast.Or, "None")} operator = expression.pop(0) + opnode, default = ops[operator] + root_line, root_column = operator.start_line, operator.start_column + if len(expression) == 0: + return ast.Name(id=default, + ctx=ast.Load(), + lineno=root_line, + col_offset=root_column) + elif len(expression) == 1: + return self.compile(expression[0]) ret = Result() values = list(map(self.compile, expression)) has_stmt = any(value.stmts for value in values) - root_line, root_column = operator.start_line, operator.start_column if has_stmt: # Compile it to an if...else sequence var = self.get_anon_var() @@ -1730,7 +1737,7 @@ class HyASTCompiler(object): ret = sum(root, ret) ret += Result(expr=expr_name, temp_variables=[expr_name, name]) else: - ret += ast.BoolOp(op=ops[operator](), + ret += ast.BoolOp(op=opnode(), lineno=root_line, col_offset=root_column, values=[value.force_expr for value in values]) diff --git a/tests/native_tests/language.hy b/tests/native_tests/language.hy index 069c1c1..28069d9 100644 --- a/tests/native_tests/language.hy +++ b/tests/native_tests/language.hy @@ -765,9 +765,13 @@ (defn test-and [] "NATIVE: test the and function" (let [[and123 (and 1 2 3)] - [and-false (and 1 False 3)]] + [and-false (and 1 False 3)] + [and-true (and)] + [and-single (and 1)]] (assert (= and123 3)) - (assert (= and-false False))) + (assert (= and-false False)) + (assert (= and-true True)) + (assert (= and-single 1))) ; short circuiting (setv a 1) (and 0 (setv a 2)) @@ -778,10 +782,14 @@ "NATIVE: test the or function" (let [[or-all-true (or 1 2 3 True "string")] [or-some-true (or False "hello")] - [or-none-true (or False False)]] + [or-none-true (or False False)] + [or-false (or)] + [or-single (or 1)]] (assert (= or-all-true 1)) (assert (= or-some-true "hello")) - (assert (= or-none-true False))) + (assert (= or-none-true False)) + (assert (= or-false nil)) + (assert (= or-single 1))) ; short circuiting (setv a 1) (or 1 (setv a 2))