From 44b95af4ba34cf1299037a105bc481e9f7b755f6 Mon Sep 17 00:00:00 2001 From: "Johan B.W. de Vries" Date: Fri, 2 May 2025 21:12:05 +0200 Subject: [PATCH] Removes the cast / u32 hacky way of casting. --- examples/crc32.py | 2 +- phasm/codestyle.py | 7 ---- phasm/compiler.py | 5 --- phasm/parser.py | 10 ----- phasm/type3/constraints.py | 44 -------------------- phasm/type3/constraintsgenerator.py | 6 --- tests/integration/test_lang/test_builtins.py | 2 +- 7 files changed, 2 insertions(+), 74 deletions(-) diff --git a/examples/crc32.py b/examples/crc32.py index a3d7e37..0f5c989 100644 --- a/examples/crc32.py +++ b/examples/crc32.py @@ -51,7 +51,7 @@ _CRC32_Table: u32[256] = ( ) def _crc32_f(crc: u32, byt: u8) -> u32: - return (crc >> 8) ^ _CRC32_Table[(crc & 0xFF) ^ u32(byt)] + return (crc >> 8) ^ _CRC32_Table[(crc & 0xFF) ^ extend(byt)] @exported def crc32(data: bytes) -> u32: diff --git a/phasm/codestyle.py b/phasm/codestyle.py index d6f57bb..5ac0efe 100644 --- a/phasm/codestyle.py +++ b/phasm/codestyle.py @@ -76,13 +76,6 @@ def expression(inp: ourlang.Expression) -> str: return str(inp.variable.name) if isinstance(inp, ourlang.UnaryOp): - if inp.operator == 'cast': - mtyp = type3(inp.type3) - if mtyp is None: - raise NotImplementedError(f'Casting to type {inp.type_var}') - - return f'{mtyp}({expression(inp.right)})' - return f'{inp.operator}{expression(inp.right)}' if isinstance(inp, ourlang.BinaryOp): diff --git a/phasm/compiler.py b/phasm/compiler.py index b94e358..33b1499 100644 --- a/phasm/compiler.py +++ b/phasm/compiler.py @@ -484,11 +484,6 @@ def expression(wgn: WasmGenerator, inp: ourlang.Expression) -> None: assert isinstance(inp.type3, type3types.Type3), type3placeholders.TYPE3_ASSERTION_ERROR - if inp.operator == 'cast': - if inp.type3 == prelude.u32 and inp.right.type3 == prelude.u8: - # Nothing to do, you can use an u8 value as a u32 no problem - return - raise NotImplementedError(expression, inp.type3, inp.operator) if isinstance(inp, ourlang.FunctionCall): diff --git a/phasm/parser.py b/phasm/parser.py index 831b435..1d4404f 100644 --- a/phasm/parser.py +++ b/phasm/parser.py @@ -489,16 +489,6 @@ class OurVisitor: if node.func.id in PRELUDE_METHODS: func = PRELUDE_METHODS[node.func.id] - elif node.func.id == 'u32': - if 1 != len(node.args): - _raise_static_error(node, f'Function {node.func.id} requires 1 arguments but {len(node.args)} are given') - - unary_op = UnaryOp( - 'cast', - self.visit_Module_FunctionDef_expr(module, function, our_locals, node.args[0]), - ) - unary_op.type3 = prelude.u32 - return unary_op elif node.func.id == 'foldl': if 3 != len(node.args): _raise_static_error(node, f'Function {node.func.id} requires 3 arguments but {len(node.args)} are given') diff --git a/phasm/type3/constraints.py b/phasm/type3/constraints.py index 256daa0..111fdd3 100644 --- a/phasm/type3/constraints.py +++ b/phasm/type3/constraints.py @@ -197,50 +197,6 @@ class TupleMatchConstraint(ConstraintBase): raise NotImplementedError(exp_type) -class CastableConstraint(ConstraintBase): - """ - A type can be cast to another type - """ - __slots__ = ('from_type3', 'to_type3', ) - - from_type3: placeholders.Type3OrPlaceholder - to_type3: placeholders.Type3OrPlaceholder - - def __init__(self, from_type3: placeholders.Type3OrPlaceholder, to_type3: placeholders.Type3OrPlaceholder, comment: Optional[str] = None) -> None: - super().__init__(comment=comment) - - self.from_type3 = from_type3 - self.to_type3 = to_type3 - - def check(self) -> CheckResult: - ftyp = self.from_type3 - if isinstance(ftyp, placeholders.PlaceholderForType) and ftyp.resolve_as is not None: - ftyp = ftyp.resolve_as - - ttyp = self.to_type3 - if isinstance(ttyp, placeholders.PlaceholderForType) and ttyp.resolve_as is not None: - ttyp = ttyp.resolve_as - - if isinstance(ftyp, placeholders.PlaceholderForType) or isinstance(ttyp, placeholders.PlaceholderForType): - return RequireTypeSubstitutes() - - if ftyp is prelude.u8 and ttyp is prelude.u32: - return None - - return Error(f'Cannot cast {ftyp.name} to {ttyp.name}') - - def human_readable(self) -> HumanReadableRet: - return ( - '{to_type3}({from_type3})', - { - 'to_type3': self.to_type3, - 'from_type3': self.from_type3, - }, - ) - - def __repr__(self) -> str: - return f'CastableConstraint({repr(self.from_type3)}, {repr(self.to_type3)}, comment={repr(self.comment)})' - class MustImplementTypeClassConstraint(ConstraintBase): """ A type must implement a given type class diff --git a/phasm/type3/constraintsgenerator.py b/phasm/type3/constraintsgenerator.py index 5d0d99e..9138984 100644 --- a/phasm/type3/constraintsgenerator.py +++ b/phasm/type3/constraintsgenerator.py @@ -12,7 +12,6 @@ from . import typeclasses as typeclasses from . import types as type3types from .constraints import ( CanBeSubscriptedConstraint, - CastableConstraint, ConstraintBase, Context, LiteralFitsConstraint, @@ -50,11 +49,6 @@ def expression(ctx: Context, inp: ourlang.Expression) -> ConstraintGenerator: return if isinstance(inp, ourlang.UnaryOp): - if 'cast' == inp.operator: - yield from expression(ctx, inp.right) - yield CastableConstraint(inp.right.type3, inp.type3) - return - raise NotImplementedError(expression, inp, inp.operator) if isinstance(inp, ourlang.BinaryOp) or isinstance(inp, ourlang.FunctionCall): diff --git a/tests/integration/test_lang/test_builtins.py b/tests/integration/test_lang/test_builtins.py index ce45cef..13400fd 100644 --- a/tests/integration/test_lang/test_builtins.py +++ b/tests/integration/test_lang/test_builtins.py @@ -49,7 +49,7 @@ def testEntry(a: bytes, b: bytes) -> u8: def test_foldl_3(): code_py = """ def xor(l: u32, r: u8) -> u32: - return l ^ u32(r) + return l ^ extend(r) @exported def testEntry(a: bytes) -> u32: