diff --git a/py2wasm/python.py b/py2wasm/python.py index 6f5d36b..96062e7 100644 --- a/py2wasm/python.py +++ b/py2wasm/python.py @@ -124,10 +124,8 @@ class Visitor: if not isinstance(arg.annotation, ast.Name): raise NotImplementedError - print(class_lookup) - print(arg.annotation.id) if arg.annotation.id in class_lookup: - params.append((arg.arg, arg.annotation.id, )) + params.append((arg.arg, 'i32', )) else: params.append((arg.arg, _parse_annotation(arg.annotation), )) @@ -296,7 +294,7 @@ class Visitor: return self.visit_Name(wlocals, exp_type, node) if isinstance(node, ast.Attribute): - return [] # TODO + return self.visit_Attribute(wlocals, exp_type, node) raise NotImplementedError(node) @@ -411,14 +409,46 @@ class Visitor: 'Could not find function {}'.format(node.func.id) if isinstance(called_func_list[0], wasm.Class): - called_params = [ - (x.name, x.type, ) - for x in called_func_list[0].members - ] - called_result: Optional[str] = called_func_list[0].name - else: - called_params = called_func_list[0].params - called_result = called_func_list[0].result + return self.visit_Call_class(module, wlocals, exp_type, node, called_func_list[0]) + + return self.visit_Call_func(module, wlocals, exp_type, node, called_func_list[0]) + + def visit_Call_class( + self, + module: wasm.Module, + wlocals: WLocals, + exp_type: str, + node: ast.Call, + func: wasm.Class, + ) -> StatementGenerator: + """ + Visits a Call node as (part of) an expression + + This instantiates the class + """ + + # TODO: malloc call + + yield wasm.Statement('i32.const 0') + yield wasm.Statement('i32.load') + + + def visit_Call_func( + self, + module: wasm.Module, + wlocals: WLocals, + exp_type: str, + node: ast.Call, + func: Union[wasm.Function, wasm.Import], + ) -> StatementGenerator: + """ + Visits a Call node as (part of) an expression + """ + assert isinstance(node.func, ast.Name) + + called_name = node.func.id + called_params = func.params + called_result = func.result assert exp_type == called_result @@ -470,6 +500,20 @@ class Visitor: raise NotImplementedError(exp_type) + def visit_Attribute(self, wlocals: WLocals, exp_type: str, node: ast.Attribute) -> StatementGenerator: + """ + Visits an Attribute node as (part of) an expression + """ + + if not isinstance(node.value, ast.Name): + raise NotImplementedError + + if not isinstance(node.ctx, ast.Load): + raise NotImplementedError + + yield wasm.Statement('local.get', '$' + node.value.id) + yield wasm.Statement(exp_type + '.load', 'offset=0') # TODO: Calculate offset based on set struct + def visit_Name(self, wlocals: WLocals, exp_type: str, node: ast.Name) -> StatementGenerator: """ Visits a Name node as (part of) an expression diff --git a/py2wasm/wasm.py b/py2wasm/wasm.py index 68300fc..70694bd 100644 --- a/py2wasm/wasm.py +++ b/py2wasm/wasm.py @@ -120,7 +120,8 @@ class Module: """ Generates the text version """ - return '(module\n {}\n {})\n'.format( + return '(module\n (memory 1)\n (data (memory 0) (i32.const 0) {})\n {}\n {})\n'.format( + '"\\\\04\\\\00\\\\00\\\\00"', '\n '.join(x.generate() for x in self.imports), '\n '.join(x.generate() for x in self.functions), )