fib works \o/
This commit is contained in:
parent
b26efc797d
commit
9616d20460
@ -29,21 +29,36 @@ class Visitor:
|
|||||||
|
|
||||||
module = wasm.Module()
|
module = wasm.Module()
|
||||||
|
|
||||||
|
# Do a check first for all function definitions
|
||||||
|
# to get their types. Otherwise you cannot call
|
||||||
|
# a method that you haven't defined just yet,
|
||||||
|
# even if it is in the same file
|
||||||
|
function_body_map: Dict[ast.FunctionDef, wasm.Function] = {}
|
||||||
for stmt in node.body:
|
for stmt in node.body:
|
||||||
if isinstance(stmt, ast.FunctionDef):
|
if isinstance(stmt, ast.FunctionDef):
|
||||||
wnode = self.visit_FunctionDef(module, stmt)
|
wnode = self.pre_visit_FunctionDef(module, stmt)
|
||||||
|
|
||||||
if isinstance(wnode, wasm.Import):
|
if isinstance(wnode, wasm.Import):
|
||||||
module.imports.append(wnode)
|
module.imports.append(wnode)
|
||||||
else:
|
else:
|
||||||
module.functions.append(wnode)
|
module.functions.append(wnode)
|
||||||
|
function_body_map[stmt] = wnode
|
||||||
|
continue
|
||||||
|
|
||||||
|
# No other pre visits to do
|
||||||
|
|
||||||
|
for stmt in node.body:
|
||||||
|
if isinstance(stmt, ast.FunctionDef):
|
||||||
|
if stmt in function_body_map:
|
||||||
|
self.parse_FunctionDef_body(module, function_body_map[stmt], stmt)
|
||||||
|
# else: It's an import, no actual body to parse
|
||||||
continue
|
continue
|
||||||
|
|
||||||
raise NotImplementedError(stmt)
|
raise NotImplementedError(stmt)
|
||||||
|
|
||||||
return module
|
return module
|
||||||
|
|
||||||
def visit_FunctionDef(
|
def pre_visit_FunctionDef(
|
||||||
self,
|
self,
|
||||||
module: wasm.Module,
|
module: wasm.Module,
|
||||||
node: ast.FunctionDef,
|
node: ast.FunctionDef,
|
||||||
@ -55,6 +70,8 @@ class Visitor:
|
|||||||
|
|
||||||
Nested / dynamicly created functions are not yet supported
|
Nested / dynamicly created functions are not yet supported
|
||||||
"""
|
"""
|
||||||
|
del module
|
||||||
|
|
||||||
exported = False
|
exported = False
|
||||||
|
|
||||||
if node.decorator_list:
|
if node.decorator_list:
|
||||||
@ -99,7 +116,18 @@ class Visitor:
|
|||||||
]
|
]
|
||||||
]
|
]
|
||||||
|
|
||||||
wlocals: WLocals = dict(params)
|
return wasm.Function(node.name, exported, params, result, [])
|
||||||
|
|
||||||
|
def parse_FunctionDef_body(
|
||||||
|
self,
|
||||||
|
module: wasm.Module,
|
||||||
|
func: wasm.Function,
|
||||||
|
node: ast.FunctionDef,
|
||||||
|
) -> None:
|
||||||
|
"""
|
||||||
|
Parses the function body
|
||||||
|
"""
|
||||||
|
wlocals: WLocals = dict(func.params)
|
||||||
|
|
||||||
statements: List[wasm.Statement] = []
|
statements: List[wasm.Statement] = []
|
||||||
for py_stmt in node.body:
|
for py_stmt in node.body:
|
||||||
@ -107,7 +135,7 @@ class Visitor:
|
|||||||
self.visit_stmt(module, node, wlocals, py_stmt)
|
self.visit_stmt(module, node, wlocals, py_stmt)
|
||||||
)
|
)
|
||||||
|
|
||||||
return wasm.Function(node.name, exported, params, result, statements)
|
func.statements = statements
|
||||||
|
|
||||||
def visit_stmt(
|
def visit_stmt(
|
||||||
self,
|
self,
|
||||||
@ -250,6 +278,10 @@ class Visitor:
|
|||||||
yield wasm.Statement('{}.add'.format(exp_type))
|
yield wasm.Statement('{}.add'.format(exp_type))
|
||||||
return
|
return
|
||||||
|
|
||||||
|
if isinstance(node.op, ast.Sub):
|
||||||
|
yield wasm.Statement('{}.sub'.format(exp_type))
|
||||||
|
return
|
||||||
|
|
||||||
raise NotImplementedError(node.op)
|
raise NotImplementedError(node.op)
|
||||||
|
|
||||||
def visit_Compare(
|
def visit_Compare(
|
||||||
@ -274,6 +306,10 @@ class Visitor:
|
|||||||
yield wasm.Statement('i32.lt_s')
|
yield wasm.Statement('i32.lt_s')
|
||||||
return
|
return
|
||||||
|
|
||||||
|
if isinstance(node.ops[0], ast.Eq):
|
||||||
|
yield wasm.Statement('i32.eq')
|
||||||
|
return
|
||||||
|
|
||||||
if isinstance(node.ops[0], ast.Gt):
|
if isinstance(node.ops[0], ast.Gt):
|
||||||
yield wasm.Statement('i32.gt_s')
|
yield wasm.Statement('i32.gt_s')
|
||||||
return
|
return
|
||||||
|
|||||||
@ -21,7 +21,7 @@ def fib(n: i32) -> i32:
|
|||||||
return helper(n - 1, 0, 1)
|
return helper(n - 1, 0, 1)
|
||||||
|
|
||||||
@exported
|
@exported
|
||||||
def testEntry():
|
def testEntry() -> i32:
|
||||||
return fib(40)
|
return fib(40)
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user