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
7eb96252
Commit
7eb96252
authored
Nov 03, 2023
by
Tom Niget
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add initial support for user types in Python
parent
0fa05177
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
28 additions
and
4 deletions
+28
-4
typon/trans/tests/pyext.post.py
typon/trans/tests/pyext.post.py
+1
-1
typon/trans/tests/pyext.py
typon/trans/tests/pyext.py
+11
-2
typon/trans/transpiler/phases/emit_cpp/module.py
typon/trans/transpiler/phases/emit_cpp/module.py
+16
-1
No files found.
typon/trans/tests/pyext.post.py
View file @
7eb96252
import
pyext
import
pyext
print
(
"Imported:"
,
pyext
.
add
(
5
,
3
),
pyext
.
fibo
(
10
),
pyext
.
squares
())
print
(
"Imported:"
,
pyext
.
add
(
5
,
3
),
pyext
.
fibo
(
10
),
pyext
.
squares
(),
pyext
.
Person
(
"jean"
,
123
))
\ No newline at end of file
\ No newline at end of file
typon/trans/tests/pyext.py
View file @
7eb96252
...
@@ -2,8 +2,17 @@
...
@@ -2,8 +2,17 @@
# extension
# extension
import
numpy
as
np
import
numpy
as
np
from
dataclasses
import
dataclass
def
add
(
x
,
y
)
->
int
:
@
dataclass
class
Person
:
name
:
str
age
:
int
def
afficher
(
self
):
print
(
self
.
name
,
self
.
age
)
def
add
(
x
,
y
):
return
x
+
y
return
x
+
y
def
fibo
(
n
):
def
fibo
(
n
):
...
@@ -16,4 +25,4 @@ def squares() -> list[int]:
...
@@ -16,4 +25,4 @@ def squares() -> list[int]:
return
np
.
square
([
x
for
x
in
range
(
5
)])
return
np
.
square
([
x
for
x
in
range
(
5
)])
if
__name__
==
"__main__"
:
if
__name__
==
"__main__"
:
print
(
"Python:"
,
add
(
5
,
3
),
fibo
(
10
),
squares
())
print
(
"Python:"
,
add
(
5
,
3
),
fibo
(
10
),
squares
(),
Person
(
"jean"
,
123
))
\ No newline at end of file
\ No newline at end of file
typon/trans/transpiler/phases/emit_cpp/module.py
View file @
7eb96252
...
@@ -59,7 +59,7 @@ class ModuleVisitor(BlockVisitor):
...
@@ -59,7 +59,7 @@ class ModuleVisitor(BlockVisitor):
yield
f"> arg
{
i
}
"
yield
f"> arg
{
i
}
"
yield
") {"
yield
") {"
yield
"
SubInterpreter lock
{};"
yield
"
InterpGuard guard
{};"
yield
f"return py::module_::import(
\
"
{
mod
}\
"
).attr(
\
"
{
name
}\
"
)("
yield
f"return py::module_::import(
\
"
{
mod
}\
"
).attr(
\
"
{
name
}\
"
)("
for
i
,
argty
in
enumerate
(
fty
.
parameters
):
for
i
,
argty
in
enumerate
(
fty
.
parameters
):
if
i
!=
0
:
if
i
!=
0
:
...
@@ -122,6 +122,21 @@ class ModuleVisitorExt(NodeVisitor):
...
@@ -122,6 +122,21 @@ class ModuleVisitorExt(NodeVisitor):
#yield f'm.def("{node.name}", CoroWrapper(PROGRAMNS::{node.name}));'
#yield f'm.def("{node.name}", CoroWrapper(PROGRAMNS::{node.name}));'
yield
f'm.def("
{
node
.
name
}
", PROGRAMNS::
{
node
.
name
}
);'
yield
f'm.def("
{
node
.
name
}
", PROGRAMNS::
{
node
.
name
}
);'
def
visit_ClassDef
(
self
,
node
:
ast
.
ClassDef
)
->
Iterable
[
str
]:
yield
f"py::class_<PROGRAMNS::
{
node
.
name
}
_s::py_type>(m,
\
"
{
node
.
name
}\
"
)"
init
=
node
.
type
.
fields
[
"__init__"
].
type
.
resolve
().
remove_self
()
init_params
=
init
.
parameters
yield
".def(py::init<"
for
i
,
argty
in
enumerate
(
init_params
):
if
i
!=
0
:
yield
", "
yield
from
self
.
visit
(
argty
)
yield
">())"
yield
f'.def("__repr__", [](const PROGRAMNS::
{
node
.
name
}
_s::py_type& self)'
yield
"{ return repr(self); })"
yield
";"
pass
def
visit_AST
(
self
,
node
:
ast
.
AST
)
->
Iterable
[
str
]:
def
visit_AST
(
self
,
node
:
ast
.
AST
)
->
Iterable
[
str
]:
yield
from
()
yield
from
()
pass
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