From e936d6e88545b2e61eaa8c2ec904cd7a4c05b2fd Mon Sep 17 00:00:00 2001 From: "Johan B.W. de Vries" Date: Sat, 31 Dec 2022 14:52:10 +0100 Subject: [PATCH] Started on tuples --- phasm/ourlang.py | 34 +++++++++++------------------ phasm/parser.py | 31 +++++++++----------------- phasm/type3/constraintsgenerator.py | 14 ++++++++++++ 3 files changed, 37 insertions(+), 42 deletions(-) diff --git a/phasm/ourlang.py b/phasm/ourlang.py index eb57978..acfa4bb 100644 --- a/phasm/ourlang.py +++ b/phasm/ourlang.py @@ -138,6 +138,19 @@ class FunctionCall(Expression): self.function = function self.arguments = [] +class TupleInstantiation(Expression): + """ + Instantiation a tuple + """ + __slots__ = ('args', ) + + args: List[Expression] + + def __init__(self, args: List[Expression]) -> None: + super().__init__() + + self.args = args + class Subscript(Expression): """ A subscript, for example to refer to a static array or tuple @@ -307,27 +320,6 @@ class StructConstructor(Function): self.struct_type3 = struct_type3 -# TODO: Broken after new type system -# class TupleConstructor(Function): -# """ -# The constructor method for a tuple -# """ -# __slots__ = ('tuple', ) -# -# tuple: TypeTuple -# -# def __init__(self, tuple_: TypeTuple) -> None: -# name = tuple_.render_internal_name() -# -# super().__init__(f'@{name}@__init___@', -1) -# -# self.returns = tuple_ -# -# for mem in tuple_.members: -# self.posonlyargs.append(FunctionParam(f'arg{mem.idx}', mem.type, )) -# -# self.tuple = tuple_ - class ModuleConstantDef: """ A constant definition within a module diff --git a/phasm/parser.py b/phasm/parser.py index 81ddbe8..9b68dfa 100644 --- a/phasm/parser.py +++ b/phasm/parser.py @@ -17,10 +17,10 @@ from .ourlang import ( Expression, BinaryOp, ConstantPrimitive, ConstantTuple, ConstantStruct, + TupleInstantiation, FunctionCall, AccessStructMember, Subscript, StructDefinition, StructConstructor, - # TupleConstructor, UnaryOp, VariableReference, Fold, @@ -413,27 +413,16 @@ class OurVisitor: _raise_static_error(node, f'Undefined variable {node.id}') if isinstance(node, ast.Tuple): - raise NotImplementedError('TODO: Broken after new type system') + arguments = [ + self.visit_Module_FunctionDef_expr(module, function, our_locals, arg_node) + for arg_node in node.elts + if isinstance(arg_node, ast.Constant) + ] - # if not isinstance(node.ctx, ast.Load): - # _raise_static_error(node, 'Must be load context') - # - # if isinstance(exp_type, TypeTuple): - # if len(exp_type.members) != len(node.elts): - # _raise_static_error(node, f'Expression is expecting a tuple of size {len(exp_type.members)}, but {len(node.elts)} are given') - # - # tuple_constructor = TupleConstructor(exp_type) - # - # func = module.functions[tuple_constructor.name] - # - # result = FunctionCall(func) - # result.arguments = [ - # self.visit_Module_FunctionDef_expr(module, function, our_locals, mem.type, arg_node) - # for arg_node, mem in zip(node.elts, exp_type.members) - # ] - # return result - # - # _raise_static_error(node, f'Expression is expecting a {codestyle.type_(exp_type)}, not a tuple') + if len(arguments) != len(node.elts): + raise NotImplementedError('Non-constant tuple members') + + return TupleInstantiation(arguments) raise NotImplementedError(f'{node} as expr in FunctionDef') diff --git a/phasm/type3/constraintsgenerator.py b/phasm/type3/constraintsgenerator.py index 36ffa3b..1c25ddc 100644 --- a/phasm/type3/constraintsgenerator.py +++ b/phasm/type3/constraintsgenerator.py @@ -102,6 +102,20 @@ def expression(ctx: Context, inp: ourlang.Expression) -> Generator[ConstraintBas return + if isinstance(inp, ourlang.TupleInstantiation): + r_type = [] + for arg in inp.args: + yield from expression(ctx, arg) + r_type.append(arg.type3) + + yield SameTypeConstraint( + inp.type3, + type3types.AppliedType3(type3types.tuple, r_type), + comment=f'The type of a tuple is a combination of its members' + ) + + return + if isinstance(inp, ourlang.Subscript): yield from expression(ctx, inp.varref) yield from expression(ctx, inp.index)