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
23be3256
Commit
23be3256
authored
Jan 15, 2024
by
Tom Niget
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Migrate class and method emission to new referencemodel
parent
316e4d8b
Changes
9
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
1339 additions
and
20 deletions
+1339
-20
typon/include/python/basedef.hpp
typon/include/python/basedef.hpp
+4
-1
typon/include/python/builtins.hpp
typon/include/python/builtins.hpp
+3
-3
typon/include/python/referencemodel.hpp
typon/include/python/referencemodel.hpp
+1206
-0
typon/trans/tests/usertype.py
typon/trans/tests/usertype.py
+6
-5
typon/trans/transpiler/phases/emit_cpp/__init__.py
typon/trans/transpiler/phases/emit_cpp/__init__.py
+6
-5
typon/trans/transpiler/phases/emit_cpp/class_.py
typon/trans/transpiler/phases/emit_cpp/class_.py
+27
-1
typon/trans/transpiler/phases/emit_cpp/expr.py
typon/trans/transpiler/phases/emit_cpp/expr.py
+2
-0
typon/trans/transpiler/phases/emit_cpp/file.py
typon/trans/transpiler/phases/emit_cpp/file.py
+20
-3
typon/trans/transpiler/phases/emit_cpp/module.py
typon/trans/transpiler/phases/emit_cpp/module.py
+65
-2
No files found.
typon/include/python/basedef.hpp
View file @
23be3256
...
...
@@ -20,7 +20,7 @@ public:
return
sync_wrapper
(
std
::
forward
<
Args
>
(
args
)...);
}
};
/*
struct method {};
template <typename Func, typename Self>
...
...
@@ -52,5 +52,8 @@ auto dot_bind(Obj, Attr attr) {
#define dotp(OBJ, NAME) [](auto && obj) -> auto { return dot_bind(obj, obj->NAME); }(OBJ)
#define dots(OBJ, NAME) [](auto && obj) -> auto { return std::remove_reference<decltype(obj)>::type::py_type::NAME; }(OBJ)
*/
#include "referencemodel.hpp"
#endif // TYPON_BASEDEF_HPP
typon/include/python/builtins.hpp
View file @
23be3256
...
...
@@ -25,17 +25,17 @@
#define _Args(...) __VA_ARGS__
#define COMMA() ,
#define METHOD(ret, name, args, ...) \
static constexpr struct name##_s : method { \
static constexpr struct name##_s :
referencemodel::
method { \
template <typename Self> ret operator() args const __VA_ARGS__ \
} name{};
#define METHOD_GEN(gen, ret, name, args, ...) \
static constexpr struct name##_s : method { \
static constexpr struct name##_s :
referencemodel::
method { \
template <typename Self, _Args gen> ret operator() args const __VA_ARGS__ \
} name{};
#define FUNCTION(ret, name, args, ...) \
struct { \
struct
: referencemodel::staticmethod
{ \
ret operator() args const __VA_ARGS__ \
} static constexpr name{};
...
...
typon/include/python/referencemodel.hpp
0 → 100644
View file @
23be3256
This diff is collapsed.
Click to expand it.
typon/trans/tests/usertype.py
View file @
23be3256
# coding: utf-8
# norun
# https://lab.nexedi.com/xavier_thompson/typon-snippets/tree/master/references
class
Person
:
name
:
str
...
...
@@ -12,14 +12,15 @@ class Person:
def
afficher
(
self
):
print
(
self
.
name
,
self
.
age
)
def
creer
():
return
Person
(
"jean"
,
123
)
#
def creer():
#
return Person("jean", 123)
if
__name__
==
"__main__"
:
y
=
Person
x
=
creer
()
#x = creer()
x
=
Person
(
"jean"
,
123
)
print
(
x
.
name
)
print
(
x
.
age
)
x
.
afficher
()
y
.
afficher
(
x
)
#
y.afficher(x)
typon/trans/transpiler/phases/emit_cpp/__init__.py
View file @
23be3256
...
...
@@ -67,11 +67,12 @@ class NodeVisitor(UniversalVisitor):
elif
node
is
TY_STR
:
yield
"PyStr"
elif
isinstance
(
node
,
UserType
):
if
node
.
is_reference
:
yield
"PyObj<"
yield
f"decltype(
{
node
.
name
}
)"
if
node
.
is_reference
:
yield
"::py_type>"
# if node.is_reference:
# yield "PyObj<"
#yield "auto"
yield
f"referencemodel::Rc<decltype(__main__::
{
node
.
name
}
)::Obj>"
# if node.is_reference:
# yield "::py_type>"
elif
isinstance
(
node
,
TypeType
):
yield
"auto"
# TODO
elif
isinstance
(
node
,
FunctionType
):
...
...
typon/trans/transpiler/phases/emit_cpp/class_.py
View file @
23be3256
...
...
@@ -84,7 +84,7 @@ class ClassInnerVisitor(NodeVisitor):
# from transpiler.phases.emit_cpp.block import BlockVisitor
# yield from BlockVisitor(self.scope).visit_func_new(node, FunctionEmissionKind.METHOD, True)
# yield f"}} {node.name} {{ this }};"
yield
f"struct
{
node
.
name
}
_m_s : method {{"
yield
f"struct
{
node
.
name
}
_m_s :
referencemodel::
method {{"
from
transpiler.phases.emit_cpp.block
import
BlockVisitor
yield
from
BlockVisitor
(
self
.
scope
).
visit_func_new
(
node
,
FunctionEmissionKind
.
METHOD
)
yield
f"}} static constexpr
{
node
.
name
}
{{}};"
...
...
@@ -109,3 +109,29 @@ class ClassOuterVisitor(NodeVisitor):
# from transpiler.phases.emit_cpp.block import BlockVisitor
# yield from BlockVisitor(self.scope).visit_func_new(node, FunctionEmissionKind.METHOD)
# yield f"}} static constexpr {node.name} {{}};"
@
dataclass
class
ClassInnerVisitor2
(
NodeVisitor
):
scope
:
Scope
def
visit_AnnAssign
(
self
,
node
:
ast
.
AnnAssign
)
->
Iterable
[
str
]:
yield
""
def
visit_FunctionDef
(
self
,
node
:
ast
.
FunctionDef
)
->
Iterable
[
str
]:
yield
"struct : referencemodel::method {"
from
transpiler.phases.emit_cpp.block
import
BlockVisitor
yield
from
BlockVisitor
(
self
.
scope
).
visit_func_new
(
node
,
FunctionEmissionKind
.
METHOD
)
yield
f"}} static constexpr
{
node
.
name
}
{{}};"
@
dataclass
class
ClassInnerVisitor4
(
NodeVisitor
):
scope
:
Scope
def
visit_AnnAssign
(
self
,
node
:
ast
.
AnnAssign
)
->
Iterable
[
str
]:
member
=
self
.
scope
.
obj_type
.
fields
[
node
.
target
.
id
]
yield
from
self
.
visit
(
member
.
type
)
yield
node
.
target
.
id
yield
";"
def
visit_FunctionDef
(
self
,
node
:
ast
.
FunctionDef
)
->
Iterable
[
str
]:
yield
""
\ No newline at end of file
typon/trans/transpiler/phases/emit_cpp/expr.py
View file @
23be3256
...
...
@@ -227,6 +227,8 @@ class ExpressionVisitor(NodeVisitor):
use_dot
=
"dotp"
else
:
use_dot
=
"dot"
if
use_dot
:
use_dot
=
"dot"
if
use_dot
:
yield
use_dot
yield
"(("
...
...
typon/trans/transpiler/phases/emit_cpp/file.py
View file @
23be3256
...
...
@@ -4,7 +4,8 @@ from dataclasses import dataclass
from
typing
import
Iterable
from
transpiler.phases.emit_cpp.block
import
BlockVisitor
from
transpiler.phases.emit_cpp.module
import
ModuleVisitor
,
ModuleVisitor2
,
ModuleVisitorExt
from
transpiler.phases.emit_cpp.module
import
ModuleVisitor
,
ModuleVisitor2
,
ModuleVisitorExt
,
ModuleVisitor3
,
\
ModuleVisitor4
# noinspection PyPep8Naming
...
...
@@ -22,10 +23,26 @@ class FileVisitor(BlockVisitor):
code
=
[
line
for
stmt
in
node
.
body
for
line
in
visitor
.
visit
(
stmt
)]
yield
from
visitor
.
includes
yield
"namespace PROGRAMNS {"
yield
"struct __main__ : referencemodel::moduletype<__main__> {"
yield
from
code
visitor
=
ModuleVisitor2
(
self
.
scope
)
# visitor = ModuleVisitor2(self.scope)
# code = [line for stmt in node.body for line in visitor.visit(stmt)]
# yield from code
visitor
=
ModuleVisitor4
(
self
.
scope
)
code
=
[
line
for
stmt
in
node
.
body
for
line
in
visitor
.
visit
(
stmt
)]
yield
from
code
visitor
=
ModuleVisitor3
(
self
.
scope
)
code
=
[
line
for
stmt
in
node
.
body
for
line
in
visitor
.
visit
(
stmt
)]
yield
from
code
yield
"auto operator -> () const { return this; }"
yield
"} __main__;"
yield
"}"
yield
"#ifdef TYPON_EXTENSION"
yield
f"PYBIND11_MODULE(
{
self
.
module_name
}
, m) {{"
...
...
@@ -37,6 +54,6 @@ class FileVisitor(BlockVisitor):
yield
"#else"
yield
"int main(int argc, char* argv[]) {"
yield
"py_sys::all.argv = typon::PyList<PyStr>(std::vector<PyStr>(argv, argv + argc));"
yield
"PROGRAMNS::root().call();"
yield
"PROGRAMNS::
__main__.
root().call();"
yield
"}"
yield
"#endif"
typon/trans/transpiler/phases/emit_cpp/module.py
View file @
23be3256
...
...
@@ -8,7 +8,7 @@ from transpiler.phases.typing import FunctionType
from
transpiler.phases.typing.scope
import
Scope
from
transpiler.phases.emit_cpp
import
CoroutineMode
,
FunctionEmissionKind
,
NodeVisitor
,
join
from
transpiler.phases.emit_cpp.block
import
BlockVisitor
from
transpiler.phases.emit_cpp.class_
import
ClassVisitor
from
transpiler.phases.emit_cpp.class_
import
ClassVisitor
,
ClassInnerVisitor
,
ClassInnerVisitor2
,
ClassInnerVisitor4
from
transpiler.phases.emit_cpp.function
import
FunctionVisitor
from
transpiler.utils
import
compare_ast
,
highlight
...
...
@@ -100,7 +100,8 @@ class ModuleVisitor(BlockVisitor):
raise
NotImplementedError
(
node
)
def
visit_ClassDef
(
self
,
node
:
ast
.
ClassDef
)
->
Iterable
[
str
]:
yield
from
ClassVisitor
().
visit
(
node
)
#yield from ClassVisitor().visit(node)
yield
from
()
def
visit_FunctionDef
(
self
,
node
:
ast
.
FunctionDef
)
->
Iterable
[
str
]:
yield
from
super
().
visit_free_func
(
node
,
FunctionEmissionKind
.
DECLARATION
)
...
...
@@ -116,6 +117,68 @@ class ModuleVisitor2(NodeVisitor):
yield
""
pass
@
dataclass
class
ModuleVisitor3
(
NodeVisitor
):
scope
:
Scope
def
visit_ClassDef
(
self
,
node
:
ast
.
ClassDef
)
->
Iterable
[
str
]:
yield
from
()
return
if
gen_instances
:
=
getattr
(
node
,
"gen_instances"
,
None
):
for
args
,
inst
in
gen_instances
.
items
():
yield
from
self
.
visit_ClassDef
(
inst
)
return
yield
f"static constexpr _detail_<__main__>::
{
node
.
name
}
<>
{
node
.
name
}
{{}};"
def
visit_FunctionDef
(
self
,
node
:
ast
.
FunctionDef
)
->
Iterable
[
str
]:
yield
from
BlockVisitor
(
self
.
scope
).
visit_free_func
(
node
,
FunctionEmissionKind
.
DEFINITION
)
@
dataclass
class
ModuleVisitor4
(
NodeVisitor
):
scope
:
Scope
def
visit_ClassDef
(
self
,
node
:
ast
.
ClassDef
)
->
Iterable
[
str
]:
if
gen_instances
:
=
getattr
(
node
,
"gen_instances"
,
None
):
for
args
,
inst
in
gen_instances
.
items
():
yield
from
self
.
visit_ClassDef
(
inst
)
return
yield
f"struct
{
node
.
name
}
: referencemodel::classtype<
{
node
.
name
}
> {{"
yield
f"template <typename _Base0 = referencemodel::object>"
yield
f"struct body : referencemodel::classbodytype<_Base0, body<>> {{"
yield
f"static constexpr std::string_view name =
\
"
{
node
.
name
}\
"
;"
inner
=
ClassInnerVisitor2
(
node
.
inner_scope
)
for
stmt
in
node
.
body
:
yield
from
inner
.
visit
(
stmt
)
yield
"};"
yield
"static constexpr body _body{};"
yield
"static_assert(sizeof _body == 1);"
yield
f"struct Obj : referencemodel::instance<
{
node
.
name
}
, Obj> {{"
inner
=
ClassInnerVisitor4
(
node
.
inner_scope
)
for
stmt
in
node
.
body
:
yield
from
inner
.
visit
(
stmt
)
yield
"template <typename... U>"
yield
"Obj(U&&... args) {"
yield
"dot(this, __init__)(this, std::forward<U>(args)...);"
yield
"}"
yield
"};"
yield
"template <typename _Unused = void, typename... T>"
yield
"auto operator() (T&&... args) const {"
yield
"return referencemodel::rc(Obj{std::forward<T>(args)...});"
yield
"}"
yield
f"}} static constexpr
{
node
.
name
}
{{}};"
def
visit_FunctionDef
(
self
,
node
:
ast
.
FunctionDef
)
->
Iterable
[
str
]:
yield
from
()
@
dataclass
class
ModuleVisitorExt
(
NodeVisitor
):
scope
:
Scope
...
...
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