Skip to content

Commit

Permalink
handle elif, closes #43
Browse files Browse the repository at this point in the history
  • Loading branch information
dakk committed Apr 23, 2024
1 parent 11ccd9b commit e4f8276
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 2 deletions.
3 changes: 3 additions & 0 deletions qlasskit/ast2ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,9 @@ def visit_If(self, node):

if target_0id[0:2] == "__" and target_0id not in self.env:
orelse_inner = ast.Name(id=target_0id[2:])
elif target_0id[0 : len("_iftarg")] == "_iftarg":
if_l.append(b)
continue
else:
orelse_inner = ast.Name(id=target_0id)

Expand Down
2 changes: 1 addition & 1 deletion qlasskit/ast2logic/t_statement.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def translate_statement( # noqa: C901
) -> Tuple[List[Tuple[str, Boolean]], Env]:
"""Parse a statement"""
# match stmt:
if isinstance(stmt, ast.If):
if isinstance(stmt, ast.If): # This is handled by ast2ast
# Translate test expression, body & orelse statements
# test = translate_expression(stmt.test, env)

Expand Down
2 changes: 2 additions & 0 deletions qlasskit/qlassfun.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,9 @@ def from_function(
original_f = eval(fun_ast.body[0].name) if isinstance(f, str) else f

def _do_translate(fun_ast, original_f):
# print(ast.dump(fun_ast, indent=4))
fun = ast2ast(fun_ast.body[0])
# print(ast.dump(fun, indent=4))
fun_name, args, fun_ret, exps = translate_ast(fun, types, defs)

exps = bool_optimizer.apply(exps)
Expand Down
13 changes: 13 additions & 0 deletions test/qlassf/test_ifthenelse.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,19 @@ def test_if(self):
qf = qlassf(f, compiler=self.compiler, to_compile=COMPILATION_ENABLED)
compute_and_compare_results(self, qf)

def test_elif(self):
f = (
"def test(a: Qint2, b: bool) -> bool:\n"
+ "\td = False\n"
+ "\tif a == 0:\n"
+ "\t\td = not b\n"
+ "\telif a == 1:\n"
+ "\t\td = b\n"
+ "\treturn d"
)
qf = qlassf(f, compiler=self.compiler, to_compile=COMPILATION_ENABLED)
compute_and_compare_results(self, qf)

def test_if_for(self):
f = (
"def test(a: bool, b: bool) -> bool:\n"
Expand Down
2 changes: 1 addition & 1 deletion test/qlassf/test_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def test_list_access_with_var_on_tuple2(self):
qf = qlassf(f, to_compile=COMPILATION_ENABLED, compiler=self.compiler)
compute_and_compare_results(self, qf)

# TODO: handle this case
# TODO: handle this case (won't fix right now #42)
# def test_list_of_tuple_of_tuple(self):
# f = (
# "def oracle(io_list: Parameter[List[Tuple[Tuple[bool, bool], bool]]],"
Expand Down

0 comments on commit e4f8276

Please sign in to comment.