Skip to content

Commit

Permalink
fix typechecker
Browse files Browse the repository at this point in the history
  • Loading branch information
dakk committed Jul 6, 2024
1 parent 80a11c9 commit fde33bd
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 20 deletions.
31 changes: 15 additions & 16 deletions qlasskit/ast2ast/astrewriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,28 +170,27 @@ def visit_Subscript(self, node): # noqa: C901
nname = node.value.id
iname = node.slice.id

def create_if_exp(i, max_i):
def create_if_exp_single(i, max_i):
if i == max_i:
return ast.Subscript(
value=ast.Name(id=nname, ctx=ast.Load()),
slice=ast.Constant(value=i),
ctx=ast.Load(),
)
value=ast.Name(id=nname, ctx=ast.Load()),
slice=ast.Constant(value=i),
ctx=ast.Load(),
)
else:
next_i = i + 1
return ast.IfExp(
test=ast.Compare(
left=ast.Name(id=iname, ctx=ast.Load()),
ops=[ast.Eq()],
comparators=[ast.Constant(value=i)],

left=ast.Name(id=iname, ctx=ast.Load()),
ops=[ast.Eq()],
comparators=[ast.Constant(value=i)],
),
body=ast.Subscript(
value=ast.Name(id=nname, ctx=ast.Load()),
slice=ast.Constant(value=i),
ctx=ast.Load(),
),
orelse=create_if_exp(next_i, max_i),
value=ast.Name(id=nname, ctx=ast.Load()),
slice=ast.Constant(value=i),
ctx=ast.Load(),
),
orelse=create_if_exp_single(next_i, max_i),
)

# Infer i and j sizes from env['a']
Expand All @@ -206,8 +205,8 @@ def create_if_exp(i, max_i):
max_i = len(outer_tuple.elts) - 1

# Create the IfExp structure
return create_if_exp(0, max_i)
return create_if_exp_single(0, max_i)

# Handle inner access L[i][j]
elif (
isinstance(node, ast.Subscript)
Expand Down
5 changes: 1 addition & 4 deletions test/qlassf/test_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,6 @@ def test_list_of_tuple_of_tuple2(self):
qf.bind(io_list=tt)

def test_list_var_access(self):
f = (
"def test(a: Qlist[bool, 4], i: Qint[2]) -> bool:\n"
"\treturn a[i]"
)
f = "def test(a: Qlist[bool, 4], i: Qint[2]) -> bool:\n" "\treturn a[i]"
qf = qlassf(f, to_compile=COMPILATION_ENABLED, compiler=self.compiler)
compute_and_compare_results(self, qf)

0 comments on commit fde33bd

Please sign in to comment.