more ideas [skip-ci]
This commit is contained in:
parent
58bd5f0889
commit
07aeb52560
@ -72,7 +72,37 @@ class Visitor:
|
|||||||
|
|
||||||
return wasm.Import(module, name, intname, _parse_import_args(node.args))
|
return wasm.Import(module, name, intname, _parse_import_args(node.args))
|
||||||
|
|
||||||
return wasm.Function(node.name, exported)
|
result = _parse_annotation(node.returns)
|
||||||
|
|
||||||
|
statements = [
|
||||||
|
self.visit_stmt(node, stmt)
|
||||||
|
for stmt in node.body
|
||||||
|
]
|
||||||
|
|
||||||
|
return wasm.Function(node.name, exported, result, statements)
|
||||||
|
|
||||||
|
def visit_stmt(self, func: ast.FunctionDef, stmt: ast.stmt) -> wasm.Statement:
|
||||||
|
"""
|
||||||
|
Visits a statement node
|
||||||
|
"""
|
||||||
|
if isinstance(stmt, ast.Return):
|
||||||
|
return self.visit_Return(func, stmt)
|
||||||
|
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
def visit_Return(self, func: ast.FunctionDef, stmt: ast.Return) -> wasm.Statement:
|
||||||
|
"""
|
||||||
|
Visits a statement node
|
||||||
|
"""
|
||||||
|
assert isinstance(stmt.value, ast.Constant)
|
||||||
|
assert isinstance(stmt.value.value, int)
|
||||||
|
|
||||||
|
return_type = _parse_annotation(func.returns)
|
||||||
|
|
||||||
|
return wasm.Statement(
|
||||||
|
'{}.const'.format(return_type),
|
||||||
|
str(stmt.value.value)
|
||||||
|
)
|
||||||
|
|
||||||
def _parse_import_decorator(func_name: str, args: List[ast.expr]) -> Tuple[str, str, str]:
|
def _parse_import_decorator(func_name: str, args: List[ast.expr]) -> Tuple[str, str, str]:
|
||||||
"""
|
"""
|
||||||
|
|||||||
@ -2,7 +2,7 @@
|
|||||||
Python classes for storing the representation of Web Assembly code
|
Python classes for storing the representation of Web Assembly code
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from typing import Iterable, List, Tuple
|
from typing import Iterable, List, Optional, Tuple
|
||||||
|
|
||||||
class Import:
|
class Import:
|
||||||
"""
|
"""
|
||||||
@ -49,12 +49,18 @@ class Function:
|
|||||||
"""
|
"""
|
||||||
Represents a Web Assembly function
|
Represents a Web Assembly function
|
||||||
"""
|
"""
|
||||||
def __init__(self, name: str, exported: bool = True) -> None:
|
def __init__(
|
||||||
|
self,
|
||||||
|
name: str,
|
||||||
|
exported: bool,
|
||||||
|
result: Optional[str],
|
||||||
|
statements: Iterable[Statement],
|
||||||
|
) -> None:
|
||||||
self.name = name
|
self.name = name
|
||||||
self.exported = exported
|
self.exported = exported
|
||||||
self.params: List[Tuple[str, str]] = []
|
self.params: List[Tuple[str, str]] = []
|
||||||
self.returns = None
|
self.result = result
|
||||||
self.statements: List[Statement] = []
|
self.statements = [*statements]
|
||||||
|
|
||||||
def generate(self) -> str:
|
def generate(self) -> str:
|
||||||
"""
|
"""
|
||||||
@ -65,6 +71,9 @@ class Function:
|
|||||||
for nam, typ in self.params:
|
for nam, typ in self.params:
|
||||||
header += ' (param ${} {})'.format(nam, typ)
|
header += ' (param ${} {})'.format(nam, typ)
|
||||||
|
|
||||||
|
if self.result:
|
||||||
|
header += ' (result {})'.format(self.result)
|
||||||
|
|
||||||
return '(func {}\n {})'.format(
|
return '(func {}\n {})'.format(
|
||||||
header,
|
header,
|
||||||
'\n '.join(x.generate() for x in self.statements),
|
'\n '.join(x.generate() for x in self.statements),
|
||||||
|
|||||||
13
tests/integration/test_simple.py
Normal file
13
tests/integration/test_simple.py
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
from .helpers import Suite
|
||||||
|
|
||||||
|
def test_return():
|
||||||
|
code_py = """
|
||||||
|
@exported
|
||||||
|
def testEntry() -> i32:
|
||||||
|
return 13
|
||||||
|
"""
|
||||||
|
|
||||||
|
result = Suite(code_py, 'test_fib').run_code()
|
||||||
|
|
||||||
|
assert 13 is result.returned_value
|
||||||
|
assert [] == result.log_int32_list
|
||||||
Loading…
x
Reference in New Issue
Block a user