Skip to content

Commit

Permalink
only offset indices when native typemap vector/matrix access is used,…
Browse files Browse the repository at this point in the history
… fixes the last problem in shogun-toolbox#3282 and is followup for shogun-toolbox#3550
  • Loading branch information
karlnapf authored and abhinavrai44 committed Jan 24, 2017
1 parent 4369a2b commit c51547b
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions examples/meta/generator/translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,6 @@ def translateElementAccess(self, elementAccess, expr=None):
"""
identifier = elementAccess[0]["Identifier"]
indexList = elementAccess[1]["IndexList"]
indexListTranslation = self.translateIndexList(indexList)

assert identifier in self.variableTypes,\
"Variable {} not initialised".format(identifier)
Expand All @@ -574,8 +573,10 @@ def translateElementAccess(self, elementAccess, expr=None):
targetDict = self.targetDict["Element"]["Assign"]
exprString = self.translateExpr(expr)

indexOffsetRequired=True
if type in targetDict:
template = Template(targetDict[type])
indexOffsetRequired=False
elif len(indexList) == 1:
template = Template(targetDict["Vector"])
elif len(indexList) == 2:
Expand All @@ -585,16 +586,21 @@ def translateElementAccess(self, elementAccess, expr=None):
"(vector) or 2 indices (matrix). Given "
" " + str(len(indexList)) + " indices")

# only potentially offset index lists if native Vector or Matrix assignment is used
# otherwise, it is not possible to call native shogun vector/matrix methods
# (which are zero indexed)
indexListTranslation = self.translateIndexList(indexList, indexOffsetRequired)

return template.substitute(identifier=identifier,
indices=indexListTranslation,
expr=exprString)

def translateIndexList(self, indexList):
def translateIndexList(self, indexList, indexOffsetRequired):
""" Translate index list AST
Args:
indexList: object like [IntLiteralAST, IntLiteralAST, ..]
"""
addOne = not self.targetDict["Element"]["ZeroIndexed"]
addOne = not self.targetDict["Element"]["ZeroIndexed"] and indexOffsetRequired
translation = ""
for idx, intLiteral in enumerate(indexList):
index = int(intLiteral["IntLiteral"])
Expand Down

0 comments on commit c51547b

Please sign in to comment.