forked from M-Labs/artiq
1
0
Fork 0

transforms/unroll_loops: count statements in try blocks

This commit is contained in:
Sebastien Bourdeauducq 2014-09-27 11:57:47 +08:00
parent 2b948ba267
commit 7289c18a42
1 changed files with 11 additions and 4 deletions

View File

@ -4,12 +4,19 @@ from artiq.transforms.tools import eval_ast, value_to_ast
def _count_stmts(node): def _count_stmts(node):
if isinstance(node, (ast.For, ast.While, ast.If)): if isinstance(node, list):
return 1 + _count_stmts(node.body) + _count_stmts(node.orelse) return sum(map(_count_stmts, node))
elif isinstance(node, ast.With): elif isinstance(node, ast.With):
return 1 + _count_stmts(node.body) return 1 + _count_stmts(node.body)
elif isinstance(node, list): elif isinstance(node, (ast.For, ast.While, ast.If)):
return sum(map(_count_stmts, node)) return 1 + _count_stmts(node.body) + _count_stmts(node.orelse)
elif isinstance(node, ast.Try):
r = 1 + _count_stmts(node.body) \
+ _count_stmts(node.orelse) \
+ _count_stmts(node.finalbody)
for handler in node.handlers:
r += 1 + _count_stmts(handler.body)
return r
else: else:
return 1 return 1