Skip to content

Commit 1b428df

Browse files
author
Troy Melhase
committed
Updates walk() calls to pass memo in all cases. Fixes natural#13.
1 parent 6548316 commit 1b428df

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

java2python/compiler/visitor.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def insertComments(self, tmpl, tree, index, memo):
4646
""" Add comments to the template from tokens in the tree. """
4747
prefix = self.config.last('commentPrefix', '# ')
4848
cache, parser, comTypes = memo.comments, tree.parser, tokens.commentTypes
49-
comNew = lambda t:t.type in comTypes and t.index not in cache
49+
comNew = lambda t:t.type in comTypes and (t.index not in cache)
5050

5151
for tok in ifilter(comNew, parser.input.tokens[memo.last:index]):
5252
cache.add(tok.index)
@@ -173,7 +173,7 @@ def nodesToAnnos(self, branch, memo):
173173
if defKey:
174174
deco = self.factory.expr(left=name, fs='@{left}({right})')
175175
deco.right = right = self.factory.expr(parent=deco)
176-
right.walk(defKey.firstChild())
176+
right.walk(defKey.firstChild(), memo)
177177
else:
178178
deco = self.factory.expr(left=name, fs='@{left}({right})')
179179
arg = deco.right = self.factory.expr(parent=deco)
@@ -432,17 +432,20 @@ def acceptExpr(self, node, memo):
432432

433433
def acceptFor(self, node, memo):
434434
""" Accept and process a 'for' statement. """
435-
self.walk(node.firstChildOfType(tokens.FOR_INIT))
435+
self.walk(node.firstChildOfType(tokens.FOR_INIT), memo)
436436
whileStat = self.factory.statement('while', fs=FS.lsrc, parent=self)
437437
cond = node.firstChildOfType(tokens.FOR_CONDITION)
438438
if not cond.children:
439439
whileStat.expr.right = 'True'
440440
else:
441-
whileStat.expr.walk(cond)
441+
whileStat.expr.walk(cond, memo)
442442
whileBlock = self.factory.methodContent(parent=self)
443-
whileBlock.walk(node.firstChildOfType(tokens.BLOCK_SCOPE))
443+
if not node.firstChildOfType(tokens.BLOCK_SCOPE).children:
444+
self.factory.expr(left='pass', parent=whileBlock)
445+
else:
446+
whileBlock.walk(node.firstChildOfType(tokens.BLOCK_SCOPE), memo)
444447
updateStat = self.factory.expr(parent=whileBlock)
445-
updateStat.walk(node.firstChildOfType(tokens.FOR_UPDATE))
448+
updateStat.walk(node.firstChildOfType(tokens.FOR_UPDATE), memo)
446449

447450
def acceptForEach(self, node, memo):
448451
""" Accept and process a 'for each' style statement. """
@@ -505,7 +508,7 @@ def acceptSwitch(self, node, memo):
505508
return
506509
# we have at least one node...
507510
parExpr = self.factory.expr(parent=self)
508-
parExpr.walk(parNode)
511+
parExpr.walk(parNode, memo)
509512
eqFs = FS.l + '==' + FS.r
510513
for caseIdx, caseNode in enumerate(caseNodes):
511514
isDefault, isFirst = caseNode.type==tokens.DEFAULT, caseIdx==0
@@ -519,7 +522,7 @@ def acceptSwitch(self, node, memo):
519522

520523
if not isDefault:
521524
right = self.factory.expr(parent=parExpr)
522-
right.walk(caseNode.firstChildOfType(tokens.EXPR))
525+
right.walk(caseNode.firstChildOfType(tokens.EXPR), memo)
523526
caseExpr.expr.right = self.factory.expr(left=parExpr, right=right, fs=eqFs)
524527
caseContent = self.factory.methodContent(parent=self)
525528
for child in caseNode.children[1:]:

test/Comments4.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Header Comment
2+
3+
class Comments4 {
4+
void method() {
5+
for (;;) { }
6+
}
7+
8+
public static void main(String[] args) {
9+
System.out.println("Comments4.");
10+
}
11+
12+
}

0 commit comments

Comments
 (0)