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
6e8e1738
Commit
6e8e1738
authored
Mar 20, 2024
by
Tom Niget
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Handle recursive functions
parent
39b718cc
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
35 additions
and
19 deletions
+35
-19
typon/include/python/builtins.hpp
typon/include/python/builtins.hpp
+1
-3
typon/trans/transpiler/phases/emit_cpp/function.py
typon/trans/transpiler/phases/emit_cpp/function.py
+8
-1
typon/trans/transpiler/phases/emit_cpp/module.py
typon/trans/transpiler/phases/emit_cpp/module.py
+1
-0
typon/trans/transpiler/phases/typing/expr.py
typon/trans/transpiler/phases/typing/expr.py
+14
-10
typon/trans/transpiler/phases/typing/types.py
typon/trans/transpiler/phases/typing/types.py
+11
-5
No files found.
typon/include/python/builtins.hpp
View file @
6e8e1738
...
...
@@ -386,9 +386,7 @@ using InterpGuard = py::scoped_interpreter;
#endif
template
<
typename
T
>
concept
HasSync
=
requires
(
T
t
)
{
{
t
.
sync
()
}
->
std
::
same_as
<
T
>
;
};
concept
HasSync
=
requires
(
T
t
)
{
typename
T
::
has_sync
;
};
/*auto call_sync(auto f, auto... args) {
if constexpr (HasSync<decltype(f)>) {
...
...
typon/trans/transpiler/phases/emit_cpp/function.py
View file @
6e8e1738
...
...
@@ -43,7 +43,9 @@ def emit_function(name: str, func: CallableInstanceType, base="function", gen_p=
yield
")>::type"
yield
var
yield
";"
yield
from
BlockVisitor
(
func
.
block_data
.
scope
,
generator
=
mode
).
visit
(
func
.
block_data
.
node
.
body
)
vis
=
BlockVisitor
(
func
.
block_data
.
scope
,
generator
=
mode
)
for
stmt
in
func
.
block_data
.
node
.
body
:
yield
from
vis
.
visit
(
stmt
)
if
not
getattr
(
func
.
block_data
.
scope
,
"has_return"
,
False
):
if
mode
==
CoroutineMode
.
SYNC
:
yield
"return"
...
...
@@ -53,10 +55,13 @@ def emit_function(name: str, func: CallableInstanceType, base="function", gen_p=
yield
"}"
rty
=
func
.
return_type
.
generic_args
[
0
]
has_sync
=
False
try
:
rty_code
=
" "
.
join
(
NodeVisitor
().
visit_BaseType
(
func
.
return_type
))
except
:
yield
from
emit_body
(
"sync"
,
CoroutineMode
.
SYNC
,
None
)
has_sync
=
True
yield
"using has_sync = std::true_type;"
def
task_type
():
yield
from
NodeVisitor
().
visit_BaseType
(
func
.
return_type
.
generic_parent
)
yield
"<"
...
...
@@ -70,6 +75,8 @@ def emit_function(name: str, func: CallableInstanceType, base="function", gen_p=
yield
from
emit_body
(
"operator()"
,
CoroutineMode
.
TASK
,
rty_code
)
yield
f"}} static constexpr
{
name
}
{{}};"
if
has_sync
:
yield
f"static_assert(HasSync<decltype(
{
name
}
)>);"
yield
f"static_assert(sizeof
{
name
}
== 1);"
...
...
typon/trans/transpiler/phases/emit_cpp/module.py
View file @
6e8e1738
...
...
@@ -43,6 +43,7 @@ def emit_module(mod: ModuleType) -> Iterable[str]:
x
=
5
match
ty
:
case
CallableInstanceType
():
ty
.
generic_parent
.
instance_cache
=
[]
ScoperExprVisitor
(
ty
.
block_data
.
scope
).
visit_function_call
(
ty
,
[
TypeVariable
()
for
_
in
ty
.
parameters
])
yield
from
emit_function
(
name
,
ty
,
gen_p
=
gen_p
)
case
GenericInstanceType
()
if
isinstance
(
ty
.
generic_parent
,
UserGenericType
):
...
...
typon/trans/transpiler/phases/typing/expr.py
View file @
6e8e1738
...
...
@@ -168,6 +168,9 @@ class ScoperExprVisitor(ScoperVisitor):
ftype
.
block_data
.
scope
.
declare_local
(
pname
,
b
)
if
not
ftype
.
is_native
:
existing
=
ftype
.
generic_parent
.
find_cached_instance
(
ftype
.
generic_args
)
if
not
existing
:
ftype
.
generic_parent
.
cache_instance
(
ftype
.
generic_args
,
ftype
)
from
transpiler.phases.typing.block
import
ScoperBlockVisitor
scope
=
ftype
.
block_data
.
scope
vis
=
ScoperBlockVisitor
(
scope
)
...
...
@@ -177,7 +180,8 @@ class ScoperExprVisitor(ScoperVisitor):
stmt
=
ast
.
Return
()
ftype
.
block_data
.
node
.
body
.
append
(
stmt
)
vis
.
visit
(
stmt
)
#ftype.generic_parent.cache_instance(ftype)
else
:
return
existing
.
return_type
.
resolve
()
return
ftype
.
return_type
.
resolve
()
# if isinstance(ftype, TypeType):# and isinstance(ftype.type_object, UserType):
# init: FunctionType = self.visit_getattr(ftype, "__init__").remove_self()
...
...
typon/trans/transpiler/phases/typing/types.py
View file @
6e8e1738
...
...
@@ -320,7 +320,7 @@ class GenericConstraint:
@
dataclass
(
eq
=
False
)
class
GenericType
(
BaseType
):
parameters
:
list
[
GenericParameter
]
=
field
(
default_factory
=
list
,
init
=
False
)
instance_cache
:
dict
[
object
,
GenericInstanceType
]
=
field
(
default_factory
=
dic
t
,
init
=
False
)
instance_cache
:
list
[(
object
,
GenericInstanceType
)]
=
field
(
default_factory
=
lis
t
,
init
=
False
)
def
constraints
(
self
,
args
:
list
[
ConcreteType
])
->
list
[
GenericConstraint
]:
return
[]
...
...
@@ -348,11 +348,17 @@ class GenericType(BaseType):
def
deref
(
self
):
return
self
.
instantiate_default
().
deref
()
def
cache_instance
(
self
,
instance
):
if
not
hasattr
(
self
,
"instance_cache"
):
self
.
instance_cache
=
{}
self
.
instance_cache
[
tuple
(
instance
.
generic_args
)]
=
instance
def
find_cached_instance
(
self
,
args
):
for
inst_args
,
inst
in
self
.
instance_cache
:
if
all
(
inst_arg
.
try_assign
(
arg
)
for
inst_arg
,
arg
in
zip
(
inst_args
,
args
)):
return
inst
return
None
def
cache_instance
(
self
,
args
,
instance
):
if
not
hasattr
(
self
,
"instance_cache"
):
self
.
instance_cache
=
[]
if
not
self
.
find_cached_instance
(
args
):
self
.
instance_cache
.
append
((
tuple
(
args
),
instance
))
@
dataclass
(
eq
=
False
,
init
=
False
)
class
BuiltinGenericType
(
UniqueTypeMixin
,
GenericType
):
...
...
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