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
4b427fb6
Commit
4b427fb6
authored
Feb 13, 2024
by
Tom Niget
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Start work on exprs
parent
2f8bdf7b
Changes
4
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
461 additions
and
157 deletions
+461
-157
typon/trans/tests/user_generics.py
typon/trans/tests/user_generics.py
+1
-1
typon/trans/transpiler/phases/emit_cpp/expr.py
typon/trans/transpiler/phases/emit_cpp/expr.py
+296
-0
typon/trans/transpiler/phases/emit_cpp/function.py
typon/trans/transpiler/phases/emit_cpp/function.py
+155
-156
typon/trans/transpiler/phases/emit_cpp/visitors.py
typon/trans/transpiler/phases/emit_cpp/visitors.py
+9
-0
No files found.
typon/trans/tests/user_generics.py
View file @
4b427fb6
...
...
@@ -23,4 +23,4 @@
if
__name__
==
"__main__"
:
print
(
5
)
\ No newline at end of file
print
(
"abc"
)
\ No newline at end of file
typon/trans/transpiler/phases/emit_cpp/expr.py
0 → 100644
View file @
4b427fb6
This diff is collapsed.
Click to expand it.
typon/trans/transpiler/phases/emit_cpp/function.py
View file @
4b427fb6
import
ast
from
dataclasses
import
dataclass
from
dataclasses
import
dataclass
,
field
from
typing
import
Iterable
,
Optional
from
transpiler.phases.emit_cpp.expr
import
ExpressionVisitor
from
transpiler.phases.typing.scope
import
Scope
from
transpiler.phases.emit_cpp.visitors
import
NodeVisitor
,
flatmap
from
transpiler.phases.emit_cpp.visitors
import
NodeVisitor
,
flatmap
,
CoroutineMode
from
transpiler.phases.typing.types
import
CallableInstanceType
,
BaseType
...
...
@@ -23,11 +23,10 @@ def emit_function(name: str, func: CallableInstanceType) -> Iterable[str]:
if
False
:
@
dataclass
class
BlockVisitor
(
NodeVisitor
):
@
dataclass
class
BlockVisitor
(
NodeVisitor
):
scope
:
Scope
#
generator: CoroutineMode = field(default=CoroutineMode.SYNC, kw_only=True)
generator
:
CoroutineMode
=
field
(
default
=
CoroutineMode
.
SYNC
,
kw_only
=
True
)
def
expr
(
self
)
->
ExpressionVisitor
:
return
ExpressionVisitor
(
self
.
scope
,
self
.
generator
)
...
...
@@ -38,34 +37,34 @@ if False:
# def visit_FunctionDef(self, node: ast.FunctionDef) -> Iterable[str]:
# yield from self.visit_free_func(node)
def
visit_free_func
(
self
,
node
:
ast
.
FunctionDef
,
emission
:
FunctionEmissionKind
)
->
Iterable
[
str
]:
if
getattr
(
node
,
"is_main"
,
False
):
if
emission
==
FunctionEmissionKind
.
DECLARATION
:
return
# Special case handling for Python's interesting way of defining an entry point.
# I mean, it's not *that* bad, it's just an attempt at retrofitting an "entry point" logic in a scripting
# language that, by essence, uses "the start of the file" as the implicit entry point, since files are
# read and executed line-by-line, contrary to usual structured languages that mark a distinction between
# declarations (functions, classes, modules, ...) and code.
# Also, for nitpickers, the C++ standard explicitly allows for omitting a `return` statement in the `main`.
# 0 is returned by default.
yield
"typon::Root root() const"
def
block
():
yield
from
node
.
body
yield
ast
.
Return
()
from
transpiler.phases.emit_cpp.function
import
FunctionVisitor
yield
"{"
yield
from
self
.
visit_func_decls
(
block
(),
node
.
scope
,
CoroutineMode
.
TASK
)
yield
"}"
return
if
emission
==
FunctionEmissionKind
.
DECLARATION
:
yield
f"struct
{
node
.
name
}
_inner {{"
yield
from
self
.
visit_func_new
(
node
,
emission
)
if
emission
==
FunctionEmissionKind
.
DECLARATION
:
yield
f"}}
{
node
.
name
}
;"
#
def visit_free_func(self, node: ast.FunctionDef, emission: FunctionEmissionKind) -> Iterable[str]:
#
if getattr(node, "is_main", False):
#
if emission == FunctionEmissionKind.DECLARATION:
#
return
#
# Special case handling for Python's interesting way of defining an entry point.
#
# I mean, it's not *that* bad, it's just an attempt at retrofitting an "entry point" logic in a scripting
#
# language that, by essence, uses "the start of the file" as the implicit entry point, since files are
#
# read and executed line-by-line, contrary to usual structured languages that mark a distinction between
#
# declarations (functions, classes, modules, ...) and code.
#
# Also, for nitpickers, the C++ standard explicitly allows for omitting a `return` statement in the `main`.
#
# 0 is returned by default.
#
yield "typon::Root root() const"
#
#
def block():
#
yield from node.body
#
yield ast.Return()
#
#
from transpiler.phases.emit_cpp.function import FunctionVisitor
#
yield "{"
#
yield from self.visit_func_decls(block(), node.scope, CoroutineMode.TASK)
#
yield "}"
#
return
#
#
if emission == FunctionEmissionKind.DECLARATION:
#
yield f"struct {node.name}_inner {{"
#
yield from self.visit_func_new(node, emission)
#
if emission == FunctionEmissionKind.DECLARATION:
#
yield f"}} {node.name};"
def
visit_func_decls
(
self
,
body
:
list
[
ast
.
stmt
],
inner_scope
:
Scope
,
mode
=
CoroutineMode
.
ASYNC
)
->
Iterable
[
str
]:
for
child
in
body
:
...
...
typon/trans/transpiler/phases/emit_cpp/visitors.py
View file @
4b427fb6
import
ast
from
enum
import
Flag
from
itertools
import
chain
from
typing
import
Iterable
...
...
@@ -89,3 +90,11 @@ def join(sep: str, items: Iterable[Iterable[str]]) -> Iterable[str]:
def
flatmap
(
f
,
items
):
return
chain
.
from_iterable
(
map
(
f
,
items
))
class
CoroutineMode
(
Flag
):
SYNC
=
1
FAKE
=
2
|
SYNC
ASYNC
=
4
GENERATOR
=
8
|
ASYNC
TASK
=
16
|
ASYNC
JOIN
=
32
|
ASYNC
\ No newline at end of file
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