Work on bytes

This commit is contained in:
Johan B.W. de Vries 2022-12-24 19:47:13 +01:00
parent 25b5d6fc06
commit 750f8806b6
3 changed files with 42 additions and 1 deletions

View File

@ -312,6 +312,12 @@ def expression(wgn: WasmGenerator, inp: ourlang.Expression) -> None:
if isinstance(inp, ourlang.Subscript): if isinstance(inp, ourlang.Subscript):
assert isinstance(inp.varref.type3, type3types.Type3), type3types.TYPE3_ASSERTION_ERROR assert isinstance(inp.varref.type3, type3types.Type3), type3types.TYPE3_ASSERTION_ERROR
if inp.varref.type3 is type3types.bytes:
expression(wgn, inp.varref)
expression(wgn, inp.index)
wgn.call(stdlib_types.__subscript_bytes__)
return
if isinstance(inp.varref.type3, type3types.AppliedType3): if isinstance(inp.varref.type3, type3types.AppliedType3):
if inp.varref.type3.base == type3types.static_array: if inp.varref.type3.base == type3types.static_array:
assert 1 == len(inp.varref.type3.args) assert 1 == len(inp.varref.type3.args)

View File

@ -394,7 +394,11 @@ class CanBeSubscriptedConstraint(ConstraintBase):
SameTypeConstraint(self.type3.args[self.index.value], self.ret_type3, comment=f'Tuple subscript index {self.index.value}'), SameTypeConstraint(self.type3.args[self.index.value], self.ret_type3, comment=f'Tuple subscript index {self.index.value}'),
] ]
# FIXME: bytes if self.type3 is types.bytes:
return [
SameTypeConstraint(types.u32, self.index_type3, comment='([]) :: bytes -> u32 -> u8'),
SameTypeConstraint(types.u8, self.ret_type3, comment='([]) :: bytes -> u32 -> u8'),
]
if self.type3.name in types.LOOKUP_TABLE: if self.type3.name in types.LOOKUP_TABLE:
return Error(f'{self.type3.name} cannot be subscripted') return Error(f'{self.type3.name} cannot be subscripted')

View File

@ -1,5 +1,7 @@
import pytest import pytest
from phasm.type3.entry import Type3Exception
from ..helpers import Suite from ..helpers import Suite
@pytest.mark.integration_test @pytest.mark.integration_test
@ -51,3 +53,32 @@ def testEntry(f: bytes) -> u8:
result = Suite(code_py).run_code(b'Short', b'Long' * 100) result = Suite(code_py).run_code(b'Short', b'Long' * 100)
assert 0 == result.returned_value assert 0 == result.returned_value
@pytest.mark.integration_test
def test_function_call_element_ok():
code_py = """
@exported
def testEntry(f: bytes) -> u8:
return helper(f[0])
def helper(x: u8) -> u8:
return x
"""
result = Suite(code_py).run_code(b'Short')
assert 83 == result.returned_value
@pytest.mark.integration_test
def test_function_call_element_type_mismatch():
code_py = """
@exported
def testEntry(f: bytes) -> u64:
return helper(f[0])
def helper(x: u64) -> u64:
return x
"""
with pytest.raises(Type3Exception, match=r'u64 must be u8 instead'):
Suite(code_py).run_code()