def entry(self) -> Reference: """ Get the index of the program's entry point """ return self.resolve( NamedAccess([Identifier('Program'), Identifier.constructor()]), {})
def finalize(self): super().finalize() if Identifier.constructor() not in self.names: # Add implicit constructor self.children.insert(0, MethodDefinition.empty_constructor(self)) if self.extends and self.extends.untyped in definitions.INTERNAL_SOURCES: self.children.insert(0, MemberDefinition(Identifier.super(), self.extends).deriving_from(self))
def compile(self, context: CompilationBuffer): if not self.values: return buffer = context.optional() ref = self[0].compile(buffer) # TODO: remove unnecessary recompilation of first element (used to infer type) list_type = GenericIdentifier(Identifier('list'), (ref.type,)) last_call = MethodCall(NamedAccess([list_type, Identifier.constructor()])).deriving_from(self) for value in self: # TODO: validate list is homogeneous, and descend to lowest common type last_call = MethodCall(NamedAccess([last_call, Identifier("append")]), ArgumentList([value])).deriving_from(self) return last_call.compile(context)
def finalize(self): if not self.is_constructor(): return super().finalize() for descendant in self.descendants: if isinstance( descendant, MethodCall) and descendant.target[0] == Identifier.super(): descendant.replace( AssignmentOperation( AssignmentOperation.REASSIGNMENT, NamedAccess([Identifier.self(), Identifier.super()]), MethodCall(NamedAccess( [self.parent.extends, Identifier.constructor()]), descendant.arguments, is_captured=True).deriving_from( self)).deriving_from(descendant)) super().finalize()
def parse_instantiating_call(target: Identifier, arguments: 'ParenthesesVector'): return MethodCall(NamedAccess.extend(target, Identifier.constructor()), ArgumentList(arguments))
def constructing_call(self): return self.target[-1] == Identifier.constructor()
def test_constructor_arguments(): method = parse_local("setup with text name, number age") parse_local("thing container").attach(method) validate_method_definition(method, Identifier.constructor(), [('name', 'text'), ('age', 'number')], Identifier('container'))
def test_simple_constructor_definition(): method = parse_local("setup") parse_local("thing container").attach(method) validate_method_definition(method, Identifier.constructor(), return_type=Identifier('container'))
def test_constructing_call(): method = parse_local('Empty(1)') assert method.target == NamedAccess([Identifier('Empty'), Identifier.constructor()]) validate_types(method.arguments, [NumericValue], MethodCall, lambda x: x.arguments) assert method.constructing_call
def is_constructor(self): return self.name == Identifier.constructor()
def constructor_definition(_1: LexicalDeclarationConstructor): return MethodDefinition(Identifier.constructor(), token=_1)
def empty_constructor(cls, parent): instance = MethodDefinition( Identifier.constructor()).deriving_from(parent) instance.parent = parent return instance