From 6e0c554cf283fcee1c11bc2c52f1f3a6bb05324f Mon Sep 17 00:00:00 2001 From: "Johan B.W. de Vries" Date: Sat, 24 Dec 2022 17:56:47 +0100 Subject: [PATCH] Starting to see how this could work with static arrays --- phasm/compiler.py | 22 +++++++++++++++++++++- phasm/type3/constraints.py | 12 ++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/phasm/compiler.py b/phasm/compiler.py index 2629ade..97855fc 100644 --- a/phasm/compiler.py +++ b/phasm/compiler.py @@ -192,6 +192,13 @@ def expression(wgn: WasmGenerator, inp: ourlang.Expression) -> None: wgn.i32.const(inp.variable.data_block.address) return + if isinstance(inp.type3, type3types.AppliedType3): + if inp.type3.base is type3types.static_array: + assert inp.variable.data_block is not None, 'Static arrays must be memory stored' + assert inp.variable.data_block.address is not None, 'Value not allocated' + wgn.i32.const(inp.variable.data_block.address) + return + # TODO: Broken after new type system # if isinstance(inp.type, typing.TypeTuple): # assert isinstance(inp.definition.constant, ourlang.ConstantTuple) @@ -306,7 +313,20 @@ def expression(wgn: WasmGenerator, inp: ourlang.Expression) -> None: return if isinstance(inp, ourlang.Subscript): - # assert inp.varref.type3 is not None, typing.ASSERTION_ERROR + assert isinstance(inp.varref.type3, type3types.Type3), type3types.TYPE3_ASSERTION_ERROR + + if isinstance(inp.varref.type3, type3types.AppliedType3): + if inp.varref.type3.base is type3types.static_array: + assert 1 == len(inp.varref.type3.args) + assert isinstance(inp.varref.type3.args[0], type3types.Type3) + + expression(wgn, inp.varref) + expression(wgn, inp.index) + wgn.i32.const(_calculate_alloc_size(inp.varref.type3.args[0])) + wgn.i32.mul() + wgn.i32.add() + return + # # assert inp.varref.type_var is not None, typing.ASSERTION_ERROR # tc_type = inp.varref.type_var.get_type() diff --git a/phasm/type3/constraints.py b/phasm/type3/constraints.py index c2eab3b..65378fc 100644 --- a/phasm/type3/constraints.py +++ b/phasm/type3/constraints.py @@ -364,6 +364,18 @@ class CanBeSubscriptedConstraint(ConstraintBase): self.index_type3 = index.type3 def check(self, smap: SubstitutionMap) -> CheckResult: + if isinstance(self.type3, types.PlaceholderForType): + if self.type3 not in smap: + return RequireTypeSubstitutes() + + self.type3 = smap[self.type3] + + if isinstance(self.type3, types.AppliedType3): + if self.type3.base is types.static_array: + return [ + SameTypeConstraint(types.u32, self.index_type3, comment='([]) :: Subscriptable a => a b -> u32 -> b') + ] + raise NotImplementedError # if isinstance(self.type3, types.PlaceholderForType): # return RequireTypeSubstitutes()