Removes the cast / u32 hacky way of casting.

This commit is contained in:
Johan B.W. de Vries 2025-05-02 21:12:05 +02:00
parent 1da1adac9f
commit 44b95af4ba
7 changed files with 2 additions and 74 deletions

View File

@ -51,7 +51,7 @@ _CRC32_Table: u32[256] = (
) )
def _crc32_f(crc: u32, byt: u8) -> u32: 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 @exported
def crc32(data: bytes) -> u32: def crc32(data: bytes) -> u32:

View File

@ -76,13 +76,6 @@ def expression(inp: ourlang.Expression) -> str:
return str(inp.variable.name) return str(inp.variable.name)
if isinstance(inp, ourlang.UnaryOp): 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)}' return f'{inp.operator}{expression(inp.right)}'
if isinstance(inp, ourlang.BinaryOp): if isinstance(inp, ourlang.BinaryOp):

View File

@ -484,11 +484,6 @@ def expression(wgn: WasmGenerator, inp: ourlang.Expression) -> None:
assert isinstance(inp.type3, type3types.Type3), type3placeholders.TYPE3_ASSERTION_ERROR 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) raise NotImplementedError(expression, inp.type3, inp.operator)
if isinstance(inp, ourlang.FunctionCall): if isinstance(inp, ourlang.FunctionCall):

View File

@ -489,16 +489,6 @@ class OurVisitor:
if node.func.id in PRELUDE_METHODS: if node.func.id in PRELUDE_METHODS:
func = PRELUDE_METHODS[node.func.id] 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': elif node.func.id == 'foldl':
if 3 != len(node.args): if 3 != len(node.args):
_raise_static_error(node, f'Function {node.func.id} requires 3 arguments but {len(node.args)} are given') _raise_static_error(node, f'Function {node.func.id} requires 3 arguments but {len(node.args)} are given')

View File

@ -197,50 +197,6 @@ class TupleMatchConstraint(ConstraintBase):
raise NotImplementedError(exp_type) 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): class MustImplementTypeClassConstraint(ConstraintBase):
""" """
A type must implement a given type class A type must implement a given type class

View File

@ -12,7 +12,6 @@ from . import typeclasses as typeclasses
from . import types as type3types from . import types as type3types
from .constraints import ( from .constraints import (
CanBeSubscriptedConstraint, CanBeSubscriptedConstraint,
CastableConstraint,
ConstraintBase, ConstraintBase,
Context, Context,
LiteralFitsConstraint, LiteralFitsConstraint,
@ -50,11 +49,6 @@ def expression(ctx: Context, inp: ourlang.Expression) -> ConstraintGenerator:
return return
if isinstance(inp, ourlang.UnaryOp): 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) raise NotImplementedError(expression, inp, inp.operator)
if isinstance(inp, ourlang.BinaryOp) or isinstance(inp, ourlang.FunctionCall): if isinstance(inp, ourlang.BinaryOp) or isinstance(inp, ourlang.FunctionCall):

View File

@ -49,7 +49,7 @@ def testEntry(a: bytes, b: bytes) -> u8:
def test_foldl_3(): def test_foldl_3():
code_py = """ code_py = """
def xor(l: u32, r: u8) -> u32: def xor(l: u32, r: u8) -> u32:
return l ^ u32(r) return l ^ extend(r)
@exported @exported
def testEntry(a: bytes) -> u32: def testEntry(a: bytes) -> u32: