PetitCommonMark/software/petitcompiler/PPCRewritingVisitor.class.st

51 lines
1.3 KiB
Smalltalk

"
I do support graph rewriting...
And I should be able to work fine with cyclic structures as well and at the same time allow for customization of the traversal strategy.
While traversing cyclic structures (where node refers to itself later) the key point is to cache the new value for the node (or at least the stub of the new value) before visiting the children of the node.
"
Class {
#name : 'PPCRewritingVisitor',
#superclass : 'PPCPassVisitor',
#category : 'PetitCompiler-Visitors'
}
{ #category : 'as yet unclassified' }
PPCRewritingVisitor >> afterAccept: node retval: retval [
self cache: node value: retval ifPresent: [ :e | "I am fine with it" ].
^ retval
]
{ #category : 'as yet unclassified' }
PPCRewritingVisitor >> change [
]
{ #category : 'as yet unclassified' }
PPCRewritingVisitor >> openDetected: node [
(self isCached: node) ifTrue: [
^ self cachedValue: node
].
^ node
]
{ #category : 'as yet unclassified' }
PPCRewritingVisitor >> visitChild: child of: node [
| newChild |
" Halt if: [ node name = 'nullToken' ]."
newChild := self visit: child.
(newChild == child) ifFalse: [
node replace: child with: newChild.
].
]
{ #category : 'as yet unclassified' }
PPCRewritingVisitor >> visitChildren: node [
node children do: [ :child |
self visitChild: child of: node
]
]