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
7013323d
Commit
7013323d
authored
Apr 08, 2024
by
Tom Niget
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Initial support for inheritance and mro works
parent
4dda1cb4
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
92 additions
and
6 deletions
+92
-6
typon/include/python/builtins/str.hpp
typon/include/python/builtins/str.hpp
+2
-1
typon/trans/tests/inheritance.py
typon/trans/tests/inheritance.py
+58
-0
typon/trans/transpiler/phases/emit_cpp/class_.py
typon/trans/transpiler/phases/emit_cpp/class_.py
+19
-3
typon/trans/transpiler/phases/emit_cpp/expr.py
typon/trans/transpiler/phases/emit_cpp/expr.py
+6
-0
typon/trans/transpiler/phases/emit_cpp/visitors.py
typon/trans/transpiler/phases/emit_cpp/visitors.py
+6
-1
typon/trans/transpiler/phases/typing/stdlib.py
typon/trans/transpiler/phases/typing/stdlib.py
+1
-1
No files found.
typon/include/python/builtins/str.hpp
View file @
7013323d
...
...
@@ -178,7 +178,8 @@ struct TyStr__oo : classtype<_Base0, TyStr__oo<>> {
// getitem
struct
:
method
{
auto
operator
()(
auto
self
,
TySlice__oo
<>::
Obj
<
auto
,
auto
,
auto
>
slice
)
const
{
template
<
typename
T
,
typename
U
,
typename
V
>
auto
operator
()(
auto
self
,
TySlice__oo
<>::
Obj
<
T
,
U
,
V
>
slice
)
const
{
auto
[
len
,
new_slice
]
=
slice
.
adjust_indices
(
self
->
value
.
size
());
Obj
result
;
result
.
value
.
reserve
(
len
);
...
...
typon/trans/tests/inheritance.py
0 → 100644
View file @
7013323d
# coding: utf-8
class
class1
:
def
method1
(
self
):
print
(
"class1::method1()"
)
def
function2
():
print
(
"function2"
)
return
class1
()
def
function1
():
r
=
function2
()
print
(
"function1()"
)
return
r
class
class2
(
class1
):
def
method2
(
self
):
self
.
method1
()
print
(
"class2::method2()"
)
class
class3
(
class1
):
def
method2
(
self
):
self
.
method1
()
print
(
"class3::method2()"
)
class
class4
(
class2
,
class3
):
def
method3
(
self
):
self
.
method1
()
print
(
"class4::method3()"
)
if
__name__
==
"__main__"
:
print
(
">>> o1 = function1()"
)
o1
=
function1
()
print
(
">>> o1.method1()"
)
o1
.
method1
()
print
(
">>> o2 = class2()"
)
o2
=
class2
()
print
(
">>> o2.method1()"
)
o2
.
method1
()
print
(
">>> o2.method2()"
)
o2
.
method2
()
print
(
">>> o3 = class3()"
)
o3
=
class3
()
print
(
">>> o3.method2()"
)
o3
.
method2
()
print
(
">>> o4 = class4()"
)
o4
=
class4
()
print
(
">>> o4.method3()"
)
o4
.
method3
()
typon/trans/transpiler/phases/emit_cpp/class_.py
View file @
7013323d
...
...
@@ -4,12 +4,28 @@ from typing import Iterable
from
transpiler.phases.emit_cpp.function
import
emit_function
from
transpiler.phases.emit_cpp.visitors
import
join
,
NodeVisitor
from
transpiler.phases.typing.expr
import
ScoperExprVisitor
from
transpiler.phases.typing.types
import
ConcreteType
,
TypeVariable
,
RuntimeValue
from
transpiler.phases.typing.types
import
ConcreteType
,
TypeVariable
,
RuntimeValue
,
TY_OBJECT
,
GenericInstanceType
def
emit_base_type
(
t
:
list
[
ConcreteType
]):
match
t
:
case
[]:
yield
"referencemodel::object"
case
[
par
]
if
isinstance
(
par
,
GenericInstanceType
)
and
not
par
.
generic_args
:
yield
"decltype("
+
par
.
generic_parent
.
name
()
+
")"
case
[
head
,
*
tail
]
if
tail
:
yield
"referencemodel::meta::rebase<"
yield
from
emit_base_type
([
head
])
yield
","
yield
from
emit_base_type
(
tail
)
yield
">"
case
_
:
raise
NotImplementedError
(
"parent not handled yet: "
+
str
(
t
))
def
emit_class
(
name
:
str
,
node
:
ConcreteType
)
->
Iterable
[
str
]:
__TB_NODE__
=
node
.
block_data
.
node
yield
f"template <typename _Base0 = referencemodel::object>"
yield
"template <typename _Base0 ="
yield
from
emit_base_type
(
node
.
get_mro
()[
1
:
-
1
])
yield
">"
yield
f"struct
{
name
}
__oo : referencemodel::classtype<_Base0,
{
name
}
__oo<>> {{"
yield
f"static constexpr std::string_view name =
\
"
{
name
}\
"
;"
...
...
typon/trans/transpiler/phases/emit_cpp/expr.py
View file @
7013323d
...
...
@@ -198,6 +198,8 @@ class ExpressionVisitor(NodeVisitor):
yield
")"
add_call
=
False
if
isinstance
(
node
.
func
.
type
,
ClassTypeType
):
inner
=
node
.
func
.
type
.
inner_type
if
isinstance
(
node
.
type
,
GenericInstanceType
):
...
...
@@ -206,11 +208,15 @@ class ExpressionVisitor(NodeVisitor):
yield
"<"
yield
from
join
(
", "
,
(
self
.
visit
(
arg
)
for
arg
in
node
.
type
.
generic_args
))
yield
">"
add_call
=
True
yield
"("
yield
from
join
(
", "
,
map
(
self
.
visit
,
node
.
args
))
yield
")"
if
add_call
and
self
.
generator
==
CoroutineMode
.
SYNC
:
yield
".call()"
#raise NotImplementedError()
# TODO
# if getattr(node, "keywords", None):
...
...
typon/trans/transpiler/phases/emit_cpp/visitors.py
View file @
7013323d
...
...
@@ -88,7 +88,10 @@ class NodeVisitor(UniversalVisitor):
case
types
.
GenericInstanceType
():
yield
from
self
.
visit
(
node
.
generic_parent
)
yield
"<"
yield
from
join
(
","
,
map
(
self
.
visit
,
node
.
generic_args
))
if
node
.
generic_args
:
yield
from
join
(
","
,
map
(
self
.
visit
,
node
.
generic_args
))
else
:
yield
"void"
yield
">"
# case types.TY_LIST:
# yield "typon::TyList"
...
...
@@ -104,6 +107,8 @@ class NodeVisitor(UniversalVisitor):
yield
"typon::Forked"
case
types
.
TY_MUTEX
:
yield
"typon::ArcMutex"
# case types.UserGenericType():
# yield f"typename decltype({node.name()})::Obj"
case
_
:
raise
NotImplementedError
(
node
)
...
...
typon/trans/transpiler/phases/typing/stdlib.py
View file @
7013323d
...
...
@@ -162,7 +162,7 @@ class StdlibVisitor(NodeVisitorSeq):
case
[
prot
]
if
is_builtin
(
prot
,
"Protocol"
):
output
.
is_protocol
=
True
case
_
:
raise
NotImplementedError
(
"parents not handled yet: "
+
", "
.
join
(
map
(
ast
.
unparse
,
node
.
bases
)))
output
.
parents
=
[
p
.
deref
()
for
p
in
bases
]
# TODO: what about uninstantiated generic types here?
for
stmt
in
node
.
body
:
visitor
.
visit
(
stmt
)
for
deco_node
in
node
.
decorator_list
:
...
...
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