Commit 19d31953 authored by Tom Niget's avatar Tom Niget

Futures work

parent 4de648a2
from typon import future
def fibo(n): def fibo(n):
if n < 2: if n < 2:
return n return n
......
...@@ -5,7 +5,7 @@ from typing import Iterable ...@@ -5,7 +5,7 @@ from typing import Iterable
from transpiler.phases.emit_cpp.visitors import NodeVisitor, CoroutineMode, join from transpiler.phases.emit_cpp.visitors import NodeVisitor, CoroutineMode, join
from transpiler.phases.typing.scope import Scope from transpiler.phases.typing.scope import Scope
from transpiler.phases.typing.types import ClassTypeType, TupleInstanceType from transpiler.phases.typing.types import ClassTypeType, TupleInstanceType, TY_FUTURE, ResolvedConcreteType
from transpiler.phases.utils import make_lnd from transpiler.phases.utils import make_lnd
from transpiler.utils import linenodata from transpiler.utils import linenodata
...@@ -122,12 +122,12 @@ class ExpressionVisitor(NodeVisitor): ...@@ -122,12 +122,12 @@ class ExpressionVisitor(NodeVisitor):
yield ")" yield ")"
def visit_Call(self, node: ast.Call) -> Iterable[str]: def visit_Call(self, node: ast.Call) -> Iterable[str]:
if isinstance(node.func, ast.Name) and node.func.id == "fork": if isinstance(node.func, ast.Name) and node.func.id in ("fork", "future"):
assert len(node.args) == 1 assert len(node.args) == 1
arg = node.args[0] arg = node.args[0]
assert isinstance(arg, ast.Lambda) assert isinstance(arg, ast.Lambda)
if self.generator != CoroutineMode.SYNC: if self.generator != CoroutineMode.SYNC:
yield "co_await typon::fork(" yield f"co_await typon::{node.func.id}("
assert isinstance(arg.body, ast.Call) assert isinstance(arg.body, ast.Call)
yield from self.visit(arg.body.func) yield from self.visit(arg.body.func)
yield "(" yield "("
...@@ -138,12 +138,29 @@ class ExpressionVisitor(NodeVisitor): ...@@ -138,12 +138,29 @@ class ExpressionVisitor(NodeVisitor):
yield from self.visit(arg.body) yield from self.visit(arg.body)
return return
# async : co_await f(args) # async : co_await f(args)
# sync : call_sync(f, args) # sync : call_sync(f, args)
if self.generator != CoroutineMode.SYNC: if self.generator != CoroutineMode.SYNC:
nty = node.type.resolve()
if not (isinstance(nty, ResolvedConcreteType) and nty.inherits(TY_FUTURE)):
yield "co_await" yield "co_await"
else: else:
yield "call_sync" yield "call_sync"
if isinstance(node.func, ast.Attribute) and node.func.attr == "get" and node.func.value.type.inherits(TY_FUTURE):
yield "("
if self.generator == CoroutineMode.SYNC:
yield from self.visit(node.func.value)
else:
yield "("
yield from self.visit(node.func.value)
yield ").get()"
yield ")"
return
yield "(" yield "("
yield from self.visit(node.func) yield from self.visit(node.func)
yield ")(" yield ")("
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment