PetitCommonMark/software/petitcompiler/PPCChoiceOptimizationVisitor.class.st

35 lines
720 B
Smalltalk

Class {
#name : 'PPCChoiceOptimizationVisitor',
#superclass : 'PPCRewritingVisitor',
#category : 'PetitCompiler-Visitors'
}
{ #category : 'visiting' }
PPCChoiceOptimizationVisitor >> rejectDuplicateChildren: node [
| seen newChildren |
seen := IdentitySet new.
newChildren := OrderedCollection new.
node children do: [ :child |
(seen includes: child) ifFalse: [
newChildren add: child
].
seen add: child
].
^ newChildren
]
{ #category : 'visiting' }
PPCChoiceOptimizationVisitor >> visitChoiceNode: node [
self visitChildren: node.
"Remove the identical children"
node children: (self rejectDuplicateChildren: node).
(node children size = 1) ifTrue: [
^ node firstChild
].
^ node
]