def test_method_parameterization(): pair_number = get_parametrized() validate_method(pair_number[Identifier('set_values')], 'number', ['number'] * 2, 1) validate_method(pair_number[Identifier('nested_param')], GenericIdentifier.wrap('list', 'number'), [GenericIdentifier.wrap('list', 'number')], 2)
def test_nested_member_parameterization(): pair_number = get_parametrized() validate_member(pair_number[Identifier('parts')], GenericIdentifier.wrap('list', 'number'), 2) validate_member( pair_number[Identifier('nested')], GenericIdentifier.wrap( 'list', GenericIdentifier.wrap('list', GenericIdentifier.wrap('list', 'number'))), 3)
def test_parameterization_propagation(): symbols = get_symbols(SOURCE_FULL) generic_type = symbols[Identifier('Person')][Identifier( 'favorite_numbers')].type parametrized_type = symbols[generic_type] assert parametrized_type.name == GenericIdentifier( Identifier('Pair'), (Identifier('number'), ))
def return_type(self): if self.is_constructor(): if self.parent.generics: return GenericIdentifier(self.parent.name, tuple(self.parent.generics)) return self.parent.name return self._return_type
def load_identifier(value): """ Parse a generic identifier """ if isinstance(value, str): return Identifier(value) elif isinstance(value, list): return GenericIdentifier(Identifier(value[0]), tuple(Identifier(x) for x in value[1])) elif isinstance(value, dict) and value['intent'] == 'cast': return CastTag(Symbol.load_identifier(value['type']))
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 parameterize(self, parameters: dict) -> 'SymbolMap': """ Creates a new SymbolMap, replacing the generic parameters in this SymbolMap with determined values :param parameters: a mapping of generic name -> resolved name """ assert set(parameters.keys()) == set( self.generics), 'Partial parameterization is not allowed' return SymbolMap([x.parameterize(parameters) for x in self.members], [x.parameterize(parameters) for x in self.methods], GenericIdentifier( self.name, tuple([parameters[x] for x in self.generics])), self.extends, [], self.convention, self.member_offset, self.method_offset)
def iterator_container_name(self): return Identifier(f'__iteration_loop_container_{self.iterator_id}__' ), GenericIdentifier(Identifier('iterator'), (self.target_type, ))