Add zero- and one-argument versions of 'and' and 'or' (ref. #835)
This commit is contained in:
parent
95cef09c6a
commit
e54d4becec
@ -1669,15 +1669,22 @@ class HyASTCompiler(object):
|
|||||||
|
|
||||||
@builds("and")
|
@builds("and")
|
||||||
@builds("or")
|
@builds("or")
|
||||||
@checkargs(min=2)
|
|
||||||
def compile_logical_or_and_and_operator(self, expression):
|
def compile_logical_or_and_and_operator(self, expression):
|
||||||
ops = {"and": ast.And,
|
ops = {"and": (ast.And, "True"),
|
||||||
"or" : ast.Or}
|
"or": (ast.Or, "None")}
|
||||||
operator = expression.pop(0)
|
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()
|
ret = Result()
|
||||||
values = list(map(self.compile, expression))
|
values = list(map(self.compile, expression))
|
||||||
has_stmt = any(value.stmts for value in values)
|
has_stmt = any(value.stmts for value in values)
|
||||||
root_line, root_column = operator.start_line, operator.start_column
|
|
||||||
if has_stmt:
|
if has_stmt:
|
||||||
# Compile it to an if...else sequence
|
# Compile it to an if...else sequence
|
||||||
var = self.get_anon_var()
|
var = self.get_anon_var()
|
||||||
@ -1730,7 +1737,7 @@ class HyASTCompiler(object):
|
|||||||
ret = sum(root, ret)
|
ret = sum(root, ret)
|
||||||
ret += Result(expr=expr_name, temp_variables=[expr_name, name])
|
ret += Result(expr=expr_name, temp_variables=[expr_name, name])
|
||||||
else:
|
else:
|
||||||
ret += ast.BoolOp(op=ops[operator](),
|
ret += ast.BoolOp(op=opnode(),
|
||||||
lineno=root_line,
|
lineno=root_line,
|
||||||
col_offset=root_column,
|
col_offset=root_column,
|
||||||
values=[value.force_expr for value in values])
|
values=[value.force_expr for value in values])
|
||||||
|
@ -765,9 +765,13 @@
|
|||||||
(defn test-and []
|
(defn test-and []
|
||||||
"NATIVE: test the and function"
|
"NATIVE: test the and function"
|
||||||
(let [[and123 (and 1 2 3)]
|
(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 (= and123 3))
|
||||||
(assert (= and-false False)))
|
(assert (= and-false False))
|
||||||
|
(assert (= and-true True))
|
||||||
|
(assert (= and-single 1)))
|
||||||
; short circuiting
|
; short circuiting
|
||||||
(setv a 1)
|
(setv a 1)
|
||||||
(and 0 (setv a 2))
|
(and 0 (setv a 2))
|
||||||
@ -778,10 +782,14 @@
|
|||||||
"NATIVE: test the or function"
|
"NATIVE: test the or function"
|
||||||
(let [[or-all-true (or 1 2 3 True "string")]
|
(let [[or-all-true (or 1 2 3 True "string")]
|
||||||
[or-some-true (or False "hello")]
|
[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-all-true 1))
|
||||||
(assert (= or-some-true "hello"))
|
(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
|
; short circuiting
|
||||||
(setv a 1)
|
(setv a 1)
|
||||||
(or 1 (setv a 2))
|
(or 1 (setv a 2))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user