Reworks function lookup

This commit is contained in:
Johan B.W. de Vries 2025-05-09 17:53:04 +02:00
parent 78c98b1e61
commit c8bf5e7c59
3 changed files with 31 additions and 19 deletions

View File

@ -183,18 +183,6 @@ INSTANCES = {
'a=i32': stdlib_types.i32_integral_rem, 'a=i32': stdlib_types.i32_integral_rem,
'a=i64': stdlib_types.i64_integral_rem, 'a=i64': stdlib_types.i64_integral_rem,
}, },
prelude.IntNum.methods['abs']: {
'a=i32': stdlib_types.i32_intnum_abs,
'a=i64': stdlib_types.i64_intnum_abs,
'a=f32': stdlib_types.f32_intnum_abs,
'a=f64': stdlib_types.f64_intnum_abs,
},
prelude.IntNum.methods['neg']: {
'a=i32': stdlib_types.i32_intnum_neg,
'a=i64': stdlib_types.i64_intnum_neg,
'a=f32': stdlib_types.f32_intnum_neg,
'a=f64': stdlib_types.f64_intnum_neg,
},
prelude.NatNum.operators['+']: { prelude.NatNum.operators['+']: {
'a=u32': stdlib_types.u32_natnum_add, 'a=u32': stdlib_types.u32_natnum_add,
'a=u64': stdlib_types.u64_natnum_add, 'a=u64': stdlib_types.u64_natnum_add,
@ -564,6 +552,18 @@ def expression(wgn: WasmGenerator, inp: ourlang.Expression) -> None:
assert arg_expr.type3 is not None, TYPE3_ASSERTION_ERROR assert arg_expr.type3 is not None, TYPE3_ASSERTION_ERROR
type_var_map[type_var] = arg_expr.type3 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( instance_key = ','.join(
f'{k.letter}={v.name}' f'{k.letter}={v.name}'
for k, v in sorted(type_var_map.items(), key=lambda x: x[0].letter) for k, v in sorted(type_var_map.items(), key=lambda x: x[0].letter)

View File

@ -1,10 +1,13 @@
""" """
The prelude are all the builtin types, type classes and methods The prelude are all the builtin types, type classes and methods
""" """
from ..type3.functions import ( from typing import Callable
TypeVariable,
) from phasm.stdlib import types as stdtypes
from ..type3.typeclasses import Type3Class from phasm.wasmgenerator import Generator
from ..type3.functions import TypeVariable
from ..type3.typeclasses import Type3Class, Type3ClassMethod
from ..type3.types import ( from ..type3.types import (
IntType3, IntType3,
Type3, Type3,
@ -16,14 +19,20 @@ from ..type3.types import (
PRELUDE_TYPE_CLASS_INSTANCES_EXISTING: set[tuple[Type3Class, tuple[Type3, ...]]] = set() 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]] = {}
def instance_type_class(cls: Type3Class, *typ: Type3) -> None: def instance_type_class(cls: Type3Class, *typ: Type3, methods: dict[str, Callable[[Generator, Type3], None]] = {}) -> None:
global PRELUDE_TYPE_CLASS_INSTANCES_EXISTING global PRELUDE_TYPE_CLASS_INSTANCES_EXISTING
global PRELUDE_TYPE_CLASS_INSTANCE_METHODS
# TODO: Check for required existing instantiations # TODO: Check for required existing instantiations
PRELUDE_TYPE_CLASS_INSTANCES_EXISTING.add((cls, tuple(typ), )) 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
none = Type3('none', TypeApplication_Nullary(None, None)) none = Type3('none', TypeApplication_Nullary(None, None))
""" """
The none type, for when functions simply don't return anything. e.g., IO(). The none type, for when functions simply don't return anything. e.g., IO().
@ -227,7 +236,9 @@ IntNum = Type3Class('IntNum', [a], methods={
'neg': [a, a], 'neg': [a, a],
}, operators={}, inherited_classes=[NatNum]) }, operators={}, inherited_classes=[NatNum])
instance_type_class(IntNum, i32) instance_type_class(IntNum, i32, methods={
'abs': stdtypes.i32_intnum_abs,
})
instance_type_class(IntNum, i64) instance_type_class(IntNum, i64)
instance_type_class(IntNum, f32) instance_type_class(IntNum, f32)
instance_type_class(IntNum, f64) instance_type_class(IntNum, f64)

View File

@ -2,6 +2,7 @@
stdlib: Standard types that are not wasm primitives stdlib: Standard types that are not wasm primitives
""" """
from phasm.stdlib import alloc from phasm.stdlib import alloc
from phasm.type3.types import Type3
from phasm.wasmgenerator import Generator, func_wrapper from phasm.wasmgenerator import Generator, func_wrapper
from phasm.wasmgenerator import VarType_i32 as i32 from phasm.wasmgenerator import VarType_i32 as i32
from phasm.wasmgenerator import VarType_i64 as i64 from phasm.wasmgenerator import VarType_i64 as i64
@ -832,7 +833,7 @@ def f64_natnum_arithmic_shift_right(g: Generator) -> None:
## ### ## ###
## class IntNum ## class IntNum
def i32_intnum_abs(g: Generator) -> None: def i32_intnum_abs(g: Generator, t: Type3 | None = None) -> None:
g.add_statement('call $stdlib.types.__i32_intnum_abs__') g.add_statement('call $stdlib.types.__i32_intnum_abs__')
def i64_intnum_abs(g: Generator) -> None: def i64_intnum_abs(g: Generator) -> None: