Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

deal with special precedence case of string concatenation #120

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/FAST-Java-Tools-Tests/FASTJavaExportVisitorTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,26 @@ FASTJavaExportVisitorTest >> testVisitFASTJavaExpressionParenthesesNone [
self export: expr equals: '4+5'
]

{ #category : #tests }
FASTJavaExportVisitorTest >> testVisitFASTJavaExpressionParenthesesStringConcatenationLeft [

| expr |
expr := self methodAST: 'void mth() { a="string"+(b+1); }'.

self export: expr statements first equals: 'a = "string"+(b+1);
'
]

{ #category : #tests }
FASTJavaExportVisitorTest >> testVisitFASTJavaExpressionParenthesesStringConcatenationRight [

| expr |
expr := self methodAST: 'void mth() { a=(b+1)+"string"; }'.

self export: expr statements first equals: 'a = (b+1)+"string";
'
]

{ #category : #tests }
FASTJavaExportVisitorTest >> testVisitFASTJavaExpressionParenthesesSubHasHigherPrecedence [

Expand Down
43 changes: 31 additions & 12 deletions src/FAST-Java-Tools/FASTJavaExportVisitor.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,21 @@ FASTJavaExportVisitor class >> initialize [

OperatorsPrecedence := Dictionary newFrom: {
"postfixOperator"
'unary++' -> 14 .
'unary--' -> 14 .
'unary++' -> 15 .
'unary--' -> 15 .
"unaryOperator"
'++unary' -> 13 .
'--unary' -> 13 .
'+unary' -> 13 .
'-unary' -> 13 .
'~unary' -> 13 .
'!unary' -> 13 .
'++unary' -> 14 .
'--unary' -> 14 .
'+unary' -> 14 .
'-unary' -> 14 .
'~unary' -> 14 .
'!unary' -> 14 .
"multiplicativeOperator"
'*' -> 12 .
'/' -> 12 .
'%' -> 12 .
'*' -> 13 .
'/' -> 13 .
'%' -> 13 .
"stringConcatenationOperator -- introduced to deal with string concatenation and numerical '+'"
'strConcat' -> 12 .
"additiveOperator"
'+' -> 11 .
'-' -> 11 .
Expand Down Expand Up @@ -180,6 +182,16 @@ FASTJavaExportVisitor >> initialize [

]

{ #category : #precedence }
FASTJavaExportVisitor >> isStringConcatenation: aFASTJavaInfixOperation [

^(aFASTJavaInfixOperation operator = '+')
and: [
(aFASTJavaInfixOperation leftOperand class = FASTJavaStringLiteral)
or: [ aFASTJavaInfixOperation rightOperand class = FASTJavaStringLiteral ]
]
]

{ #category : #precedence }
FASTJavaExportVisitor >> needsParentheses: operator [
"an expression containing #operator needs parentheses if it is inside another expression
Expand Down Expand Up @@ -640,6 +652,13 @@ FASTJavaExportVisitor >> visitFASTJavaImportDeclaration: aFASTJavaImportDeclarat

{ #category : #'visiting expression' }
FASTJavaExportVisitor >> visitFASTJavaInfixOperation: aFASTJavaInfixOperation [

| operator |

operator := (self isStringConcatenation: aFASTJavaInfixOperation)
ifTrue: [ 'strConcat' ]
ifFalse: [ aFASTJavaInfixOperation operator ].

self checkForComments: aFASTJavaInfixOperation andDo: [
self
expression: [
Expand All @@ -649,7 +668,7 @@ FASTJavaExportVisitor >> visitFASTJavaInfixOperation: aFASTJavaInfixOperation [
ifFalse: [ self noIndent: aFASTJavaInfixOperation operator].
aFASTJavaInfixOperation rightOperand accept: self.
]
withOperator: aFASTJavaInfixOperation operator
withOperator: operator
]

]
Expand Down