diff --git a/.travis.yml b/.travis.yml index fa8eb9b..09f6974 100644 --- a/.travis.yml +++ b/.travis.yml @@ -30,9 +30,17 @@ script: - > coverage run -a --source=hebi -m hebi build examples/smart_contracts/assert_sum.py - > - for i in $(find examples -type f -name "*.py" -not \( -name "broken*" -o -name "extract*" \)); do - coverage run -a --source=hebi -m hebi compile "$i" + for i in $(find examples -type f -name "*.py" -not \( -name "broken*" -o -name "extract*" -o -name "__*" \)); do + echo "$i" + coverage run -a --source=hebi -m hebi compile "$i" > /dev/null done +# smart contracts with special parameters +- > + coverage run -a --source=hebi -m hebi build examples/smart_contracts/parameterized.py '{"int": 42}' +- > + coverage run -a --source=hebi -m hebi build examples/smart_contracts/dual_use.py --force-three-params +- > + coverage run -a --source=hebi -m hebi build examples/smart_contracts/wrapped_token.py '{"bytes": "ae810731b5d21c0d182d89c60a1eff7095dffd1c0dce8707a8611099"}' '{"bytes": "4d494c4b"}' '{"int": 1000000}' --force-three-params after_success: - coverage report diff --git a/README.md b/README.md index b7035e6..ad6babb 100644 --- a/README.md +++ b/README.md @@ -202,7 +202,7 @@ python3 -m hebi compile_pluto examples/smart_contracts/assert_sum.py You can sponsor the development of hebi through GitHub or [Teiki](https://alpha.teiki.network/projects/opshin) or just by sending ADA. Drop me a message on social media and let me know what it is for. -- **[Teiki](https://alpha.teiki.network/projects/opshin)** Stake your ada to support OpShin at [Teiki](https://alpha.teiki.network/projects/opshin) +- **[Kreate](https://beta.kreate.community/projects/opshin)** Stake your ada to support OpShin at [Kreate](https://beta.kreate.community/projects/opshin) - **GitHub** Sponsor the developers of this project through the button "Sponsor" next to them - **ADA** Donation in ADA can be submitted to `$opshin` or `addr1qyz3vgd5xxevjy2rvqevz9n7n7dney8n6hqggp23479fm6vwpj9clsvsf85cd4xc59zjztr5zwpummwckmzr2myjwjns74lhmr`. diff --git a/docs/hebi/compiler.html b/docs/hebi/compiler.html index 3d3e45d..f7687a1 100644 --- a/docs/hebi/compiler.html +++ b/docs/hebi/compiler.html @@ -88,7 +88,6 @@

Module hebi.compiler

from .rewrite.rewrite_zero_ary import RewriteZeroAry from .optimize.optimize_remove_pass import OptimizeRemovePass from .optimize.optimize_remove_deadvars import OptimizeRemoveDeadvars -from .optimize.optimize_varlen import OptimizeVarlen from .type_inference import * from .util import CompilingNodeTransformer, PowImpl from .typed_ast import transform_ext_params_map, transform_output_map, RawPlutoExpr @@ -258,10 +257,7 @@

Module hebi.compiler

# TODO can use more sophisiticated procedure here i.e. functions marked by comment main_fun: typing.Optional[InstanceType] = None for s in node.body: - if ( - isinstance(s, FunctionDef) - and s.orig_name == self.validator_function_name - ): + if isinstance(s, FunctionDef) and s.name == self.validator_function_name: main_fun = s assert ( main_fun is not None @@ -273,9 +269,9 @@

Module hebi.compiler

# check if this is a contract written to double function enable_double_func_mint_spend = False - if len(main_fun_typ.typ.argtyps) >= 3 and self.force_three_params: + if len(main_fun_typ.argtyps) >= 3 and self.force_three_params: # check if is possible - second_last_arg = main_fun_typ.typ.argtyps[-2] + second_last_arg = main_fun_typ.argtyps[-2] assert isinstance( second_last_arg, InstanceType ), "Can not pass Class into validator" @@ -300,20 +296,24 @@

Module hebi.compiler

body = node.body + [ TypedReturn( value=Name( - id=self.validator_function_name, typ=main_fun_typ, ctx=Load() + id=self.validator_function_name, + typ=InstanceType(main_fun_typ), + ctx=Load(), ), - typ=main_fun_typ.typ.rettyp, + typ=InstanceType(main_fun_typ), ) ] validator = plt.Lambda( - [f"p{i}" for i, _ in enumerate(main_fun_typ.typ.argtyps)], - transform_output_map(main_fun_typ.typ.rettyp)( + [f"p{i}" for i, _ in enumerate(main_fun_typ.argtyps)], + transform_output_map(main_fun_typ.rettyp)( plt.Let( [ ( "val", - self.visit_sequence(body)(plt.Unit()), + self.visit_sequence(body)( + plt.ConstrData(plt.Integer(0), plt.EmptyDataList()) + ), ), ], plt.Apply( @@ -321,7 +321,7 @@

Module hebi.compiler

plt.Var("val"), *[ transform_ext_params_map(a)(plt.Var(f"p{i}")) - for i, a in enumerate(main_fun_typ.typ.argtyps) + for i, a in enumerate(main_fun_typ.argtyps) ], ), ), @@ -329,7 +329,7 @@

Module hebi.compiler

) if enable_double_func_mint_spend: validator = wrap_validator_double_function( - validator, pass_through=len(main_fun_typ.typ.argtyps) - 3 + validator, pass_through=len(main_fun_typ.argtyps) - 3 ) elif self.force_three_params: # Error if the double function is enforced but not possible @@ -431,7 +431,11 @@

Module hebi.compiler

) -> typing.Callable[[plt.AST], plt.AST]: body = node.body.copy() # defaults to returning None if there is no return statement - compiled_body = self.visit_sequence(body)(plt.Unit()) + if node.typ.typ.rettyp.typ == AnyType(): + ret_val = plt.ConstrData(plt.Integer(0), plt.EmptyDataList()) + else: + ret_val = plt.Unit() + compiled_body = self.visit_sequence(body)(ret_val) return lambda x: plt.Let( [ ( @@ -455,7 +459,7 @@

Module hebi.compiler

def visit_Return(self, node: TypedReturn) -> typing.Callable[[plt.AST], plt.AST]: # Throw away the term we were passed, this is going to be the last! compiled_return = self.visit(node.value) - if isinstance(node.typ.typ.rettyp.typ, AnyType): + if isinstance(node.typ.typ, AnyType): # if the function returns generic data, wrap the function return value compiled_return = transform_output_map(node.value.typ)(compiled_return) return lambda _: compiled_return @@ -992,10 +996,7 @@

Classes

# TODO can use more sophisiticated procedure here i.e. functions marked by comment main_fun: typing.Optional[InstanceType] = None for s in node.body: - if ( - isinstance(s, FunctionDef) - and s.orig_name == self.validator_function_name - ): + if isinstance(s, FunctionDef) and s.name == self.validator_function_name: main_fun = s assert ( main_fun is not None @@ -1007,9 +1008,9 @@

Classes

# check if this is a contract written to double function enable_double_func_mint_spend = False - if len(main_fun_typ.typ.argtyps) >= 3 and self.force_three_params: + if len(main_fun_typ.argtyps) >= 3 and self.force_three_params: # check if is possible - second_last_arg = main_fun_typ.typ.argtyps[-2] + second_last_arg = main_fun_typ.argtyps[-2] assert isinstance( second_last_arg, InstanceType ), "Can not pass Class into validator" @@ -1034,20 +1035,24 @@

Classes

body = node.body + [ TypedReturn( value=Name( - id=self.validator_function_name, typ=main_fun_typ, ctx=Load() + id=self.validator_function_name, + typ=InstanceType(main_fun_typ), + ctx=Load(), ), - typ=main_fun_typ.typ.rettyp, + typ=InstanceType(main_fun_typ), ) ] validator = plt.Lambda( - [f"p{i}" for i, _ in enumerate(main_fun_typ.typ.argtyps)], - transform_output_map(main_fun_typ.typ.rettyp)( + [f"p{i}" for i, _ in enumerate(main_fun_typ.argtyps)], + transform_output_map(main_fun_typ.rettyp)( plt.Let( [ ( "val", - self.visit_sequence(body)(plt.Unit()), + self.visit_sequence(body)( + plt.ConstrData(plt.Integer(0), plt.EmptyDataList()) + ), ), ], plt.Apply( @@ -1055,7 +1060,7 @@

Classes

plt.Var("val"), *[ transform_ext_params_map(a)(plt.Var(f"p{i}")) - for i, a in enumerate(main_fun_typ.typ.argtyps) + for i, a in enumerate(main_fun_typ.argtyps) ], ), ), @@ -1063,7 +1068,7 @@

Classes

) if enable_double_func_mint_spend: validator = wrap_validator_double_function( - validator, pass_through=len(main_fun_typ.typ.argtyps) - 3 + validator, pass_through=len(main_fun_typ.argtyps) - 3 ) elif self.force_three_params: # Error if the double function is enforced but not possible @@ -1165,7 +1170,11 @@

Classes

) -> typing.Callable[[plt.AST], plt.AST]: body = node.body.copy() # defaults to returning None if there is no return statement - compiled_body = self.visit_sequence(body)(plt.Unit()) + if node.typ.typ.rettyp.typ == AnyType(): + ret_val = plt.ConstrData(plt.Integer(0), plt.EmptyDataList()) + else: + ret_val = plt.Unit() + compiled_body = self.visit_sequence(body)(ret_val) return lambda x: plt.Let( [ ( @@ -1189,7 +1198,7 @@

Classes

def visit_Return(self, node: TypedReturn) -> typing.Callable[[plt.AST], plt.AST]: # Throw away the term we were passed, this is going to be the last! compiled_return = self.visit(node.value) - if isinstance(node.typ.typ.rettyp.typ, AnyType): + if isinstance(node.typ.typ, AnyType): # if the function returns generic data, wrap the function return value compiled_return = transform_output_map(node.value.typ)(compiled_return) return lambda _: compiled_return @@ -1812,7 +1821,11 @@

Methods

) -> typing.Callable[[plt.AST], plt.AST]: body = node.body.copy() # defaults to returning None if there is no return statement - compiled_body = self.visit_sequence(body)(plt.Unit()) + if node.typ.typ.rettyp.typ == AnyType(): + ret_val = plt.ConstrData(plt.Integer(0), plt.EmptyDataList()) + else: + ret_val = plt.Unit() + compiled_body = self.visit_sequence(body)(ret_val) return lambda x: plt.Let( [ ( @@ -1941,10 +1954,7 @@

Methods

# TODO can use more sophisiticated procedure here i.e. functions marked by comment main_fun: typing.Optional[InstanceType] = None for s in node.body: - if ( - isinstance(s, FunctionDef) - and s.orig_name == self.validator_function_name - ): + if isinstance(s, FunctionDef) and s.name == self.validator_function_name: main_fun = s assert ( main_fun is not None @@ -1956,9 +1966,9 @@

Methods

# check if this is a contract written to double function enable_double_func_mint_spend = False - if len(main_fun_typ.typ.argtyps) >= 3 and self.force_three_params: + if len(main_fun_typ.argtyps) >= 3 and self.force_three_params: # check if is possible - second_last_arg = main_fun_typ.typ.argtyps[-2] + second_last_arg = main_fun_typ.argtyps[-2] assert isinstance( second_last_arg, InstanceType ), "Can not pass Class into validator" @@ -1983,20 +1993,24 @@

Methods

body = node.body + [ TypedReturn( value=Name( - id=self.validator_function_name, typ=main_fun_typ, ctx=Load() + id=self.validator_function_name, + typ=InstanceType(main_fun_typ), + ctx=Load(), ), - typ=main_fun_typ.typ.rettyp, + typ=InstanceType(main_fun_typ), ) ] validator = plt.Lambda( - [f"p{i}" for i, _ in enumerate(main_fun_typ.typ.argtyps)], - transform_output_map(main_fun_typ.typ.rettyp)( + [f"p{i}" for i, _ in enumerate(main_fun_typ.argtyps)], + transform_output_map(main_fun_typ.rettyp)( plt.Let( [ ( "val", - self.visit_sequence(body)(plt.Unit()), + self.visit_sequence(body)( + plt.ConstrData(plt.Integer(0), plt.EmptyDataList()) + ), ), ], plt.Apply( @@ -2004,7 +2018,7 @@

Methods

plt.Var("val"), *[ transform_ext_params_map(a)(plt.Var(f"p{i}")) - for i, a in enumerate(main_fun_typ.typ.argtyps) + for i, a in enumerate(main_fun_typ.argtyps) ], ), ), @@ -2012,7 +2026,7 @@

Methods

) if enable_double_func_mint_spend: validator = wrap_validator_double_function( - validator, pass_through=len(main_fun_typ.typ.argtyps) - 3 + validator, pass_through=len(main_fun_typ.argtyps) - 3 ) elif self.force_three_params: # Error if the double function is enforced but not possible @@ -2093,7 +2107,7 @@

Methods

def visit_Return(self, node: TypedReturn) -> typing.Callable[[plt.AST], plt.AST]:
     # Throw away the term we were passed, this is going to be the last!
     compiled_return = self.visit(node.value)
-    if isinstance(node.typ.typ.rettyp.typ, AnyType):
+    if isinstance(node.typ.typ, AnyType):
         # if the function returns generic data, wrap the function return value
         compiled_return = transform_output_map(node.value.typ)(compiled_return)
     return lambda _: compiled_return
diff --git a/docs/hebi/index.html b/docs/hebi/index.html index 1534f54..babdde1 100644 --- a/docs/hebi/index.html +++ b/docs/hebi/index.html @@ -213,7 +213,7 @@

Debugging artefacts

Sponsoring

You can sponsor the development of hebi through GitHub or Teiki or just by sending ADA. Drop me a message on social media and let me know what it is for.

@@ -241,18 +241,18 @@

Supporters

import warnings -try: - from .compiler import * - from .builder import * -except ImportError as e: - warnings.warn(ImportWarning(e)) - -__version__ = "0.1.1.0.12.3" +__version__ = "0.1.1.0.12.4" __author__ = "nielstron" __author_email__ = "n.muendler@web.de" __copyright__ = "Copyright (C) 2023 nielstron" __license__ = "MIT" -__url__ = "https://github.com/imperatorlang/hebi" +__url__ = "https://github.com/imperatorlang/hebi" + +try: + from .compiler import * + from .builder import * +except ImportError as e: + warnings.warn(ImportWarning(e))
diff --git a/docs/hebi/std/fractions.html b/docs/hebi/std/fractions.html index 2bbc545..46e2da7 100644 --- a/docs/hebi/std/fractions.html +++ b/docs/hebi/std/fractions.html @@ -81,6 +81,8 @@

Module hebi.std.fractions

from dataclasses import dataclass from pycardano import PlutusData +from hebi.std.math import * + @dataclass(unsafe_hash=True) class Fraction(PlutusData): diff --git a/docs/hebi/tests/test_misc.html b/docs/hebi/tests/test_misc.html index 4c8e095..068c4c6 100644 --- a/docs/hebi/tests/test_misc.html +++ b/docs/hebi/tests/test_misc.html @@ -100,7 +100,7 @@

Module hebi.tests.test_misc

for d in [uplc.PlutusInteger(20), uplc.PlutusInteger(22), uplc.BuiltinUnit()]: f = uplc.Apply(f, d) ret = uplc_eval(f) - self.assertEqual(ret, uplc.BuiltinUnit()) + self.assertEqual(ret, uplc.PlutusConstr(0, [])) def test_assert_sum_contract_fail(self): input_file = "examples/smart_contracts/assert_sum.py" @@ -265,7 +265,7 @@

Module hebi.tests.test_misc

]: f = uplc.Apply(f, d) ret = uplc_eval(f) - self.assertEqual(ret, uplc.BuiltinUnit()) + self.assertEqual(ret, uplc.PlutusConstr(0, [])) def test_gift_contract_fail(self): input_file = "examples/smart_contracts/gift.py" @@ -843,7 +843,7 @@

Module hebi.tests.test_misc

def test_return_anything(self): source_code = """ -from opshin.prelude import * +from hebi.prelude import * def validator() -> Anything: return b"" @@ -855,7 +855,7 @@

Module hebi.tests.test_misc

def test_no_return_annotation(self): source_code = """ -from opshin.prelude import * +from hebi.prelude import * def validator(): return b"" @@ -867,7 +867,7 @@

Module hebi.tests.test_misc

def test_no_parameter_annotation(self): source_code = """ -from opshin.prelude import * +from hebi.prelude import * def validator(a) -> bytes: b: bytes = a @@ -878,34 +878,6 @@

Module hebi.tests.test_misc

res = uplc_eval(uplc.Apply(code, uplc.PlutusByteString(b""))) self.assertEqual(res, uplc.PlutusByteString(b"")) - @given(xs=st.dictionaries(st.integers(), st.binary())) - def test_dict_items_values_deconstr(self, xs): - # asserts that deconstruction of parameters works for for loops too - source_code = """ -def validator(xs: Dict[int, bytes]) -> bytes: - sum_values = b"" - for _, x in xs.items(): - sum_values += x - return sum_values -""" - ast = compiler.parse(source_code) - code = compiler.compile(ast) - code = code.compile() - f = code.term - # UPLC lambdas may only take one argument at a time, so we evaluate by repeatedly applying - for d in [ - uplc.PlutusMap( - {uplc.PlutusInteger(k): uplc.PlutusByteString(v) for k, v in xs.items()} - ) - ]: - f = uplc.Apply(f, d) - ret = uplc_eval(f).value - self.assertEqual( - ret, - b"".join(xs.values()), - "for loop deconstruction did not behave as expected", - ) - def test_nested_deconstruction(self): source_code = """ def validator(xs) -> int: @@ -925,48 +897,34 @@

Module hebi.tests.test_misc

"for loop deconstruction did not behave as expected", ) - @given( - xs=st.dictionaries( - st.binary(), - st.dictionaries(st.binary(), st.integers(), max_size=3), - max_size=5, - ) - ) - def test_dict_items_values_deconstr(self, xs): - # nested deconstruction with a Value-like object - source_code = """ -def validator(xs: Dict[bytes, Dict[bytes, int]]) -> int: - sum_values = 0 - for pid, tk_dict in xs.items(): - for tk_name, tk_amount in tk_dict.items(): - sum_values += tk_amount - return sum_values + def test_different_return_types_anything(self): + source_code = """ +from hebi.prelude import * + +def validator(a: int) -> Anything: + if a > 0: + return b"" + else: + return 0 """ ast = compiler.parse(source_code) - code = compiler.compile(ast) - code = code.compile() - f = code.term - # UPLC lambdas may only take one argument at a time, so we evaluate by repeatedly applying - for d in [ - uplc.PlutusMap( - { - uplc.PlutusByteString(k): uplc.PlutusMap( - { - uplc.PlutusByteString(k2): uplc.PlutusInteger(v2) - for k2, v2 in v.items() - } - ) - for k, v in xs.items() - } - ) - ]: - f = uplc.Apply(f, d) - ret = uplc_eval(f).value - self.assertEqual( - ret, - sum(v for pid, d in xs.items() for nam, v in d.items()), - "for loop deconstruction did not behave as expected", - ) + code = compiler.compile(ast).compile() + res = uplc_eval(uplc.Apply(code, uplc.PlutusInteger(1))) + self.assertEqual(res, uplc.PlutusByteString(b"")) + res = uplc_eval(uplc.Apply(code, uplc.PlutusInteger(-1))) + self.assertEqual(res, uplc.PlutusInteger(0)) + + def test_no_return_annotation_no_return(self): + source_code = """ +from hebi.prelude import * + +def validator(a): + pass +""" + ast = compiler.parse(source_code) + code = compiler.compile(ast).compile() + res = uplc_eval(uplc.Apply(code, uplc.PlutusConstr(0, []))) + self.assertEqual(res, uplc.PlutusConstr(0, []))
@@ -1047,7 +1005,7 @@

Classes

for d in [uplc.PlutusInteger(20), uplc.PlutusInteger(22), uplc.BuiltinUnit()]: f = uplc.Apply(f, d) ret = uplc_eval(f) - self.assertEqual(ret, uplc.BuiltinUnit()) + self.assertEqual(ret, uplc.PlutusConstr(0, [])) def test_assert_sum_contract_fail(self): input_file = "examples/smart_contracts/assert_sum.py" @@ -1212,7 +1170,7 @@

Classes

]: f = uplc.Apply(f, d) ret = uplc_eval(f) - self.assertEqual(ret, uplc.BuiltinUnit()) + self.assertEqual(ret, uplc.PlutusConstr(0, [])) def test_gift_contract_fail(self): input_file = "examples/smart_contracts/gift.py" @@ -1790,7 +1748,7 @@

Classes

def test_return_anything(self): source_code = """ -from opshin.prelude import * +from hebi.prelude import * def validator() -> Anything: return b"" @@ -1802,7 +1760,7 @@

Classes

def test_no_return_annotation(self): source_code = """ -from opshin.prelude import * +from hebi.prelude import * def validator(): return b"" @@ -1814,7 +1772,7 @@

Classes

def test_no_parameter_annotation(self): source_code = """ -from opshin.prelude import * +from hebi.prelude import * def validator(a) -> bytes: b: bytes = a @@ -1825,34 +1783,6 @@

Classes

res = uplc_eval(uplc.Apply(code, uplc.PlutusByteString(b""))) self.assertEqual(res, uplc.PlutusByteString(b"")) - @given(xs=st.dictionaries(st.integers(), st.binary())) - def test_dict_items_values_deconstr(self, xs): - # asserts that deconstruction of parameters works for for loops too - source_code = """ -def validator(xs: Dict[int, bytes]) -> bytes: - sum_values = b"" - for _, x in xs.items(): - sum_values += x - return sum_values -""" - ast = compiler.parse(source_code) - code = compiler.compile(ast) - code = code.compile() - f = code.term - # UPLC lambdas may only take one argument at a time, so we evaluate by repeatedly applying - for d in [ - uplc.PlutusMap( - {uplc.PlutusInteger(k): uplc.PlutusByteString(v) for k, v in xs.items()} - ) - ]: - f = uplc.Apply(f, d) - ret = uplc_eval(f).value - self.assertEqual( - ret, - b"".join(xs.values()), - "for loop deconstruction did not behave as expected", - ) - def test_nested_deconstruction(self): source_code = """ def validator(xs) -> int: @@ -1872,48 +1802,34 @@

Classes

"for loop deconstruction did not behave as expected", ) - @given( - xs=st.dictionaries( - st.binary(), - st.dictionaries(st.binary(), st.integers(), max_size=3), - max_size=5, - ) - ) - def test_dict_items_values_deconstr(self, xs): - # nested deconstruction with a Value-like object - source_code = """ -def validator(xs: Dict[bytes, Dict[bytes, int]]) -> int: - sum_values = 0 - for pid, tk_dict in xs.items(): - for tk_name, tk_amount in tk_dict.items(): - sum_values += tk_amount - return sum_values + def test_different_return_types_anything(self): + source_code = """ +from hebi.prelude import * + +def validator(a: int) -> Anything: + if a > 0: + return b"" + else: + return 0 """ ast = compiler.parse(source_code) - code = compiler.compile(ast) - code = code.compile() - f = code.term - # UPLC lambdas may only take one argument at a time, so we evaluate by repeatedly applying - for d in [ - uplc.PlutusMap( - { - uplc.PlutusByteString(k): uplc.PlutusMap( - { - uplc.PlutusByteString(k2): uplc.PlutusInteger(v2) - for k2, v2 in v.items() - } - ) - for k, v in xs.items() - } - ) - ]: - f = uplc.Apply(f, d) - ret = uplc_eval(f).value - self.assertEqual( - ret, - sum(v for pid, d in xs.items() for nam, v in d.items()), - "for loop deconstruction did not behave as expected", - ) + code = compiler.compile(ast).compile() + res = uplc_eval(uplc.Apply(code, uplc.PlutusInteger(1))) + self.assertEqual(res, uplc.PlutusByteString(b"")) + res = uplc_eval(uplc.Apply(code, uplc.PlutusInteger(-1))) + self.assertEqual(res, uplc.PlutusInteger(0)) + + def test_no_return_annotation_no_return(self): + source_code = """ +from hebi.prelude import * + +def validator(a): + pass +""" + ast = compiler.parse(source_code) + code = compiler.compile(ast).compile() + res = uplc_eval(uplc.Apply(code, uplc.PlutusConstr(0, []))) + self.assertEqual(res, uplc.PlutusConstr(0, []))

Ancestors

Class variables

+
var current_ret_type
+
+
+
var scopes
@@ -1928,11 +1934,13 @@

Methods

def visit_FunctionDef(self, node: FunctionDef) -> TypedFunctionDef:
     tfd = copy(node)
     assert not node.decorator_list, "Functions may not have decorators"
+    rettyp = InstanceType(self.type_from_annotation(tfd.returns))
     self.enter_scope()
+    self.current_ret_type.append(rettyp)
     tfd.args = self.visit(node.args)
     functyp = FunctionType(
         [t.typ for t in tfd.args.args],
-        InstanceType(self.type_from_annotation(tfd.returns)),
+        rettyp,
     )
     tfd.typ = InstanceType(functyp)
     # We need the function type inside for recursion
@@ -1945,13 +1953,14 @@ 

Methods

# Check that return type and annotated return type match if not rets: assert ( - functyp.rettyp == NoneInstanceType + functyp.rettyp >= NoneInstanceType ), f"Function '{node.name}' has no return statement but is supposed to return not-None value" else: assert all( functyp.rettyp >= r.typ for r in rets ), f"Function '{node.name}' annotated return type does not match actual return type" self.exit_scope() + self.current_ret_type.pop(-1) # We need the function type outside for usage self.set_variable_type(node.name, tfd.typ) return tfd
@@ -2158,7 +2167,9 @@

Methods

def visit_Return(self, node: Return) -> TypedReturn:
     tp = copy(node)
     tp.value = self.visit(node.value)
-    tp.typ = tp.value.typ
+    tp.typ = (
+        tp.value.typ if not self.current_ret_type else self.current_ret_type[-1]
+    )
     return tp
@@ -2650,45 +2661,6 @@

Methods

-
-class ReturnExtractor -
-
-

Utility to find all Return statements in an AST subtree

-
- -Expand source code - -
class ReturnExtractor(NodeVisitor):
-    """Utility to find all Return statements in an AST subtree"""
-
-    def __init__(self):
-        self.returns = []
-
-    def visit_Return(self, node: Return) -> None:
-        self.returns.append(node)
-
-

Ancestors

-
    -
  • ast.NodeVisitor
  • -
-

Methods

-
-
-def visit_Return(self, node: _ast.Return) ‑> None -
-
-
-
- -Expand source code - -
def visit_Return(self, node: Return) -> None:
-    self.returns.append(node)
-
-
-
-
@@ -2766,6 +2738,7 @@

Index

  • AggressiveTypeInferencer

  • -
  • -

    ReturnExtractor

    - -
  • diff --git a/docs/hebi/typed_ast.html b/docs/hebi/typed_ast.html index 9f0a9da..850be2e 100644 --- a/docs/hebi/typed_ast.html +++ b/docs/hebi/typed_ast.html @@ -1177,7 +1177,9 @@

    Module hebi.typed_ast

    StringInstanceType: lambda x: plt.BData(plt.EncodeUtf8(x)), IntegerInstanceType: lambda x: plt.IData(x), ByteStringInstanceType: lambda x: plt.BData(x), - UnitInstanceType: lambda x: plt.Apply(plt.Lambda(["_"], plt.Unit()), x), + UnitInstanceType: lambda x: plt.Apply( + plt.Lambda(["_"], plt.ConstrData(plt.Integer(0), plt.EmptyDataList())), x + ), BoolInstanceType: lambda x: plt.IData( plt.IfThenElse(x, plt.Integer(1), plt.Integer(0)) ), @@ -5076,6 +5078,7 @@

    Ancestors

    Subclasses

    Methods

    diff --git a/docs/hebi/util.html b/docs/hebi/util.html index ff39b93..b50a83c 100644 --- a/docs/hebi/util.html +++ b/docs/hebi/util.html @@ -627,7 +627,17 @@

    Module hebi.util

    def datum_to_json(d: pycardano.Datum) -> str: - return pycardano.PlutusData.to_json(d)
    + return pycardano.PlutusData.to_json(d) + + +class ReturnExtractor(TypedNodeVisitor): + """Utility to find all Return statements in an AST subtree""" + + def __init__(self): + self.returns = [] + + def visit_Return(self, node: Return) -> None: + self.returns.append(node)
    @@ -1420,6 +1430,56 @@

    Class variables

    +
    +class ReturnExtractor +
    +
    +

    Utility to find all Return statements in an AST subtree

    +
    + +Expand source code + +
    class ReturnExtractor(TypedNodeVisitor):
    +    """Utility to find all Return statements in an AST subtree"""
    +
    +    def __init__(self):
    +        self.returns = []
    +
    +    def visit_Return(self, node: Return) -> None:
    +        self.returns.append(node)
    +
    +

    Ancestors

    + +

    Methods

    +
    +
    +def visit(self, node) +
    +
    +

    +Inherited from: +TypedNodeVisitor.visit +

    +

    Visit a node.

    +
    +
    +def visit_Return(self, node: _ast.Return) ‑> None +
    +
    +
    +
    + +Expand source code + +
    def visit_Return(self, node: Return) -> None:
    +    self.returns.append(node)
    +
    +
    +
    +
    class ReversedImpl
    @@ -1629,6 +1689,13 @@

    Pyt
  • +

    ReturnExtractor

    + +
  • +
  • ReversedImpl

    • impl_from_args
    • diff --git a/docs/index.js b/docs/index.js index 8f50089..bd76364 100644 --- a/docs/index.js +++ b/docs/index.js @@ -47,7 +47,7 @@ INDEX=[ { "ref":"hebi", "url":0, -"doc":" hebi > You are building what you want. Why not also build how you want? This is an implementation of smart contracts for Cardano which are written in a very strict subset of valid Python. The general philosophy of this project is to write a compiler that ensure the following: If the program compiles then: 1. it is a valid Python program 2. the output running it with python is the same as running it on-chain. > Note this is the sister project of [eopsin](https: github.com/ImperatorLang/eopsin). It uses an even more restricted subset of python (for example no while loops). The benefit is that the resulting code is greatly reduced in size and cpu/memory consumption. Why hebi? - 100% valid Python. Leverage the existing tool stack for Python, syntax highlighting, linting, debugging, unit-testing, [property-based testing](https: hypothesis.readthedocs.io/), [verification](https: github.com/marcoeilers/nagini) - Intuitive. Just like Python. - Functional. Forces you to write elegant, functional code in Python. - Efficient & Secure. Static type inference ensures strict typing and optimized code Eopsin is more comfortable to use than hebi. If you want to start building, write your contract in eopsin first. Then, after everything works to your pleasing, try to port to hebi and enjoy the performance gains. Getting Started OpShin Pioneer Program Check out the [opshin-pioneer-program]( https: github.com/OpShin/opshin-pioneer-program) for a host of educational example contracts, test cases and off-chain code. Example repository Check out the [opshin-starter-kit]( https: github.com/OpShin/opshin-starter-kit) repository for a quick start in setting up a development environment and compiling some sample contracts yourself. You can replace the contracts in your local copy of the repository with code from the examples section here to start exploring different contracts. Developer Community and Questions The eopsin repository contains a discussions page. Feel free to open up a new discussion with questions regarding development using hebi and using certain features. Others may be able to help you and will also benefit from the previously shared questions. Check out the community [here](https: github.com/OpShin/opshin/discussions) You can also chat with other developers [in the welcoming discord community](https: discord.gg/umR3A2g4uw) of OpShin Installation Install Python 3.8, 3.9 or 3.10. Then run python3 -m pip install hebi Writing a Smart Contract A short non-complete introduction in starting to write smart contracts follows. 1. Make sure you understand EUTxOs, Addresses, Validators etc on Cardano. [There is a wonderful crashcourse by @KtorZ](https: aiken-lang.org/fundamentals/eutxo). The contract will work on these concepts 2. Make sure you understand python. hebi works like python and uses python. There are tons of tutorials for python, choose what suits you best. 3. Make sure your contract is valid python and the types check out. Write simple contracts first and run them using hebi eval to get a feeling for how they work. 4. Make sure your contract is valid hebi code. Run hebi compile and look at the compiler erros for guidance along what works and doesn't work and why. 5. Dig into the [ examples ](https: github.com/OpShin/hebi/tree/main/examples) to understand common patterns. Check out the [ prelude ](https: hebi.opshin.dev/hebi/prelude.html) for understanding how the Script Context is structured and how complex datums are defined. 6. Check out the [sample repository](https: github.com/OpShin/opshin-starter-kit) to find a sample setup for developing your own contract. In summary, a smart contract in hebi is defined by the function validator in your contract file. The function validates that a specific value can be spent, minted, burned, withdrawn etc, depending on where it is invoked/used as a credential. If the function fails (i.e. raises an error of any kind such as a KeyError or AssertionError ) the validation is denied, and the funds can not be spent, minted, burned etc. > There is a subtle difference here in comparison to most other Smart Contract languages. > In hebi a validator may return anything (in particular also False ) - as long as it does not fail, the execution is considered valid. > This is more similar to how contracts in Solidity always pass, unless they run out of gas or hit an error. > So make sure to assert what you want to ensure to hold for validation! A simple contract called the \"Gift Contract\" verifies that only specific wallets can withdraw money. They are authenticated by a signature. If you don't understand what a pubkeyhash is and how this validates anything, check out [this gentle introduction into Cardanos EUTxO](https: aiken-lang.org/fundamentals/eutxo). Also see the [tutorial by pycardano ](https: pycardano.readthedocs.io/en/latest/guides/plutus.html) for explanations on what each of the parameters to the validator means and how to build transactions with the contract. from hebi.prelude import @dataclass class WithdrawDatum(PlutusData): pubkeyhash: bytes def validator(datum: WithdrawDatum, redeemer: None, context: ScriptContext) -> None: assert datum.pubkeyhash in context.tx_info.signatories, \"Required signature missing\" All contracts written in hebi are 100% valid python. Minting policies expect only a redeemer and script context as argument. Check out the [Architecture guide](https: github.com/OpShin/hebi/blob/main/ARCHITECTURE.md minting-policy -spending-validator-double-function) for details on how to write double functioning contracts. The [ examples ](https: github.com/OpShin/hebi/blob/main/examples) folder contains more examples. Also check out the [opshin-pioneer-program]( https: github.com/OpShin/opshin-pioneer-program) and [opshin-starter-kit]( https: github.com/OpShin/opshin-starter-kit) repo. Compiling Write your program in python. You may start with the content of examples . Arguments to scripts are passed in as Plutus Data objects in JSON notation. You can run any of the following commands Evaluate script in Python - this can be used to make sure there are no obvious errors hebi eval examples/smart_contracts/assert_sum.py \"{\\\"int\\\": 4}\" \"{\\\"int\\\": 38}\" \"{\\\"constructor\\\": 0, \\\"fields\\\": []}\" Compile script to 'uplc', the Cardano Smart Contract assembly hebi compile examples/smart_contracts/assert_sum.py Deploying The deploy process generates all artifacts required for usage with common libraries like [pycardano](https: github.com/Python-Cardano/pycardano), [lucid](https: github.com/spacebudz/lucid) and the [cardano-cli](https: github.com/input-output-hk/cardano-node). Automatically generate all artifacts needed for using this contract hebi build examples/smart_contracts/assert_sum.py See the [tutorial by pycardano ](https: pycardano.readthedocs.io/en/latest/guides/plutus.html) for explanations how to build transactions with opshin contracts. The small print _Not every valid python program is a valid smart contract_. Not all language features of python will or can be supported. The reasons are mainly of practical nature (i.e. we can't infer types when functions like eval are allowed). Specifically, only a pure subset of python is allowed. Further, only immutable objects may be generated. For your program to be accepted, make sure to only make use of language constructs supported by the compiler. You will be notified of which constructs are not supported when trying to compile. Name Hebi is japanese for \"snake\", which is a play on words on python , the underlying language. Versioning scheme Since this project builds on top of eopsin, it has a particular versioning scheme. The first three numbers indicate the version of hebi (starting at 0.1.0 ). Then follows the latest version number of eopsin which was merged into the project (starting at 0.9.3 ). This is intended to help navigating releases among both packages, where it might be important that a recent eopsin release is integrated that contains a security patch. Contributing Architecture This program consists of a few independent components: 1. An aggressive static type inferencer 2. Rewriting tools to simplify complex python expressions 3. A compiler from a subset of python into UPLC Debugging artefacts For debugging purposes, you can also run Compile script to 'uplc', and evaluate the script in UPLC (for debugging purposes) python3 -m hebi eval_uplc examples/smart_contracts/assert_sum.py \"{\\\"int\\\": 4}\" \"{\\\"int\\\": 38}\" \"{\\\"constructor\\\": 0, \\\"fields\\\": []}\" Compile script to 'pluto', an intermediate language (for debugging purposes) python3 -m hebi compile_pluto examples/smart_contracts/assert_sum.py Sponsoring You can sponsor the development of hebi through GitHub or [Teiki](https: alpha.teiki.network/projects/opshin) or just by sending ADA. Drop me a message on social media and let me know what it is for. - [Teiki](https: alpha.teiki.network/projects/opshin) Stake your ada to support OpShin at [Teiki](https: alpha.teiki.network/projects/opshin) - GitHub Sponsor the developers of this project through the button \"Sponsor\" next to them - ADA Donation in ADA can be submitted to $opshin or addr1qyz3vgd5xxevjy2rvqevz9n7n7dney8n6hqggp23479fm6vwpj9clsvsf85cd4xc59zjztr5zwpummwckmzr2myjwjns74lhmr . Supporters The main sponsor of this project is [Inversion](https: inversion.dev/cardano/). Here is a word from them! > At Inversion, we pride ourselves on our passion for life and our ability to create exceptional software solutions for our clients. Our team of experts, with over a century of cumulative experience, is dedicated to harnessing the power of the Cardano blockchain to bring innovative and scalable decentralized applications to life. We've successfully built applications for NFT management, staking and delegation, chain data monitoring, analytics, and web3 integrations, as well as countless non-blockchain systems. With a focus on security, transparency, and sustainability, our team is excited to contribute to the Cardano ecosystem, pushing the boundaries of decentralized technologies to improve lives worldwide. Trust Inversion to be your go-to partner for robust, effective, and forward-thinking solutions, whether blockchain based, traditional systems, or a mix of the two. They have recently started a podcast, called \"Africa On Chain\", which you can check out here: https: www.youtube.com/@africaonchain" +"doc":" hebi > You are building what you want. Why not also build how you want? This is an implementation of smart contracts for Cardano which are written in a very strict subset of valid Python. The general philosophy of this project is to write a compiler that ensure the following: If the program compiles then: 1. it is a valid Python program 2. the output running it with python is the same as running it on-chain. > Note this is the sister project of [eopsin](https: github.com/ImperatorLang/eopsin). It uses an even more restricted subset of python (for example no while loops). The benefit is that the resulting code is greatly reduced in size and cpu/memory consumption. Why hebi? - 100% valid Python. Leverage the existing tool stack for Python, syntax highlighting, linting, debugging, unit-testing, [property-based testing](https: hypothesis.readthedocs.io/), [verification](https: github.com/marcoeilers/nagini) - Intuitive. Just like Python. - Functional. Forces you to write elegant, functional code in Python. - Efficient & Secure. Static type inference ensures strict typing and optimized code Eopsin is more comfortable to use than hebi. If you want to start building, write your contract in eopsin first. Then, after everything works to your pleasing, try to port to hebi and enjoy the performance gains. Getting Started OpShin Pioneer Program Check out the [opshin-pioneer-program]( https: github.com/OpShin/opshin-pioneer-program) for a host of educational example contracts, test cases and off-chain code. Example repository Check out the [opshin-starter-kit]( https: github.com/OpShin/opshin-starter-kit) repository for a quick start in setting up a development environment and compiling some sample contracts yourself. You can replace the contracts in your local copy of the repository with code from the examples section here to start exploring different contracts. Developer Community and Questions The eopsin repository contains a discussions page. Feel free to open up a new discussion with questions regarding development using hebi and using certain features. Others may be able to help you and will also benefit from the previously shared questions. Check out the community [here](https: github.com/OpShin/opshin/discussions) You can also chat with other developers [in the welcoming discord community](https: discord.gg/umR3A2g4uw) of OpShin Installation Install Python 3.8, 3.9 or 3.10. Then run python3 -m pip install hebi Writing a Smart Contract A short non-complete introduction in starting to write smart contracts follows. 1. Make sure you understand EUTxOs, Addresses, Validators etc on Cardano. [There is a wonderful crashcourse by @KtorZ](https: aiken-lang.org/fundamentals/eutxo). The contract will work on these concepts 2. Make sure you understand python. hebi works like python and uses python. There are tons of tutorials for python, choose what suits you best. 3. Make sure your contract is valid python and the types check out. Write simple contracts first and run them using hebi eval to get a feeling for how they work. 4. Make sure your contract is valid hebi code. Run hebi compile and look at the compiler erros for guidance along what works and doesn't work and why. 5. Dig into the [ examples ](https: github.com/OpShin/hebi/tree/main/examples) to understand common patterns. Check out the [ prelude ](https: hebi.opshin.dev/hebi/prelude.html) for understanding how the Script Context is structured and how complex datums are defined. 6. Check out the [sample repository](https: github.com/OpShin/opshin-starter-kit) to find a sample setup for developing your own contract. In summary, a smart contract in hebi is defined by the function validator in your contract file. The function validates that a specific value can be spent, minted, burned, withdrawn etc, depending on where it is invoked/used as a credential. If the function fails (i.e. raises an error of any kind such as a KeyError or AssertionError ) the validation is denied, and the funds can not be spent, minted, burned etc. > There is a subtle difference here in comparison to most other Smart Contract languages. > In hebi a validator may return anything (in particular also False ) - as long as it does not fail, the execution is considered valid. > This is more similar to how contracts in Solidity always pass, unless they run out of gas or hit an error. > So make sure to assert what you want to ensure to hold for validation! A simple contract called the \"Gift Contract\" verifies that only specific wallets can withdraw money. They are authenticated by a signature. If you don't understand what a pubkeyhash is and how this validates anything, check out [this gentle introduction into Cardanos EUTxO](https: aiken-lang.org/fundamentals/eutxo). Also see the [tutorial by pycardano ](https: pycardano.readthedocs.io/en/latest/guides/plutus.html) for explanations on what each of the parameters to the validator means and how to build transactions with the contract. from hebi.prelude import @dataclass class WithdrawDatum(PlutusData): pubkeyhash: bytes def validator(datum: WithdrawDatum, redeemer: None, context: ScriptContext) -> None: assert datum.pubkeyhash in context.tx_info.signatories, \"Required signature missing\" All contracts written in hebi are 100% valid python. Minting policies expect only a redeemer and script context as argument. Check out the [Architecture guide](https: github.com/OpShin/hebi/blob/main/ARCHITECTURE.md minting-policy -spending-validator-double-function) for details on how to write double functioning contracts. The [ examples ](https: github.com/OpShin/hebi/blob/main/examples) folder contains more examples. Also check out the [opshin-pioneer-program]( https: github.com/OpShin/opshin-pioneer-program) and [opshin-starter-kit]( https: github.com/OpShin/opshin-starter-kit) repo. Compiling Write your program in python. You may start with the content of examples . Arguments to scripts are passed in as Plutus Data objects in JSON notation. You can run any of the following commands Evaluate script in Python - this can be used to make sure there are no obvious errors hebi eval examples/smart_contracts/assert_sum.py \"{\\\"int\\\": 4}\" \"{\\\"int\\\": 38}\" \"{\\\"constructor\\\": 0, \\\"fields\\\": []}\" Compile script to 'uplc', the Cardano Smart Contract assembly hebi compile examples/smart_contracts/assert_sum.py Deploying The deploy process generates all artifacts required for usage with common libraries like [pycardano](https: github.com/Python-Cardano/pycardano), [lucid](https: github.com/spacebudz/lucid) and the [cardano-cli](https: github.com/input-output-hk/cardano-node). Automatically generate all artifacts needed for using this contract hebi build examples/smart_contracts/assert_sum.py See the [tutorial by pycardano ](https: pycardano.readthedocs.io/en/latest/guides/plutus.html) for explanations how to build transactions with opshin contracts. The small print _Not every valid python program is a valid smart contract_. Not all language features of python will or can be supported. The reasons are mainly of practical nature (i.e. we can't infer types when functions like eval are allowed). Specifically, only a pure subset of python is allowed. Further, only immutable objects may be generated. For your program to be accepted, make sure to only make use of language constructs supported by the compiler. You will be notified of which constructs are not supported when trying to compile. Name Hebi is japanese for \"snake\", which is a play on words on python , the underlying language. Versioning scheme Since this project builds on top of eopsin, it has a particular versioning scheme. The first three numbers indicate the version of hebi (starting at 0.1.0 ). Then follows the latest version number of eopsin which was merged into the project (starting at 0.9.3 ). This is intended to help navigating releases among both packages, where it might be important that a recent eopsin release is integrated that contains a security patch. Contributing Architecture This program consists of a few independent components: 1. An aggressive static type inferencer 2. Rewriting tools to simplify complex python expressions 3. A compiler from a subset of python into UPLC Debugging artefacts For debugging purposes, you can also run Compile script to 'uplc', and evaluate the script in UPLC (for debugging purposes) python3 -m hebi eval_uplc examples/smart_contracts/assert_sum.py \"{\\\"int\\\": 4}\" \"{\\\"int\\\": 38}\" \"{\\\"constructor\\\": 0, \\\"fields\\\": []}\" Compile script to 'pluto', an intermediate language (for debugging purposes) python3 -m hebi compile_pluto examples/smart_contracts/assert_sum.py Sponsoring You can sponsor the development of hebi through GitHub or [Teiki](https: alpha.teiki.network/projects/opshin) or just by sending ADA. Drop me a message on social media and let me know what it is for. - [Kreate](https: beta.kreate.community/projects/opshin) Stake your ada to support OpShin at [Kreate](https: beta.kreate.community/projects/opshin) - GitHub Sponsor the developers of this project through the button \"Sponsor\" next to them - ADA Donation in ADA can be submitted to $opshin or addr1qyz3vgd5xxevjy2rvqevz9n7n7dney8n6hqggp23479fm6vwpj9clsvsf85cd4xc59zjztr5zwpummwckmzr2myjwjns74lhmr . Supporters The main sponsor of this project is [Inversion](https: inversion.dev/cardano/). Here is a word from them! > At Inversion, we pride ourselves on our passion for life and our ability to create exceptional software solutions for our clients. Our team of experts, with over a century of cumulative experience, is dedicated to harnessing the power of the Cardano blockchain to bring innovative and scalable decentralized applications to life. We've successfully built applications for NFT management, staking and delegation, chain data monitoring, analytics, and web3 integrations, as well as countless non-blockchain systems. With a focus on security, transparency, and sustainability, our team is excited to contribute to the Cardano ecosystem, pushing the boundaries of decentralized technologies to improve lives worldwide. Trust Inversion to be your go-to partner for robust, effective, and forward-thinking solutions, whether blockchain based, traditional systems, or a mix of the two. They have recently started a podcast, called \"Africa On Chain\", which you can check out here: https: www.youtube.com/@africaonchain" }, { "ref":"hebi.typed_ast", @@ -1843,13 +1843,19 @@ INDEX=[ "func":1 }, { -"ref":"hebi.tests.test_misc.MiscTest.test_dict_items_values_deconstr", +"ref":"hebi.tests.test_misc.MiscTest.test_nested_deconstruction", "url":4, "doc":"", "func":1 }, { -"ref":"hebi.tests.test_misc.MiscTest.test_nested_deconstruction", +"ref":"hebi.tests.test_misc.MiscTest.test_different_return_types_anything", +"url":4, +"doc":"", +"func":1 +}, +{ +"ref":"hebi.tests.test_misc.MiscTest.test_no_return_annotation_no_return", "url":4, "doc":"", "func":1 @@ -4253,17 +4259,6 @@ INDEX=[ "doc":"" }, { -"ref":"hebi.type_inference.ReturnExtractor", -"url":41, -"doc":"Utility to find all Return statements in an AST subtree" -}, -{ -"ref":"hebi.type_inference.ReturnExtractor.visit_Return", -"url":41, -"doc":"", -"func":1 -}, -{ "ref":"hebi.type_inference.AggressiveTypeInferencer", "url":41, "doc":"A :class: NodeVisitor subclass that walks the abstract syntax tree and allows modification of nodes. The NodeTransformer will walk the AST and use the return value of the visitor methods to replace or remove the old node. If the return value of the visitor method is None , the node will be removed from its location, otherwise it is replaced with the return value. The return value may be the original node in which case no replacement takes place. Here is an example transformer that rewrites all occurrences of name lookups ( foo ) to data['foo'] class RewriteName(NodeTransformer): def visit_Name(self, node): return Subscript( value=Name(id='data', ctx=Load( , slice=Index(value=Str(s=node.id , ctx=node.ctx ) Keep in mind that if the node you're operating on has child nodes you must either transform the child nodes yourself or call the :meth: generic_visit method for the node first. For nodes that were part of a collection of statements (that applies to all statement nodes), the visitor may also return a list of nodes rather than just a single node. Usually you use the transformer like this node = YourTransformer().visit(node)" @@ -4279,6 +4274,11 @@ INDEX=[ "doc":"" }, { +"ref":"hebi.type_inference.AggressiveTypeInferencer.current_ret_type", +"url":41, +"doc":"" +}, +{ "ref":"hebi.type_inference.AggressiveTypeInferencer.variable_type", "url":41, "doc":"", @@ -4743,6 +4743,23 @@ INDEX=[ "func":1 }, { +"ref":"hebi.util.ReturnExtractor", +"url":19, +"doc":"Utility to find all Return statements in an AST subtree" +}, +{ +"ref":"hebi.util.ReturnExtractor.visit_Return", +"url":19, +"doc":"", +"func":1 +}, +{ +"ref":"hebi.util.ReturnExtractor.visit", +"url":1, +"doc":"Visit a node.", +"func":1 +}, +{ "ref":"hebi.prelude", "url":42, "doc":"" diff --git a/hebi/__init__.py b/hebi/__init__.py index f8a846f..a624e58 100644 --- a/hebi/__init__.py +++ b/hebi/__init__.py @@ -6,15 +6,15 @@ import warnings -try: - from .compiler import * - from .builder import * -except ImportError as e: - warnings.warn(ImportWarning(e)) - -__version__ = "0.1.1.0.12.3" +__version__ = "0.1.1.0.12.4" __author__ = "nielstron" __author_email__ = "n.muendler@web.de" __copyright__ = "Copyright (C) 2023 nielstron" __license__ = "MIT" __url__ = "https://github.com/imperatorlang/hebi" + +try: + from .compiler import * + from .builder import * +except ImportError as e: + warnings.warn(ImportWarning(e)) diff --git a/hebi/compiler.py b/hebi/compiler.py index eeb808a..06409a6 100644 --- a/hebi/compiler.py +++ b/hebi/compiler.py @@ -18,7 +18,6 @@ from .rewrite.rewrite_zero_ary import RewriteZeroAry from .optimize.optimize_remove_pass import OptimizeRemovePass from .optimize.optimize_remove_deadvars import OptimizeRemoveDeadvars -from .optimize.optimize_varlen import OptimizeVarlen from .type_inference import * from .util import CompilingNodeTransformer, PowImpl from .typed_ast import transform_ext_params_map, transform_output_map, RawPlutoExpr @@ -242,7 +241,9 @@ def visit_Module(self, node: TypedModule) -> plt.AST: [ ( "val", - self.visit_sequence(body)(plt.Unit()), + self.visit_sequence(body)( + plt.ConstrData(plt.Integer(0), plt.EmptyDataList()) + ), ), ], plt.Apply( @@ -360,7 +361,11 @@ def visit_FunctionDef( ) -> typing.Callable[[plt.AST], plt.AST]: body = node.body.copy() # defaults to returning None if there is no return statement - compiled_body = self.visit_sequence(body)(plt.Unit()) + if node.typ.typ.rettyp.typ == AnyType(): + ret_val = plt.ConstrData(plt.Integer(0), plt.EmptyDataList()) + else: + ret_val = plt.Unit() + compiled_body = self.visit_sequence(body)(ret_val) return lambda x: plt.Let( [ ( diff --git a/hebi/tests/test_misc.py b/hebi/tests/test_misc.py index f4f172e..b9e6f6e 100644 --- a/hebi/tests/test_misc.py +++ b/hebi/tests/test_misc.py @@ -30,7 +30,7 @@ def test_assert_sum_contract_succeed(self): for d in [uplc.PlutusInteger(20), uplc.PlutusInteger(22), uplc.BuiltinUnit()]: f = uplc.Apply(f, d) ret = uplc_eval(f) - self.assertEqual(ret, uplc.BuiltinUnit()) + self.assertEqual(ret, uplc.PlutusConstr(0, [])) def test_assert_sum_contract_fail(self): input_file = "examples/smart_contracts/assert_sum.py" @@ -195,7 +195,7 @@ def test_gift_contract_succeed(self): ]: f = uplc.Apply(f, d) ret = uplc_eval(f) - self.assertEqual(ret, uplc.BuiltinUnit()) + self.assertEqual(ret, uplc.PlutusConstr(0, [])) def test_gift_contract_fail(self): input_file = "examples/smart_contracts/gift.py" @@ -843,3 +843,15 @@ def validator(a: int) -> Anything: self.assertEqual(res, uplc.PlutusByteString(b"")) res = uplc_eval(uplc.Apply(code, uplc.PlutusInteger(-1))) self.assertEqual(res, uplc.PlutusInteger(0)) + + def test_no_return_annotation_no_return(self): + source_code = """ +from hebi.prelude import * + +def validator(a): + pass +""" + ast = compiler.parse(source_code) + code = compiler.compile(ast).compile() + res = uplc_eval(uplc.Apply(code, uplc.PlutusConstr(0, []))) + self.assertEqual(res, uplc.PlutusConstr(0, [])) diff --git a/hebi/tests/test_stdlib.py b/hebi/tests/test_stdlib.py index c7d89d2..1e96f50 100644 --- a/hebi/tests/test_stdlib.py +++ b/hebi/tests/test_stdlib.py @@ -256,7 +256,9 @@ def validator(x: None) -> None: for d in [uplc.BuiltinUnit()]: f = uplc.Apply(f, d) ret = uplc_eval(f) - self.assertEqual(ret, uplc.BuiltinUnit(), "literal None returned wrong value") + self.assertEqual( + ret, uplc.PlutusConstr(0, []), "literal None returned wrong value" + ) @given(st.booleans()) def test_constant_bool(self, x: bool): diff --git a/hebi/type_inference.py b/hebi/type_inference.py index bb432fd..68cfb2f 100644 --- a/hebi/type_inference.py +++ b/hebi/type_inference.py @@ -360,7 +360,7 @@ def visit_FunctionDef(self, node: FunctionDef) -> TypedFunctionDef: # Check that return type and annotated return type match if not rets: assert ( - functyp.rettyp == NoneInstanceType + functyp.rettyp >= NoneInstanceType ), f"Function '{node.name}' has no return statement but is supposed to return not-None value" else: assert all( diff --git a/hebi/typed_ast.py b/hebi/typed_ast.py index 15fd487..dad736a 100644 --- a/hebi/typed_ast.py +++ b/hebi/typed_ast.py @@ -1107,7 +1107,9 @@ def transform_ext_params_map(p: Type): StringInstanceType: lambda x: plt.BData(plt.EncodeUtf8(x)), IntegerInstanceType: lambda x: plt.IData(x), ByteStringInstanceType: lambda x: plt.BData(x), - UnitInstanceType: lambda x: plt.Apply(plt.Lambda(["_"], plt.Unit()), x), + UnitInstanceType: lambda x: plt.Apply( + plt.Lambda(["_"], plt.ConstrData(plt.Integer(0), plt.EmptyDataList())), x + ), BoolInstanceType: lambda x: plt.IData( plt.IfThenElse(x, plt.Integer(1), plt.Integer(0)) ), diff --git a/pyproject.toml b/pyproject.toml index 9861de0..ef43ac8 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "hebi" -version = "0.1.1.0.12.3" +version = "0.1.1.0.12.4" description = "A simple and fast pythonic programming language for Smart Contracts on Cardano" authors = ["nielstron "] license = "MIT"