From d55b885175bc0438048f63bb0c590cdea337a415 Mon Sep 17 00:00:00 2001 From: fabiovandewaeter Date: Wed, 3 Jul 2024 14:19:25 +0200 Subject: [PATCH] add parenthesis when reference --- .../FamixValueOfType.extension.st | 15 ++++ .../FamixValue2PharoVisitor.class.st | 81 ++++++++++++------- .../RBAssignmentNode.extension.st | 14 ++++ 3 files changed, 80 insertions(+), 30 deletions(-) create mode 100644 src/Famix-Value-Exporter/RBAssignmentNode.extension.st diff --git a/src/Famix-Value-Entities-Extensions/FamixValueOfType.extension.st b/src/Famix-Value-Entities-Extensions/FamixValueOfType.extension.st index 18acf66..1760cf6 100644 --- a/src/Famix-Value-Entities-Extensions/FamixValueOfType.extension.st +++ b/src/Famix-Value-Entities-Extensions/FamixValueOfType.extension.st @@ -12,6 +12,21 @@ FamixValueOfType >> containsObject [ ^ false ] +{ #category : #'*Famix-Value-Entities-Extensions' } +FamixValueOfType >> isReferencedInLoop [ + + | count | + count := self referenceCount. + ^ count > 1 or: [ self isRoot and: [ count = 1 ] ] +] + +{ #category : #'*Famix-Value-Entities-Extensions' } +FamixValueOfType >> isRoot [ + + ^ self argumentInSpan isNotNil or: [ + self resultInSpan isNotNil or: [ self receiverInSpan isNotNil ] ] +] + { #category : #'*Famix-Value-Entities-Extensions' } FamixValueOfType >> mooseNameOn: aStream [ diff --git a/src/Famix-Value-Exporter/FamixValue2PharoVisitor.class.st b/src/Famix-Value-Exporter/FamixValue2PharoVisitor.class.st index d2788bf..d9f7652 100644 --- a/src/Famix-Value-Exporter/FamixValue2PharoVisitor.class.st +++ b/src/Famix-Value-Exporter/FamixValue2PharoVisitor.class.st @@ -10,25 +10,36 @@ Class { #category : #'Famix-Value-Exporter' } +{ #category : #visiting } +FamixValue2PharoVisitor >> addParenthesis: aNode [ + "add parenthesis when the node as multiple references" + + | receiver newNode | + receiver := RBAssignmentNode new + variable: aNode variable; + value: (aNode value messages at: 1) receiver. + newNode := aNode value. + (newNode messages at: 1) receiver: receiver. + ^ newNode +] + { #category : #testing } FamixValue2PharoVisitor >> ensureVisited: value [ - | node | - self varNameDict - at: value - ifPresent: [ :name | ^ RBVariableNode named: name ] - ifAbsentPut: [ - node := value accept: self. - value referenceCount > 1 - ifTrue: [ - | name | - name := self varNameFor: value. - node := RBAssignmentNode - variable: (RBVariableNode named: name) - value: node. - name ] - ifFalse: [ nil ] ]. - ^ node + ^ self varNameDict + at: value + ifPresent: [ :name | RBVariableNode named: name ] + ifAbsent: [ + | name node | + value isOfPrimitiveType ifFalse: [ + self varNameDict at: value put: (name := self varNameFor: value) ]. + node := value accept: self. + value isReferencedInLoop + ifTrue: [ + RBAssignmentNode + variable: (RBVariableNode named: name) + value: node ] + ifFalse: [ node ] ] ] { #category : #ast } @@ -80,7 +91,12 @@ FamixValue2PharoVisitor >> visitCollection: collection [ | collectionNode | collectionNode := RBArrayNode statements: (collection value collect: [ :element | - self ensureVisited: element value ]). + | newNode | + newNode := self ensureVisited: element value. + (element value isReferencedInLoop and: [ + newNode isVariable not ]) + ifTrue: [ newNode addParenthesisToVariable] + ifFalse: [ newNode ] ]). ^ collection type name = 'Array' ifTrue: [ collectionNode ] ifFalse: [ @@ -139,20 +155,25 @@ FamixValue2PharoVisitor >> visitObject: object [ { #category : #visiting } FamixValue2PharoVisitor >> visitObjectAttribute: attribute [ + | newNode | attribute attribute ifNil: [ "ignore unknown attributes" ^ nil ]. - ^ (attribute object type findSetterOf: attribute attribute) - ifNotNil: [ :setter | - RBMessageNode - receiver: RBVariableNode new - selector: setter name - arguments: { (self ensureVisited: attribute value) } ] - ifNil: [ "Use reflectivity" - RBMessageNode - receiver: RBVariableNode new - selector: #instVarNamed:put: - arguments: { - (RBVariableNode named: '#' , attribute attribute name). - (self ensureVisited: attribute value) } ] + (attribute object type findSetterOf: attribute attribute) + ifNotNil: [ :setter | + newNode := RBMessageNode + receiver: RBVariableNode new + selector: setter name + arguments: { (self ensureVisited: attribute value) } ] + ifNil: [ "Use reflectivity" + newNode := RBMessageNode + receiver: RBVariableNode new + selector: #instVarNamed:put: + arguments: { + (RBVariableNode named: '#' , attribute attribute name). + (self ensureVisited: attribute value) } ]. + + attribute value isReferencedInLoop ifTrue: [ + ^ newNode addParenthesisToVariable]. + ^ newNode ] { #category : #visiting } diff --git a/src/Famix-Value-Exporter/RBAssignmentNode.extension.st b/src/Famix-Value-Exporter/RBAssignmentNode.extension.st new file mode 100644 index 0000000..d4d8f52 --- /dev/null +++ b/src/Famix-Value-Exporter/RBAssignmentNode.extension.st @@ -0,0 +1,14 @@ +Extension { #name : #RBAssignmentNode } + +{ #category : #'*Famix-Value-Exporter' } +RBAssignmentNode >> addParenthesisToVariable [ + "add parenthesis because the node as multiple references" + + | receiver newNode | + receiver := RBAssignmentNode new + variable: self variable; + value: (self value messages at: 1) receiver. + newNode := self value. + (newNode messages at: 1) receiver: receiver. + ^ newNode +]