Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug fix, Recursion Error #369 #404

Merged
merged 5 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions opshin/tests/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -3024,3 +3024,21 @@ def validator(_: None) -> None:
"""
res = eval_uplc(source_code, Unit())
self.assertEqual(res, uplc.PlutusConstr(0, []), "Invalid return")

@given(a=st.booleans(), b=st.booleans())
def test_cast_bool_to_int_lt(self, a: bool, b: bool):
source_code = """
def validator(a: bool, b:bool)-> int:
return 5+(a<b)
"""
res = eval_uplc_value(source_code, a, b)
self.assertEquals(res, 5 + (a < b))

@given(a=st.booleans(), b=st.booleans())
def test_cast_bool_to_int_gt(self, a: bool, b: bool):
source_code = """
def validator(a: bool, b:bool)-> int:
return 5+(a>b)
nielstron marked this conversation as resolved.
Show resolved Hide resolved
"""
res = eval_uplc_value(source_code, a, b)
self.assertEquals(res, 5 + (a > b))
nielstron marked this conversation as resolved.
Show resolved Hide resolved
24 changes: 24 additions & 0 deletions opshin/type_impls.py
Original file line number Diff line number Diff line change
Expand Up @@ -1645,6 +1645,9 @@ def _binop_return_type(self, binop: operator, other: "Type") -> "Type":
):
if other == IntegerInstanceType:
return IntegerType()
elif other == BoolInstanceType:
# cast to integer
return IntegerType()
if isinstance(binop, Mult):
if other == IntegerInstanceType:
return IntegerType()
Expand Down Expand Up @@ -1673,6 +1676,23 @@ def _binop_bin_fun(self, binop: operator, other: AST):
PowImpl(x, OVar("y")),
),
)
if other.typ == BoolInstanceType:
if isinstance(binop, Add):
return lambda x, y: OLet(
[("x", x), ("y", y)],
plt.Ite(
OVar("y"), plt.AddInteger(OVar("x"), plt.Integer(1)), OVar("x")
),
)
elif isinstance(binop, Sub):
return lambda x, y: OLet(
[("x", x), ("y", y)],
plt.Ite(
OVar("y"),
plt.SubtractInteger(OVar("x"), plt.Integer(1)),
OVar("x"),
),
)

if isinstance(binop, Mult):
if other.typ == IntegerInstanceType:
Expand Down Expand Up @@ -2308,6 +2328,10 @@ def cmp(self, op: cmpop, o: "Type") -> plt.AST:
return OLambda(["x", "y"], plt.Iff(OVar("x"), OVar("y")))
if isinstance(op, NotEq):
return OLambda(["x", "y"], plt.Not(plt.Iff(OVar("x"), OVar("y"))))
if isinstance(op, Lt):
return OLambda(["x", "y"], plt.And(plt.Not(OVar("x")), OVar("y")))
if isinstance(op, Gt):
return OLambda(["x", "y"], plt.And(OVar("x"), plt.Not(OVar("y"))))
return super().cmp(op, o)

def stringify(self, recursive: bool = False) -> plt.AST:
Expand Down
Loading