Skip to content

Commit 2922fbd

Browse files
committed
Cast traslated only for primitives
1 parent 764be76 commit 2922fbd

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

java2python/compiler/visitor.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -678,7 +678,6 @@ def acceptPreformatted(self, node, memo):
678678
return acceptPreformatted
679679

680680
acceptArrayElementAccess = makeNodePreformattedExpr(FS.l + '[' + FS.r + ']')
681-
acceptCastExpr = makeNodePreformattedExpr(FS.l + '(' + FS.r + ')' ) # problem?
682681
acceptDiv = makeNodePreformattedExpr(FS.l + ' / ' + FS.r)
683682
acceptLogicalAnd = makeNodePreformattedExpr(FS.l + ' and ' + FS.r)
684683
acceptLogicalNot = makeNodePreformattedExpr('not ' + FS.l)
@@ -690,6 +689,25 @@ def acceptPreformatted(self, node, memo):
690689
acceptUnaryMinus = makeNodePreformattedExpr('-' + FS.l)
691690
acceptUnaryPlus = makeNodePreformattedExpr('+' + FS.l)
692691

692+
def acceptCastExpr(self, node, memo): # problem?
693+
""" Accept and process a cast expression. """
694+
# If the type of casting is a primitive type,
695+
# then do the cast, else drop it.
696+
factory = self.factory.expr
697+
typeTok = node.firstChildOfType(tokens.TYPE)
698+
typeIdent = typeTok.firstChild()
699+
typeName = typeIdent.text
700+
if typeIdent.type == tokens.QUALIFIED_TYPE_IDENT:
701+
typeName = typeIdent.firstChild().text
702+
703+
if typeName in tokens.primitiveTypeNames:
704+
# Cast using the primitive type constructor
705+
self.fs = typeName + '(' + FS.r + ')'
706+
else:
707+
self.fs = FS.r
708+
self.left, self.right = visitors = factory(parent=self), factory(parent=self)
709+
self.zipWalk(node.children, visitors, memo)
710+
693711
def makeAcceptPrePost(suffix, pre):
694712
""" Make an accept method for pre- and post- assignment expressions. """
695713
def acceptPrePost(self, node, memo):

java2python/lang/base.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ def methodTypes(self):
8888
mod = self.module
8989
return (mod.VOID_METHOD_DECL, mod.FUNCTION_METHOD_DECL, )
9090

91+
@property
92+
def primitiveTypeNames(self):
93+
""" Type name of well-known primitive types """
94+
return ('bool', 'str', 'int', 'long', 'float', )
95+
9196
@property
9297
def module(self):
9398
""" Provides lazy import to the parser module. """

0 commit comments

Comments
 (0)