Skip to content

Commit

Permalink
pytholite/compiler: support range(constants) in for loops
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastien Bourdeauducq committed Nov 10, 2012
1 parent 370bab1 commit 37f113c
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion migen/pytholite/compiler.py
Expand Up @@ -128,7 +128,7 @@ def visit_statement(self, statement):
target = statement.target.id
if target in self.symdict:
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 = []
for iteration in it:
self.symdict[target] = iteration
Expand All @@ -143,6 +143,19 @@ def visit_statement(self, statement):
raise NotImplementedError
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):
if isinstance(node.targets[0], ast.Name):
self.targetname = node.targets[0].id
Expand Down

0 comments on commit 37f113c

Please sign in to comment.