From d908f3d432d83478244cc6cbbe8fd37a4e92db1b Mon Sep 17 00:00:00 2001 From: "Johan B.W. de Vries" Date: Sat, 10 May 2025 17:04:01 +0200 Subject: [PATCH] Reworks function lookup # Conflicts: # phasm/prelude/__init__.py --- phasm/compiler.py | 26 +++----------------------- phasm/prelude/__init__.py | 15 +++++++++++---- phasm/stdlib/types.py | 2 +- 3 files changed, 15 insertions(+), 28 deletions(-) diff --git a/phasm/compiler.py b/phasm/compiler.py index 17d21b2..028c07b 100644 --- a/phasm/compiler.py +++ b/phasm/compiler.py @@ -552,29 +552,9 @@ def expression(wgn: WasmGenerator, inp: ourlang.Expression) -> None: assert arg_expr.type3 is not None, TYPE3_ASSERTION_ERROR type_var_map[type_var] = arg_expr.type3 - method_instance_key = ( - inp.function, - tuple( - - ) - ) - - instance = PRELUDE_TYPE_CLASS_INSTANCE_METHODS.get(method_instance_key) - if isinstance is not None: - instance(wgn) - return - - instance_key = ','.join( - f'{k.letter}={v.name}' - for k, v in sorted(type_var_map.items(), key=lambda x: x[0].letter) - ) - - instance = INSTANCES.get(inp.function, {}).get(instance_key, None) - if instance is not None: - instance(wgn) - return - - raise NotImplementedError(inp.function, instance_key) + router = prelude.PRELUDE_TYPE_CLASS_INSTANCE_METHODS[inp.function] + router(wgn, type_var_map) + return wgn.add_statement('call', '${}'.format(inp.function.name)) return diff --git a/phasm/prelude/__init__.py b/phasm/prelude/__init__.py index d2860dc..4c5b98c 100644 --- a/phasm/prelude/__init__.py +++ b/phasm/prelude/__init__.py @@ -12,6 +12,7 @@ from ..type3.types import ( IntType3, Type3, TypeApplication_Nullary, + TypeApplicationRouter, TypeConstructor_StaticArray, TypeConstructor_Struct, TypeConstructor_Tuple, @@ -19,9 +20,9 @@ from ..type3.types import ( PRELUDE_TYPE_CLASS_INSTANCES_EXISTING: set[tuple[Type3Class, tuple[Type3, ...]]] = set() -PRELUDE_TYPE_CLASS_INSTANCE_METHODS: dict[tuple[Type3ClassMethod, tuple[Type3, ...]], Callable[[Generator, Type3], None]] = {} +PRELUDE_TYPE_CLASS_INSTANCE_METHODS: dict[Type3ClassMethod, TypeApplicationRouter[Generator, None]] = {} -def instance_type_class(cls: Type3Class, *typ: Type3, methods: dict[str, Callable[[Generator, Type3], None]] = {}) -> None: +def instance_type_class(cls: Type3Class, *typ: Type3, methods: dict[str, Callable[[Generator], None]] = {}) -> None: global PRELUDE_TYPE_CLASS_INSTANCES_EXISTING global PRELUDE_TYPE_CLASS_INSTANCE_METHODS @@ -29,9 +30,15 @@ def instance_type_class(cls: Type3Class, *typ: Type3, methods: dict[str, Callabl PRELUDE_TYPE_CLASS_INSTANCES_EXISTING.add((cls, tuple(typ), )) - for method, generator in methods.items(): - PRELUDE_TYPE_CLASS_INSTANCE_METHODS[(cls.methods[method], tuple(typ))] = generator + for method_name, generator in methods.items(): + method = cls.methods[method_name] + router = PRELUDE_TYPE_CLASS_INSTANCE_METHODS.get(method) + if router is None: + router = TypeApplicationRouter[Generator, None]() + PRELUDE_TYPE_CLASS_INSTANCE_METHODS[method] = router + + router.add_n(typ, generator) none = Type3('none', TypeApplication_Nullary(None, None)) """ diff --git a/phasm/stdlib/types.py b/phasm/stdlib/types.py index d81f971..2ae449c 100644 --- a/phasm/stdlib/types.py +++ b/phasm/stdlib/types.py @@ -833,7 +833,7 @@ def f64_natnum_arithmic_shift_right(g: Generator) -> None: ## ### ## class IntNum -def i32_intnum_abs(g: Generator, t: Type3 | None = None) -> None: +def i32_intnum_abs(g: Generator) -> None: g.add_statement('call $stdlib.types.__i32_intnum_abs__') def i64_intnum_abs(g: Generator) -> None: