Skip to content

Commit

Permalink
allow print call inside qlassf functions
Browse files Browse the repository at this point in the history
  • Loading branch information
dakk committed Oct 12, 2023
1 parent f5336f8 commit e6e5eb1
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 3 deletions.
2 changes: 1 addition & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@
- [x] Allow constant functions
- [x] Doc: emphatize the compiler flow
- [x] Doc: properly render documentation
- [x] Builtin debug functions: print()

### Week 4: (16 Oct 23)
- [ ] Publish doc
- [ ] Fix structure and typing location
- [ ] Int arithmetic expressions (+, -, *, /)
- [ ] Function call
- [ ] Builtin functions: sum(), max(), min(), len()
- [ ] Builtin debug functions: print()

## Month 2:

Expand Down
4 changes: 4 additions & 0 deletions qlasskit/ast2logic/t_ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ def translate_ast(fun) -> LogicFun:
exps = []
for stmt in fun.body:
s_exps, env = translate_statement(stmt, env)

if s_exps == None:
continue

exps.append(s_exps)

exps_flat = flatten(exps)
Expand Down
10 changes: 10 additions & 0 deletions qlasskit/ast2logic/t_expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,16 +220,26 @@ def unfold(v_exps, op):
else:
raise exceptions.ExpressionNotHandledException(expr)

# Binop
elif isinstance(expr, ast.BinOp):
# Add | Sub | Mult | MatMult | Div | Mod | Pow | LShift | RShift
# | BitOr | BitXor | BitAnd | FloorDiv
raise exceptions.ExpressionNotHandledException(expr)

# Call
elif isinstance(expr, ast.Call):
if expr.func.id == 'print':
return (None, None)

else:
raise exceptions.ExpressionNotHandledException(expr)

# Lambda
# Dict
# Set
# Call
# List


else:
raise exceptions.ExpressionNotHandledException(expr)
4 changes: 4 additions & 0 deletions qlasskit/ast2logic/t_statement.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,9 @@ def translate_statement( # noqa: C901
# Continue
# Match

elif isinstance(stmt, ast.Expr):
texp, vexp = translate_expression(stmt.value, env)
return None, env

else:
raise exceptions.StatementNotHandledException(stmt)
9 changes: 8 additions & 1 deletion test/test_qlassf.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,14 @@
from qlasskit import Qint4, QlassF, qlassf

from . import utils

from .utils import COMPILATION_ENABLED, compute_and_compare_results

class TestQlassf(unittest.TestCase):
def test_print_call(self):
f = "def test(a: bool) -> bool:\n\tprint(a)\n\treturn a"
qf = qlassf(f, to_compile=COMPILATION_ENABLED)
self.assertEqual(len(qf.expressions), 1)
compute_and_compare_results(self, qf)

class TestQlassfDecorator(unittest.TestCase):
def test_decorator(self):
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ deps =
pytest
parameterized
commands =
pytest -rP
pytest #-rP

[testenv:flake8]
deps =
Expand Down

0 comments on commit e6e5eb1

Please sign in to comment.