Skip to content
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
29 changes: 28 additions & 1 deletion java2python/compiler/visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,6 @@ def acceptPreformatted(self, node, memo):
return acceptPreformatted

acceptArrayElementAccess = makeNodePreformattedExpr(FS.l + '[' + FS.r + ']')
acceptCastExpr = makeNodePreformattedExpr(FS.l + '(' + FS.r + ')' ) # problem?
acceptDiv = makeNodePreformattedExpr(FS.l + ' / ' + FS.r)
acceptLogicalAnd = makeNodePreformattedExpr(FS.l + ' and ' + FS.r)
acceptLogicalNot = makeNodePreformattedExpr('not ' + FS.l)
Expand All @@ -690,6 +689,34 @@ def acceptPreformatted(self, node, memo):
acceptUnaryMinus = makeNodePreformattedExpr('-' + FS.l)
acceptUnaryPlus = makeNodePreformattedExpr('+' + FS.l)

def acceptCastExpr(self, node, memo):
""" Accept and process a cast expression. """
# If the type of casting is a primitive type,
# then do the cast, else drop it.
factory = self.factory.expr
typeTok = node.firstChildOfType(tokens.TYPE)
typeIdent = typeTok.firstChild()
typeName = typeIdent.text
if typeIdent.type == tokens.QUALIFIED_TYPE_IDENT:
typeName = typeIdent.firstChild().text

if typeName in tokens.primitiveTypeNames:
# Cast using the primitive type constructor
self.fs = typeName + '(' + FS.r + ')'
else:
mode = self.config.last('objCastMode')
if mode == 'drop':
# Use drop policy
self.fs = FS.r
elif mode == 'ctor':
# Make constructor
self.fs = FS.l + '(' + FS.r + ')'
else:
warn('Couldn\'t perform cast operation. ' + typeName + \
' is not a primitive and objCastMode in the config file has wrong value.')
self.left, self.right = visitors = factory(parent=self), factory(parent=self)
self.zipWalk(node.children, visitors, memo)

def makeAcceptPrePost(suffix, pre):
""" Make an accept method for pre- and post- assignment expressions. """
def acceptPrePost(self, node, memo):
Expand Down
9 changes: 9 additions & 0 deletions java2python/config/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,15 @@
# convenience.


# Specifies how cast operations of non-primitive types are handled
# (primitive types are automatically handled)
# Valid values:
# - 'drop': completely drops type and cast info
# - 'ctor': converts the cast in a constructor call
# E.g.: (Cast) x -> Cast(x)
objCastMode = 'drop'


# module output subs.
moduleOutputSubs = [
(r'System\.out\.println\((.*)\)', r'print \1'),
Expand Down
5 changes: 5 additions & 0 deletions java2python/lang/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ def methodTypes(self):
mod = self.module
return (mod.VOID_METHOD_DECL, mod.FUNCTION_METHOD_DECL, )

@property
def primitiveTypeNames(self):
""" Type name of well-known primitive types """
return ('bool', 'str', 'int', 'long', 'float', )

@property
def module(self):
""" Provides lazy import to the parser module. """
Expand Down