Skip to content

Commit

Permalink
handle symbol reassignment
Browse files Browse the repository at this point in the history
  • Loading branch information
dakk committed Sep 27, 2023
1 parent 88ed934 commit 9160900
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 0 deletions.
18 changes: 18 additions & 0 deletions qlasskit/ast_to_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,20 @@ def translate_statement(stmt, env: Env) -> Tuple[List[Tuple[str, BoolExp]], Env]
"""Parse a statement"""
# match stmt:
if isinstance(stmt, ast.If):
# Translate test expression, body & orelse statements
# test = translate_expression(stmt.test, env)

# body = []
# for st_inner in stmt.body:
# exps, env = translate_statement(st_inner, env)
# body.extend(exps)

# orelse = []
# for st_inner in stmt.orelse:
# exps, env = translate_statement(st_inner, env)
# orelse.extend(exps)

# print(ast.dump(stmt))
raise exceptions.StatementNotHandledException(stmt)

elif isinstance(stmt, ast.Assign):
Expand All @@ -140,6 +154,10 @@ def translate_statement(stmt, env: Env) -> Tuple[List[Tuple[str, BoolExp]], Env]
)

target = Symbol(stmt.targets[0].id)

if target in env:
raise exceptions.SymbolReassingedException(target)

val = translate_expression(stmt.value, env)
env[target] = "bool" # TODO: handle all types
return [(target, val)], env
Expand Down
5 changes: 5 additions & 0 deletions qlasskit/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,8 @@ def __init__(self, message):
class ConstantReturnException(Exception):
def __init__(self, name, val):
super().__init__(f"{name} is costant = {val}")


class SymbolReassingedException(Exception):
def __init__(self, name):
super().__init__(f"{name} cannot be reassinged")
22 changes: 22 additions & 0 deletions test/test_qlassf_bool.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,25 @@ def test_assign3(self):
qf = qlassf(f, to_compile=False)
self.assertEqual(len(qf.expressions), 5)
self.assertEqual(qf.expressions[-1][1], ITE(d & e, g, h))

def test_reassign_exception(self):
f = "def test(a: bool) -> bool:\n\ta = not a\n\treturn a"
self.assertRaises(
exceptions.SymbolReassingedException,
lambda f: qlassf(f, to_compile=False),
f,
)

# def test_if(self):
# f = (
# "def test(a: bool, b: bool) -> bool:\n"
# + "\td = False\n"
# + "\tif a:\n"
# + "\t\td = not d\n"
# + "\telse:\n"
# + "\t\td = True\n"
# + "\treturn d"
# )
# qf = qlassf(f, to_compile=False)
# self.assertEqual(len(qf.expressions), 2)
# self.assertEqual(qf.expressions[-1][1], ITE(d & e, g, h))
1 change: 1 addition & 0 deletions test/test_qlassf_int.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class TestQlassfInt(unittest.TestCase):
# def test9(a: Int8) -> bool:
# return a == 42

# Raise costant return
# def test10() -> Int8:
# return 42

Expand Down

0 comments on commit 9160900

Please sign in to comment.