Started on tuples
This commit is contained in:
parent
ac8daf74f5
commit
e936d6e885
@ -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
|
||||
|
||||
@ -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')
|
||||
|
||||
|
||||
@ -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)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user