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
8a55840a
Commit
8a55840a
authored
Jul 07, 2023
by
Tom Niget
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add `with` block desugaring
parent
c8d6c8db
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
31 additions
and
0 deletions
+31
-0
trans/transpiler/__init__.py
trans/transpiler/__init__.py
+2
-0
trans/transpiler/phases/desugar_with/__init__.py
trans/transpiler/phases/desugar_with/__init__.py
+29
-0
No files found.
trans/transpiler/__init__.py
View file @
8a55840a
...
...
@@ -2,6 +2,7 @@
import
ast
from
transpiler.consts
import
MAPPINGS
from
transpiler.phases.desugar_with
import
DesugarWith
#from transpiler.phases import initial_pytype
from
transpiler.phases.emit_cpp.file
import
FileVisitor
from
transpiler.phases.if_main
import
IfMainVisitor
...
...
@@ -12,6 +13,7 @@ from transpiler.phases.typing.scope import Scope
def
transpile
(
source
):
res
=
ast
.
parse
(
source
,
type_comments
=
True
)
#res = initial_pytype.run(source, res)
res
=
DesugarWith
().
visit
(
res
)
IfMainVisitor
().
visit
(
res
)
ScoperBlockVisitor
().
visit
(
res
)
#print(res.scope)
...
...
trans/transpiler/phases/desugar_with/__init__.py
0 → 100644
View file @
8a55840a
# coding: utf-8
import
ast
from
transpiler.phases.utils
import
PlainBlock
def
process
(
items
:
list
[
ast
.
withitem
],
body
:
list
[
ast
.
stmt
])
->
PlainBlock
:
first
,
*
rest
=
items
val
,
name
=
first
.
context_expr
,
first
.
optional_vars
cm_name
=
ast
.
Name
(
id
=
f"cm_
{
hash
(
first
)
}
"
)
res
=
[
ast
.
Assign
(
targets
=
[
cm_name
],
value
=
val
)
]
enter_call
=
ast
.
Call
(
func
=
ast
.
Attribute
(
value
=
cm_name
,
attr
=
"__enter__"
),
args
=
[],
keywords
=
[])
if
name
:
res
.
append
(
ast
.
Assign
(
targets
=
[
name
],
value
=
enter_call
))
else
:
res
.
append
(
ast
.
Expr
(
value
=
enter_call
))
if
rest
:
res
.
append
(
process
(
rest
,
body
))
else
:
res
.
append
(
PlainBlock
(
body
))
res
.
append
(
ast
.
Expr
(
value
=
ast
.
Call
(
func
=
ast
.
Attribute
(
value
=
cm_name
,
attr
=
"__exit__"
),
args
=
[],
keywords
=
[])))
return
PlainBlock
(
res
)
class
DesugarWith
(
ast
.
NodeTransformer
):
def
visit_With
(
self
,
node
:
ast
.
With
):
return
process
(
node
.
items
,
node
.
body
)
\ 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