diff --git a/phasm/codestyle.py b/phasm/codestyle.py index 70fee26..abf8e86 100644 --- a/phasm/codestyle.py +++ b/phasm/codestyle.py @@ -35,11 +35,18 @@ def type3(inp: Type3OrPlaceholder) -> str: """ assert isinstance(inp, Type3), TYPE3_ASSERTION_ERROR - if isinstance(inp, type3types.AppliedType3) and inp.base is type3types.tuple: - return '(' + ', '.join( - type3(x) - for x in inp.args - ) + ', )' + if isinstance(inp, type3types.AppliedType3): + if inp.base is type3types.tuple: + return '(' + ', '.join( + type3(x) + 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 diff --git a/phasm/parser.py b/phasm/parser.py index 9e57094..44d2680 100644 --- a/phasm/parser.py +++ b/phasm/parser.py @@ -723,7 +723,7 @@ class OurVisitor: if not isinstance(node.value, ast.Name): _raise_static_error(node, 'Must be name') 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): _raise_static_error(node, 'Must subscript using a constant index') if not isinstance(node.slice.value.value, int): diff --git a/phasm/type3/constraints.py b/phasm/type3/constraints.py index 2643c97..c2eab3b 100644 --- a/phasm/type3/constraints.py +++ b/phasm/type3/constraints.py @@ -264,25 +264,49 @@ class LiteralFitsConstraint(ConstraintBase): res: NewConstraintList - if isinstance(self.type3, types.AppliedType3) and self.type3.base is types.tuple: - if not isinstance(self.literal, ourlang.ConstantTuple): - return Error('Must be tuple') + if isinstance(self.type3, types.AppliedType3): + if self.type3.base is types.tuple: + if not isinstance(self.literal, ourlang.ConstantTuple): + return Error('Must be tuple') - if len(self.type3.args) != len(self.literal.value): - return Error('Tuple element count mismatch') + if len(self.type3.args) != len(self.literal.value): + return Error('Tuple element count mismatch') - res = [] + res = [] - res.extend( - LiteralFitsConstraint(x, y) - for x, y in zip(self.type3.args, self.literal.value) - ) - res.extend( - SameTypeConstraint(x, y.type3) - for x, y in zip(self.type3.args, self.literal.value) - ) + res.extend( + LiteralFitsConstraint(x, y) + for x, y in zip(self.type3.args, self.literal.value) + ) + res.extend( + SameTypeConstraint(x, y.type3) + for x, y in zip(self.type3.args, self.literal.value) + ) - 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 not isinstance(self.literal, ourlang.ConstantStruct): diff --git a/tests/integration/test_lang/test_static_array.py b/tests/integration/test_lang/test_static_array.py index b83a2bf..5c83943 100644 --- a/tests/integration/test_lang/test_static_array.py +++ b/tests/integration/test_lang/test_static_array.py @@ -7,9 +7,23 @@ from ..constants import ( ) 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.parametrize('type_', ALL_INT_TYPES) -def test_module_constant(type_): +def test_module_constant_3(type_): code_py = f""" CONSTANT: {type_}[3] = (24, 57, 80, )