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
a6f9a9dc
Commit
a6f9a9dc
authored
Aug 19, 2023
by
Tom Niget
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add support for nested functions
parent
e54a10fc
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
37 additions
and
8 deletions
+37
-8
trans/transpiler/phases/emit_cpp/__init__.py
trans/transpiler/phases/emit_cpp/__init__.py
+1
-0
trans/transpiler/phases/emit_cpp/block.py
trans/transpiler/phases/emit_cpp/block.py
+13
-6
trans/transpiler/phases/emit_cpp/function.py
trans/transpiler/phases/emit_cpp/function.py
+6
-0
trans/transpiler/phases/typing/common.py
trans/transpiler/phases/typing/common.py
+17
-2
No files found.
trans/transpiler/phases/emit_cpp/__init__.py
View file @
a6f9a9dc
...
...
@@ -111,6 +111,7 @@ class FunctionEmissionKind(enum.Enum):
DECLARATION
=
enum
.
auto
()
DEFINITION
=
enum
.
auto
()
METHOD
=
enum
.
auto
()
LAMBDA
=
enum
.
auto
()
def
join
(
sep
:
str
,
items
:
Iterable
[
Iterable
[
str
]])
->
Iterable
[
str
]:
items
=
iter
(
items
)
...
...
trans/transpiler/phases/emit_cpp/block.py
View file @
a6f9a9dc
...
...
@@ -56,12 +56,15 @@ class BlockVisitor(NodeVisitor):
yield
f"}}
{
node
.
name
}
;"
def
visit_func_new
(
self
,
node
:
ast
.
FunctionDef
,
emission
:
FunctionEmissionKind
,
skip_first_arg
:
bool
=
False
)
->
Iterable
[
str
]:
if
emission
==
FunctionEmissionKind
.
METHOD
:
yield
"template <typename Self>"
yield
from
self
.
visit
(
node
.
type
.
return_type
)
if
emission
==
FunctionEmissionKind
.
DEFINITION
:
yield
f"
{
node
.
name
}
_inner::"
yield
"operator()"
if
emission
==
FunctionEmissionKind
.
LAMBDA
:
yield
"[&]"
else
:
if
emission
==
FunctionEmissionKind
.
METHOD
:
yield
"template <typename Self>"
yield
from
self
.
visit
(
node
.
type
.
return_type
)
if
emission
==
FunctionEmissionKind
.
DEFINITION
:
yield
f"
{
node
.
name
}
_inner::"
yield
"operator()"
yield
"("
padded_defaults
=
[
None
]
*
(
node
.
type
.
optional_at
or
len
(
node
.
args
.
args
))
+
node
.
args
.
defaults
args_iter
=
zip
(
node
.
args
.
args
,
node
.
type
.
parameters
,
padded_defaults
)
...
...
@@ -89,6 +92,10 @@ class BlockVisitor(NodeVisitor):
yield
";"
return
if
emission
==
FunctionEmissionKind
.
LAMBDA
:
yield
"->"
yield
from
self
.
visit
(
node
.
type
.
return_type
)
yield
"{"
class
ReturnVisitor
(
SearchVisitor
):
...
...
trans/transpiler/phases/emit_cpp/function.py
View file @
a6f9a9dc
...
...
@@ -113,3 +113,9 @@ class FunctionVisitor(BlockVisitor):
#yield from self.visit(handler)
pass
# todo
def
visit_FunctionDef
(
self
,
node
:
ast
.
FunctionDef
)
->
Iterable
[
str
]:
yield
"auto"
yield
self
.
fix_name
(
node
.
name
)
yield
"="
yield
from
self
.
visit_func_new
(
node
,
FunctionEmissionKind
.
LAMBDA
)
yield
";"
trans/transpiler/phases/typing/common.py
View file @
a6f9a9dc
...
...
@@ -5,7 +5,7 @@ from typing import Dict, Optional
from
transpiler.utils
import
highlight
from
transpiler.phases.typing.annotations
import
TypeAnnotationVisitor
from
transpiler.phases.typing.scope
import
Scope
,
ScopeKind
,
VarDecl
from
transpiler.phases.typing.types
import
BaseType
,
TypeVariable
,
TY_NONE
,
TypeType
from
transpiler.phases.typing.types
import
BaseType
,
TypeVariable
,
TY_NONE
,
TypeType
,
BuiltinFeature
from
transpiler.phases.utils
import
NodeVisitorSeq
PRELUDE
=
Scope
.
make_global
()
...
...
@@ -16,6 +16,10 @@ class ScoperVisitor(NodeVisitorSeq):
root_decls
:
Dict
[
str
,
VarDecl
]
=
field
(
default_factory
=
dict
)
cur_class
:
Optional
[
TypeType
]
=
None
def
expr
(
self
)
->
"ScoperExprVisitor"
:
from
transpiler.phases.typing.expr
import
ScoperExprVisitor
return
ScoperExprVisitor
(
self
.
scope
,
self
.
root_decls
)
def
anno
(
self
)
->
"TypeAnnotationVisitor"
:
return
TypeAnnotationVisitor
(
self
.
scope
,
self
.
cur_class
)
...
...
@@ -58,7 +62,15 @@ class ScoperVisitor(NodeVisitorSeq):
for
b
in
node
.
body
:
decls
=
{}
visitor
=
ScoperBlockVisitor
(
node
.
inner_scope
,
decls
)
visitor
.
fdecls
=
[]
visitor
.
visit
(
b
)
if
len
(
visitor
.
fdecls
)
>
1
:
raise
NotImplementedError
(
"?"
)
elif
len
(
visitor
.
fdecls
)
==
1
:
fnode
,
frtype
=
visitor
.
fdecls
[
0
]
self
.
visit_function_definition
(
fnode
,
frtype
)
del
node
.
inner_scope
.
vars
[
fnode
.
name
]
visitor
.
visit_assign_target
(
ast
.
Name
(
fnode
.
name
),
fnode
.
type
)
b
.
decls
=
decls
if
not
node
.
inner_scope
.
has_return
:
rtype
.
unify
(
TY_NONE
)
# todo: properly indicate missing return
...
...
@@ -77,4 +89,7 @@ def get_next(iter_type):
except
:
from
transpiler.phases.typing.exceptions
import
NotIteratorError
raise
NotIteratorError
(
iter_type
)
return
next_type
\ No newline at end of file
return
next_type
def
is_builtin
(
x
,
feature
):
return
isinstance
(
x
,
BuiltinFeature
)
and
x
.
val
==
feature
\ 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