Started on tuples

This commit is contained in:
Johan B.W. de Vries 2022-12-31 14:52:10 +01:00
parent ac8daf74f5
commit e936d6e885
3 changed files with 37 additions and 42 deletions

View File

@ -138,6 +138,19 @@ class FunctionCall(Expression):
self.function = function self.function = function
self.arguments = [] 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): class Subscript(Expression):
""" """
A subscript, for example to refer to a static array or tuple A subscript, for example to refer to a static array or tuple
@ -307,27 +320,6 @@ class StructConstructor(Function):
self.struct_type3 = struct_type3 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: class ModuleConstantDef:
""" """
A constant definition within a module A constant definition within a module

View File

@ -17,10 +17,10 @@ from .ourlang import (
Expression, Expression,
BinaryOp, BinaryOp,
ConstantPrimitive, ConstantTuple, ConstantStruct, ConstantPrimitive, ConstantTuple, ConstantStruct,
TupleInstantiation,
FunctionCall, AccessStructMember, Subscript, FunctionCall, AccessStructMember, Subscript,
StructDefinition, StructConstructor, StructDefinition, StructConstructor,
# TupleConstructor,
UnaryOp, VariableReference, UnaryOp, VariableReference,
Fold, Fold,
@ -413,27 +413,16 @@ class OurVisitor:
_raise_static_error(node, f'Undefined variable {node.id}') _raise_static_error(node, f'Undefined variable {node.id}')
if isinstance(node, ast.Tuple): 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): if len(arguments) != len(node.elts):
# _raise_static_error(node, 'Must be load context') raise NotImplementedError('Non-constant tuple members')
#
# if isinstance(exp_type, TypeTuple): return TupleInstantiation(arguments)
# 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')
raise NotImplementedError(f'{node} as expr in FunctionDef') raise NotImplementedError(f'{node} as expr in FunctionDef')

View File

@ -102,6 +102,20 @@ def expression(ctx: Context, inp: ourlang.Expression) -> Generator[ConstraintBas
return 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): if isinstance(inp, ourlang.Subscript):
yield from expression(ctx, inp.varref) yield from expression(ctx, inp.varref)
yield from expression(ctx, inp.index) yield from expression(ctx, inp.index)