pytholite/compiler: support range(constants) in for loops

This commit is contained in:
Sebastien Bourdeauducq 2012-11-10 15:26:13 +01:00
parent 370bab1190
commit 37f113c3ea
1 changed files with 14 additions and 1 deletions

View File

@ -128,7 +128,7 @@ class _Compiler:
target = statement.target.id target = statement.target.id
if target in self.symdict: if target in self.symdict:
raise NotImplementedError("For loop target must use an available name") raise NotImplementedError("For loop target must use an available name")
it = ast.literal_eval(statement.iter) it = self.visit_iterator(statement.iter)
last_exit_states = [] last_exit_states = []
for iteration in it: for iteration in it:
self.symdict[target] = iteration self.symdict[target] = iteration
@ -143,6 +143,19 @@ class _Compiler:
raise NotImplementedError raise NotImplementedError
return states, exit_states return states, exit_states
def visit_iterator(self, node):
if isinstance(node, ast.List):
return ast.literal_eval(node)
elif isinstance(node, ast.Call) and isinstance(node.func, ast.Name):
funcname = node.func.id
args = map(ast.literal_eval, node.args)
if funcname == "range":
return range(*args)
else:
raise NotImplementedError
else:
raise NotImplementedError
def visit_assign(self, node): def visit_assign(self, node):
if isinstance(node.targets[0], ast.Name): if isinstance(node.targets[0], ast.Name):
self.targetname = node.targets[0].id self.targetname = node.targets[0].id