Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
T
typon-compiler
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
typon
typon-compiler
Commits
02310e9a
Commit
02310e9a
authored
Apr 09, 2023
by
Tom Niget
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add support for future()
parent
1afe2cb5
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
38 additions
and
18 deletions
+38
-18
trans/tests/naive_fibonacci_future.py
trans/tests/naive_fibonacci_future.py
+12
-0
trans/transpiler/visitors/block.py
trans/transpiler/visitors/block.py
+1
-1
trans/transpiler/visitors/expr.py
trans/transpiler/visitors/expr.py
+20
-17
trans/typon/__init__.py
trans/typon/__init__.py
+5
-0
No files found.
trans/tests/naive_fibonacci_future.py
0 → 100644
View file @
02310e9a
from
typon
import
future
def
fibo
(
n
:
int
)
->
int
:
if
n
<
2
:
return
n
a
=
future
(
lambda
:
fibo
(
n
-
1
))
b
=
future
(
lambda
:
fibo
(
n
-
2
))
return
a
+
b
if
__name__
==
"__main__"
:
print
(
fibo
(
30
))
# should display 832040
\ No newline at end of file
trans/transpiler/visitors/block.py
View file @
02310e9a
...
...
@@ -34,7 +34,7 @@ class BlockVisitor(NodeVisitor):
def
visit_Call
(
self
,
node
:
ast
.
Call
):
func
=
node
.
func
if
compare_ast
(
func
,
ast
.
parse
(
'fork'
,
mode
=
"eval"
).
body
):
if
compare_ast
(
func
,
ast
.
parse
(
"fork"
,
mode
=
"eval"
).
body
):
yield
CoroutineMode
.
JOIN
yield
from
()
...
...
trans/transpiler/visitors/expr.py
View file @
02310e9a
...
...
@@ -74,11 +74,13 @@ class ExpressionVisitor(NodeVisitor):
def
visit_Name
(
self
,
node
:
ast
.
Name
)
->
Iterable
[
str
]:
res
=
self
.
fix_name
(
node
.
id
)
if
(
decl
:
=
self
.
scope
.
get
(
res
)
):
if
decl
:
=
self
.
scope
.
get
(
res
):
if
decl
.
kind
==
VarKind
.
SELF
:
res
=
"(*this)"
elif
decl
.
future
and
CoroutineMode
.
ASYNC
in
self
.
generator
:
res
=
f"
{
res
}
.get()"
if
decl
.
future
==
"future"
:
res
=
"co_await "
+
res
yield
res
def
visit_Compare
(
self
,
node
:
ast
.
Compare
)
->
Iterable
[
str
]:
...
...
@@ -98,23 +100,24 @@ class ExpressionVisitor(NodeVisitor):
if
getattr
(
node
,
"kwargs"
,
None
):
raise
NotImplementedError
(
node
,
"kwargs"
)
func
=
node
.
func
if
compare_ast
(
func
,
ast
.
parse
(
'fork'
,
mode
=
"eval"
).
body
):
assert
len
(
node
.
args
)
==
1
arg
=
node
.
args
[
0
]
assert
isinstance
(
arg
,
ast
.
Lambda
)
node
.
is_future
=
True
vis
=
self
.
reset
()
vis
.
generator
=
CoroutineMode
.
SYNC
# todo: bad code
if
CoroutineMode
.
ASYNC
in
self
.
generator
:
yield
"co_await typon::fork("
yield
from
vis
.
visit
(
arg
.
body
)
yield
")"
for
name
in
(
"fork"
,
"future"
):
if
compare_ast
(
func
,
ast
.
parse
(
name
,
mode
=
"eval"
).
body
):
assert
len
(
node
.
args
)
==
1
arg
=
node
.
args
[
0
]
assert
isinstance
(
arg
,
ast
.
Lambda
)
node
.
is_future
=
name
vis
=
self
.
reset
()
vis
.
generator
=
CoroutineMode
.
SYNC
# todo: bad code
if
CoroutineMode
.
ASYNC
in
self
.
generator
:
yield
f"co_await typon::
{
name
}
("
yield
from
vis
.
visit
(
arg
.
body
)
yield
")"
return
elif
CoroutineMode
.
FAKE
in
self
.
generator
:
yield
from
self
.
visit
(
arg
.
body
)
return
elif
CoroutineMode
.
FAKE
in
self
.
generator
:
yield
from
self
.
visit
(
arg
.
body
)
return
elif
compare_ast
(
func
,
ast
.
parse
(
'sync'
,
mode
=
"eval"
).
body
):
if
compare_ast
(
func
,
ast
.
parse
(
'sync'
,
mode
=
"eval"
).
body
):
if
CoroutineMode
.
ASYNC
in
self
.
generator
:
yield
"co_await typon::Sync()"
elif
CoroutineMode
.
FAKE
in
self
.
generator
:
...
...
trans/typon/__init__.py
View file @
02310e9a
...
...
@@ -8,6 +8,11 @@ def fork(f: Callable[[], T]) -> T:
return
f
()
def
future
(
f
:
Callable
[[],
T
])
->
T
:
# stub
return
f
()
def
sync
():
# stub
pass
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment