From 4876be77fd910fe3bf3df487680d91bbc26a7f20 Mon Sep 17 00:00:00 2001 From: "Johan B.W. de Vries" Date: Mon, 26 Dec 2022 13:20:15 +0100 Subject: [PATCH] Cleanup type comments, old generator file --- phasm/parser.py | 74 ++--------------------------------------------- phasm/wasmeasy.py | 69 ------------------------------------------- 2 files changed, 2 insertions(+), 141 deletions(-) delete mode 100644 phasm/wasmeasy.py diff --git a/phasm/parser.py b/phasm/parser.py index fd06510..e4cc56f 100644 --- a/phasm/parser.py +++ b/phasm/parser.py @@ -259,64 +259,7 @@ class OurVisitor: data_block, ) - raise NotImplementedError('TODO: Broken after new typing system') - - # if isinstance(exp_type, TypeTuple): - # if not isinstance(node.value, ast.Tuple): - # _raise_static_error(node, 'Must be tuple') - # - # if len(exp_type.members) != len(node.value.elts): - # _raise_static_error(node, 'Invalid number of tuple values') - # - # tuple_data = [ - # self.visit_Module_Constant(module, arg_node) - # for arg_node, mem in zip(node.value.elts, exp_type.members) - # if isinstance(arg_node, ast.Constant) - # ] - # if len(exp_type.members) != len(tuple_data): - # _raise_static_error(node, 'Tuple arguments must be constants') - # - # # Allocate the data - # data_block = ModuleDataBlock(tuple_data) - # module.data.blocks.append(data_block) - # - # # Then return the constant as a pointer - # return ModuleConstantDef( - # node.target.id, - # node.lineno, - # exp_type, - # ConstantTuple(tuple_data), - # data_block, - # ) - # - # if isinstance(exp_type, TypeStaticArray): - # if not isinstance(node.value, ast.Tuple): - # _raise_static_error(node, 'Must be static array') - # - # if len(exp_type.members) != len(node.value.elts): - # _raise_static_error(node, 'Invalid number of static array values') - # - # static_array_data = [ - # self.visit_Module_Constant(module, arg_node) - # for arg_node in node.value.elts - # if isinstance(arg_node, ast.Constant) - # ] - # if len(exp_type.members) != len(static_array_data): - # _raise_static_error(node, 'Static array arguments must be constants') - # - # # Allocate the data - # data_block = ModuleDataBlock(static_array_data) - # module.data.blocks.append(data_block) - # - # # Then return the constant as a pointer - # return ModuleConstantDef( - # node.target.id, - # node.lineno, - # ConstantStaticArray(static_array_data), - # data_block, - # ) - # - # raise NotImplementedError(f'{node} on Module AnnAssign') + raise NotImplementedError(f'{node} on Module AnnAssign') def visit_Module_stmt(self, module: Module, node: ast.stmt) -> None: if isinstance(node, ast.FunctionDef): @@ -399,9 +342,6 @@ class OurVisitor: else: raise NotImplementedError(f'Operator {node.op}') - # Assume the type doesn't change when descending into a binary operator - # e.g. you can do `"hello" * 3` with the code below (yet) - return BinaryOp( operator, self.visit_Module_FunctionDef_expr(module, function, our_locals, node.left), @@ -434,9 +374,6 @@ class OurVisitor: else: raise NotImplementedError(f'Operator {node.ops}') - # Assume the type doesn't change when descending into a binary operator - # e.g. you can do `"hello" * 3` with the code below (yet) - return BinaryOp( operator, self.visit_Module_FunctionDef_expr(module, function, our_locals, node.left), @@ -528,8 +465,6 @@ class OurVisitor: if 1 != len(node.args): _raise_static_error(node, f'Function {node.func.id} requires 1 arguments but {len(node.args)} are given') - # FIXME: This is a stub, proper casting is todo - return UnaryOp( 'cast', self.visit_Module_FunctionDef_expr(module, function, our_locals, node.args[0]), @@ -543,15 +478,10 @@ class OurVisitor: self.visit_Module_FunctionDef_expr(module, function, our_locals, node.args[0]), ) elif node.func.id == 'foldl': - # TODO: This should a much more generic function! - # For development purposes, we're assuming you're doing a foldl(Callable[[u8, u8], u8], u8, bytes) - # In the future, we should probably infer the type of the second argument, - # and use it as expected types for the other u8s and the Iterable[u8] (i.e. bytes) - if 3 != len(node.args): _raise_static_error(node, f'Function {node.func.id} requires 3 arguments but {len(node.args)} are given') - # TODO: This is not generic + # TODO: This is not generic, you cannot return a function subnode = node.args[0] if not isinstance(subnode, ast.Name): raise NotImplementedError(f'Calling methods that are not a name {subnode}') diff --git a/phasm/wasmeasy.py b/phasm/wasmeasy.py deleted file mode 100644 index 01c5d0c..0000000 --- a/phasm/wasmeasy.py +++ /dev/null @@ -1,69 +0,0 @@ -""" -Helper functions to quickly generate WASM code -""" -from typing import List, Optional - -import functools - -from . import wasm - -#pylint: disable=C0103,C0115,C0116,R0201,R0902 - -class Prefix_inn_fnn: - def __init__(self, prefix: str) -> None: - self.prefix = prefix - - # 6.5.5. Memory Instructions - self.load = functools.partial(wasm.Statement, f'{self.prefix}.load') - self.store = functools.partial(wasm.Statement, f'{self.prefix}.store') - - # 6.5.6. Numeric Instructions - self.clz = functools.partial(wasm.Statement, f'{self.prefix}.clz') - self.ctz = functools.partial(wasm.Statement, f'{self.prefix}.ctz') - self.popcnt = functools.partial(wasm.Statement, f'{self.prefix}.popcnt') - self.add = functools.partial(wasm.Statement, f'{self.prefix}.add') - self.sub = functools.partial(wasm.Statement, f'{self.prefix}.sub') - self.mul = functools.partial(wasm.Statement, f'{self.prefix}.mul') - self.div_s = functools.partial(wasm.Statement, f'{self.prefix}.div_s') - self.div_u = functools.partial(wasm.Statement, f'{self.prefix}.div_u') - self.rem_s = functools.partial(wasm.Statement, f'{self.prefix}.rem_s') - self.rem_u = functools.partial(wasm.Statement, f'{self.prefix}.rem_u') - self.and_ = functools.partial(wasm.Statement, f'{self.prefix}.and') - self.or_ = functools.partial(wasm.Statement, f'{self.prefix}.or') - self.xor = functools.partial(wasm.Statement, f'{self.prefix}.xor') - self.shl = functools.partial(wasm.Statement, f'{self.prefix}.shl') - self.shr_s = functools.partial(wasm.Statement, f'{self.prefix}.shr_s') - self.shr_u = functools.partial(wasm.Statement, f'{self.prefix}.shr_u') - self.rotl = functools.partial(wasm.Statement, f'{self.prefix}.rotl') - self.rotr = functools.partial(wasm.Statement, f'{self.prefix}.rotr') - - self.eqz = functools.partial(wasm.Statement, f'{self.prefix}.eqz') - self.eq = functools.partial(wasm.Statement, f'{self.prefix}.eq') - self.ne = functools.partial(wasm.Statement, f'{self.prefix}.ne') - self.lt_s = functools.partial(wasm.Statement, f'{self.prefix}.lt_s') - self.lt_u = functools.partial(wasm.Statement, f'{self.prefix}.lt_u') - self.gt_s = functools.partial(wasm.Statement, f'{self.prefix}.gt_s') - self.gt_u = functools.partial(wasm.Statement, f'{self.prefix}.gt_u') - self.le_s = functools.partial(wasm.Statement, f'{self.prefix}.le_s') - self.le_u = functools.partial(wasm.Statement, f'{self.prefix}.le_u') - self.ge_s = functools.partial(wasm.Statement, f'{self.prefix}.ge_s') - self.ge_u = functools.partial(wasm.Statement, f'{self.prefix}.ge_u') - - def const(self, value: int, comment: Optional[str] = None) -> wasm.Statement: - return wasm.Statement(f'{self.prefix}.const', f'0x{value:08x}', comment=comment) - -i32 = Prefix_inn_fnn('i32') -i64 = Prefix_inn_fnn('i64') - -class Block: - def __init__(self, start: str) -> None: - self.start = start - - def __call__(self, *statements: wasm.Statement) -> List[wasm.Statement]: - return [ - wasm.Statement('if'), - *statements, - wasm.Statement('end'), - ] - -if_ = Block('if')