2
0
mirror of https://github.com/m-labs/artiq.git synced 2024-12-25 11:18:27 +08:00

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

View File

@ -4,12 +4,19 @@ from artiq.transforms.tools import eval_ast, value_to_ast
def _count_stmts(node):
if isinstance(node, (ast.For, ast.While, ast.If)):
return 1 + _count_stmts(node.body) + _count_stmts(node.orelse)
if isinstance(node, list):
return sum(map(_count_stmts, node))
elif isinstance(node, ast.With):
return 1 + _count_stmts(node.body)
elif isinstance(node, list):
return sum(map(_count_stmts, node))
elif isinstance(node, (ast.For, ast.While, ast.If)):
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:
return 1