Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
pystencils
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
pycodegen
pystencils
Commits
8ceb6781
Commit
8ceb6781
authored
3 months ago
by
Richard Angersbach
Browse files
Options
Downloads
Patches
Plain Diff
Introduce _clone_structural function for PsStructural node (similar to PsExpression)
parent
216ef8bc
No related branches found
No related tags found
1 merge request
!455
Introduction of structural ast nodes
Pipeline
#74961
failed
3 months ago
Stage: Code Quality
Stage: Unit Tests
Stage: legacy_test
Stage: docs
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/pystencils/backend/ast/structural.py
+28
-10
28 additions, 10 deletions
src/pystencils/backend/ast/structural.py
with
28 additions
and
10 deletions
src/pystencils/backend/ast/structural.py
+
28
−
10
View file @
8ceb6781
from
__future__
import
annotations
from
__future__
import
annotations
from
abc
import
ABC
from
abc
import
ABC
,
abstractmethod
from
typing
import
Iterable
,
Sequence
,
cast
from
typing
import
Iterable
,
Sequence
,
cast
from
types
import
NoneType
from
types
import
NoneType
...
@@ -17,6 +17,24 @@ class PsStructuralNode(PsAstNode, ABC):
...
@@ -17,6 +17,24 @@ class PsStructuralNode(PsAstNode, ABC):
This class acts as a trait that structural AST nodes like blocks, conditionals, etc. can inherit from.
This class acts as a trait that structural AST nodes like blocks, conditionals, etc. can inherit from.
"""
"""
def
clone
(
self
)
->
PsStructuralNode
:
"""
Clone this structure node.
.. note::
Subclasses of `PsStructuralNode` should not override this method,
but implement `_clone_structural` instead.
That implementation shall call `clone` on any of its children.
"""
return
self
.
_clone_structural
()
@abstractmethod
def
_clone_structural
(
self
)
->
PsStructuralNode
:
"""
Implementation of structural node cloning.
:meta public:
"""
pass
class
PsBlock
(
PsStructuralNode
):
class
PsBlock
(
PsStructuralNode
):
__match_args__
=
(
"
statements
"
,)
__match_args__
=
(
"
statements
"
,)
...
@@ -38,8 +56,8 @@ class PsBlock(PsStructuralNode):
...
@@ -38,8 +56,8 @@ class PsBlock(PsStructuralNode):
def
set_child
(
self
,
idx
:
int
,
c
:
PsAstNode
):
def
set_child
(
self
,
idx
:
int
,
c
:
PsAstNode
):
self
.
_statements
[
idx
]
=
failing_cast
(
PsStructuralNode
,
c
)
self
.
_statements
[
idx
]
=
failing_cast
(
PsStructuralNode
,
c
)
def
clone
(
self
)
->
PsBlock
:
def
_
clone
_structural
(
self
)
->
PsBlock
:
return
PsBlock
([
failing_cast
(
PsStructuralNode
,
stmt
.
clone
()
)
for
stmt
in
self
.
_statements
])
return
PsBlock
([
stmt
.
_clone_structural
(
)
for
stmt
in
self
.
_statements
])
@property
@property
def
statements
(
self
)
->
list
[
PsStructuralNode
]:
def
statements
(
self
)
->
list
[
PsStructuralNode
]:
...
@@ -68,7 +86,7 @@ class PsStatement(PsStructuralNode):
...
@@ -68,7 +86,7 @@ class PsStatement(PsStructuralNode):
def
expression
(
self
,
expr
:
PsExpression
):
def
expression
(
self
,
expr
:
PsExpression
):
self
.
_expression
=
expr
self
.
_expression
=
expr
def
clone
(
self
)
->
PsStatement
:
def
_
clone
_structural
(
self
)
->
PsStatement
:
return
PsStatement
(
self
.
_expression
.
clone
())
return
PsStatement
(
self
.
_expression
.
clone
())
def
get_children
(
self
)
->
tuple
[
PsAstNode
,
...]:
def
get_children
(
self
)
->
tuple
[
PsAstNode
,
...]:
...
@@ -110,7 +128,7 @@ class PsAssignment(PsStructuralNode):
...
@@ -110,7 +128,7 @@ class PsAssignment(PsStructuralNode):
def
rhs
(
self
,
expr
:
PsExpression
):
def
rhs
(
self
,
expr
:
PsExpression
):
self
.
_rhs
=
expr
self
.
_rhs
=
expr
def
clone
(
self
)
->
PsAssignment
:
def
_
clone
_structural
(
self
)
->
PsAssignment
:
return
PsAssignment
(
self
.
_lhs
.
clone
(),
self
.
_rhs
.
clone
())
return
PsAssignment
(
self
.
_lhs
.
clone
(),
self
.
_rhs
.
clone
())
def
get_children
(
self
)
->
tuple
[
PsAstNode
,
...]:
def
get_children
(
self
)
->
tuple
[
PsAstNode
,
...]:
...
@@ -150,7 +168,7 @@ class PsDeclaration(PsAssignment):
...
@@ -150,7 +168,7 @@ class PsDeclaration(PsAssignment):
def
declared_symbol
(
self
)
->
PsSymbol
:
def
declared_symbol
(
self
)
->
PsSymbol
:
return
cast
(
PsSymbolExpr
,
self
.
_lhs
).
symbol
return
cast
(
PsSymbolExpr
,
self
.
_lhs
).
symbol
def
clone
(
self
)
->
PsDeclaration
:
def
_
clone
_structural
(
self
)
->
PsDeclaration
:
return
PsDeclaration
(
cast
(
PsSymbolExpr
,
self
.
_lhs
.
clone
()),
self
.
rhs
.
clone
())
return
PsDeclaration
(
cast
(
PsSymbolExpr
,
self
.
_lhs
.
clone
()),
self
.
rhs
.
clone
())
def
set_child
(
self
,
idx
:
int
,
c
:
PsAstNode
):
def
set_child
(
self
,
idx
:
int
,
c
:
PsAstNode
):
...
@@ -223,7 +241,7 @@ class PsLoop(PsStructuralNode):
...
@@ -223,7 +241,7 @@ class PsLoop(PsStructuralNode):
def
body
(
self
,
block
:
PsBlock
):
def
body
(
self
,
block
:
PsBlock
):
self
.
_body
=
block
self
.
_body
=
block
def
clone
(
self
)
->
PsLoop
:
def
_
clone
_structural
(
self
)
->
PsLoop
:
return
PsLoop
(
return
PsLoop
(
self
.
_ctr
.
clone
(),
self
.
_ctr
.
clone
(),
self
.
_start
.
clone
(),
self
.
_start
.
clone
(),
...
@@ -291,7 +309,7 @@ class PsConditional(PsStructuralNode):
...
@@ -291,7 +309,7 @@ class PsConditional(PsStructuralNode):
def
branch_false
(
self
,
block
:
PsBlock
|
None
):
def
branch_false
(
self
,
block
:
PsBlock
|
None
):
self
.
_branch_false
=
block
self
.
_branch_false
=
block
def
clone
(
self
)
->
PsConditional
:
def
_
clone
_structural
(
self
)
->
PsConditional
:
return
PsConditional
(
return
PsConditional
(
self
.
_condition
.
clone
(),
self
.
_condition
.
clone
(),
self
.
_branch_true
.
clone
(),
self
.
_branch_true
.
clone
(),
...
@@ -344,7 +362,7 @@ class PsPragma(PsLeafMixIn, PsEmptyLeafMixIn, PsStructuralNode):
...
@@ -344,7 +362,7 @@ class PsPragma(PsLeafMixIn, PsEmptyLeafMixIn, PsStructuralNode):
def
text
(
self
)
->
str
:
def
text
(
self
)
->
str
:
return
self
.
_text
return
self
.
_text
def
clone
(
self
)
->
PsPragma
:
def
_
clone
_structural
(
self
)
->
PsPragma
:
return
PsPragma
(
self
.
text
)
return
PsPragma
(
self
.
text
)
def
structurally_equal
(
self
,
other
:
PsAstNode
)
->
bool
:
def
structurally_equal
(
self
,
other
:
PsAstNode
)
->
bool
:
...
@@ -369,7 +387,7 @@ class PsComment(PsLeafMixIn, PsEmptyLeafMixIn, PsStructuralNode):
...
@@ -369,7 +387,7 @@ class PsComment(PsLeafMixIn, PsEmptyLeafMixIn, PsStructuralNode):
def
lines
(
self
)
->
tuple
[
str
,
...]:
def
lines
(
self
)
->
tuple
[
str
,
...]:
return
self
.
_lines
return
self
.
_lines
def
clone
(
self
)
->
PsComment
:
def
_
clone
_structural
(
self
)
->
PsComment
:
return
PsComment
(
self
.
_text
)
return
PsComment
(
self
.
_text
)
def
structurally_equal
(
self
,
other
:
PsAstNode
)
->
bool
:
def
structurally_equal
(
self
,
other
:
PsAstNode
)
->
bool
:
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment