Some fixes for static array

This commit is contained in:
Johan B.W. de Vries 2022-12-24 17:37:53 +01:00
parent d4d5e9e482
commit 1ab6dfd333
4 changed files with 67 additions and 22 deletions

View File

@ -35,12 +35,19 @@ def type3(inp: Type3OrPlaceholder) -> str:
""" """
assert isinstance(inp, Type3), TYPE3_ASSERTION_ERROR assert isinstance(inp, Type3), TYPE3_ASSERTION_ERROR
if isinstance(inp, type3types.AppliedType3) and inp.base is type3types.tuple: if isinstance(inp, type3types.AppliedType3):
if inp.base is type3types.tuple:
return '(' + ', '.join( return '(' + ', '.join(
type3(x) type3(x)
for x in inp.args for x in inp.args
) + ', )' ) + ', )'
if inp.base is type3types.static_array:
assert 1 == len(inp.args)
assert isinstance(inp.args[0], Type3), TYPE3_ASSERTION_ERROR
return inp.args[0].name + '[3]' # FIXME: Where to store this value?
return inp.name return inp.name
def struct_definition(inp: ourlang.StructDefinition) -> str: def struct_definition(inp: ourlang.StructDefinition) -> str:

View File

@ -723,7 +723,7 @@ class OurVisitor:
if not isinstance(node.value, ast.Name): if not isinstance(node.value, ast.Name):
_raise_static_error(node, 'Must be name') _raise_static_error(node, 'Must be name')
if not isinstance(node.slice, ast.Index): if not isinstance(node.slice, ast.Index):
_raise_static_error(node, 'Must subscript using an index') _raise_static_error(node, 'Must subscript using an index') # FIXME: Do we use type level length?
if not isinstance(node.slice.value, ast.Constant): if not isinstance(node.slice.value, ast.Constant):
_raise_static_error(node, 'Must subscript using a constant index') _raise_static_error(node, 'Must subscript using a constant index')
if not isinstance(node.slice.value.value, int): if not isinstance(node.slice.value.value, int):

View File

@ -264,7 +264,8 @@ class LiteralFitsConstraint(ConstraintBase):
res: NewConstraintList res: NewConstraintList
if isinstance(self.type3, types.AppliedType3) and self.type3.base is types.tuple: if isinstance(self.type3, types.AppliedType3):
if self.type3.base is types.tuple:
if not isinstance(self.literal, ourlang.ConstantTuple): if not isinstance(self.literal, ourlang.ConstantTuple):
return Error('Must be tuple') return Error('Must be tuple')
@ -284,6 +285,29 @@ class LiteralFitsConstraint(ConstraintBase):
return res return res
if self.type3.base is types.static_array:
if not isinstance(self.literal, ourlang.ConstantTuple):
return Error('Must be tuple')
assert 1 == len(self.type3.args)
# FIXME: How to store type level length?
# if len(self.type3.args) != len(self.literal.value):
# return Error('Tuple element count mismatch')
res = []
res.extend(
LiteralFitsConstraint(self.type3.args[0], y)
for y in self.literal.value
)
res.extend(
SameTypeConstraint(self.type3.args[0], y.type3)
for y in self.literal.value
)
return res
if isinstance(self.type3, types.StructType3): if isinstance(self.type3, types.StructType3):
if not isinstance(self.literal, ourlang.ConstantStruct): if not isinstance(self.literal, ourlang.ConstantStruct):
return Error('Must be struct') return Error('Must be struct')

View File

@ -7,9 +7,23 @@ from ..constants import (
) )
from ..helpers import Suite from ..helpers import Suite
@pytest.mark.integration_test
def test_module_constant_def():
code_py = """
CONSTANT: u8[3] = (24, 57, 80, )
@exported
def testEntry() -> i32:
return 0
"""
result = Suite(code_py).run_code()
assert 0 == result.returned_value
@pytest.mark.integration_test @pytest.mark.integration_test
@pytest.mark.parametrize('type_', ALL_INT_TYPES) @pytest.mark.parametrize('type_', ALL_INT_TYPES)
def test_module_constant(type_): def test_module_constant_3(type_):
code_py = f""" code_py = f"""
CONSTANT: {type_}[3] = (24, 57, 80, ) CONSTANT: {type_}[3] = (24, 57, 80, )