From 1b7d145c65ef16181d9dbd59bfd89d741c94ca3a Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Mon, 8 Apr 2013 00:00:20 +0200 Subject: [PATCH] Implement the (or) function too The (or) function is to be constructed similarly to (and), so refactor the compile_and_operator function to handle or aswell. Signed-off-by: Gergely Nagy --- hy/compiler.py | 7 +++++-- tests/native_tests/language.hy | 9 +++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/hy/compiler.py b/hy/compiler.py index 026461d..13e9448 100644 --- a/hy/compiler.py +++ b/hy/compiler.py @@ -547,13 +547,16 @@ class HyASTCompiler(object): col_offset=operator.start_column) @builds("and") + @builds("or") @checkargs(min=2) - def compile_and_operator(self, expression): + def compile_logical_or_and_and_operator(self, expression): + ops = {"and": ast.And, + "or": ast.Or} operator = expression.pop(0) values = [] for child in expression: values.append (self.compile(child)) - return ast.BoolOp(op=ast.And(), + return ast.BoolOp(op=ops[operator](), lineno=operator.start_line, col_offset=operator.start_column, values=values) diff --git a/tests/native_tests/language.hy b/tests/native_tests/language.hy index f533a9b..9eaf0f5 100644 --- a/tests/native_tests/language.hy +++ b/tests/native_tests/language.hy @@ -413,6 +413,15 @@ (assert (= and123 3)) (assert (= and-false False)))) +(defn test-or [] + "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)]] + (assert (= or-all-true 1)) + (assert (= or-some-true "hello")) + (assert (= or-none-true False)))) + ; FEATURE: native hy-eval ; ; - related to bug #64