Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

pytholite: use eval instead of literal_eval

  • Loading branch information...
commit b0d467d7443564fb95eca383308e17f93a70f376 1 parent ecc4062
Sébastien Bourdeauducq authored June 28, 2013
18  migen/pytholite/compiler.py
@@ -9,7 +9,7 @@
9 9
 from migen.pytholite.expr import *
10 10
 from migen.pytholite import transel
11 11
 from migen.pytholite.io import gen_io
12  
-from migen.pytholite.fsm import *
  12
+from migen.pytholite.util import *
13 13
 
14 14
 def _is_name_used(node, name):
15 15
 	for n in ast.walk(node):
@@ -99,7 +99,7 @@ def visit_assign_special(self, sa, node, statements):
99 99
 		if callee == transel.Register:
100 100
 			if len(value.args) != 1:
101 101
 				raise TypeError("Register() takes exactly 1 argument")
102  
-			bits_sign = ast.literal_eval(value.args[0])
  102
+			bits_sign = eval_ast(value.args[0], self.symdict)
103 103
 			if isinstance(node.targets[0], ast.Name):
104 104
 				targetname = node.targets[0].id
105 105
 			else:
@@ -206,18 +206,8 @@ def visit_for(self, sa, node):
206 206
 		sa.assemble(states, last_exit_states)
207 207
 	
208 208
 	def visit_iterator(self, node):
209  
-		if isinstance(node, ast.List):
210  
-			return ast.literal_eval(node)
211  
-		elif isinstance(node, ast.Call) and isinstance(node.func, ast.Name):
212  
-			funcname = node.func.id
213  
-			args = map(ast.literal_eval, node.args)
214  
-			if funcname == "range":
215  
-				return range(*args)
216  
-			else:
217  
-				raise NotImplementedError
218  
-		else:
219  
-			raise NotImplementedError
220  
-	
  209
+		return eval_ast(node, self.symdict)
  210
+
221 211
 	def visit_expr_statement(self, sa, node):
222 212
 		if isinstance(node.value, ast.Yield):
223 213
 			yvalue = node.value.value
6  migen/pytholite/expr.py
@@ -4,6 +4,7 @@
4 4
 from migen.fhdl.structure import _Slice
5 5
 from migen.pytholite import transel
6 6
 from migen.pytholite.reg import *
  7
+from migen.pytholite.util import eval_ast
7 8
 
8 9
 class ExprCompiler:
9 10
 	def __init__(self, symdict):
@@ -36,9 +37,9 @@ def visit_expr_call(self, node):
36 37
 			if len(node.args) != 2 and len(node.args) != 3:
37 38
 				raise TypeError("bitslice() takes 2 or 3 arguments")
38 39
 			val = self.visit_expr(node.args[0])
39  
-			low = ast.literal_eval(node.args[1])
  40
+			low = eval_ast(node.args[1], self.symdict)
40 41
 			if len(node.args) == 3:
41  
-				up = ast.literal_eval(node.args[2])
  42
+				up = eval_ast(node.args[2], self.symdict)
42 43
 			else:
43 44
 				up = low + 1
44 45
 			return _Slice(val, low, up)
@@ -111,4 +112,3 @@ def visit_expr_attribute(self, node):
111 112
 	
112 113
 	def visit_expr_subscript(self, node):
113 114
 		raise NotImplementedError
114  
-	
20  migen/pytholite/io.py
@@ -6,7 +6,7 @@
6 6
 from migen.flow.transactions import *
7 7
 from migen.bus import wishbone
8 8
 from migen.bus.transactions import *
9  
-from migen.pytholite.fsm import *
  9
+from migen.pytholite.util import *
10 10
 from migen.pytholite.expr import ExprCompiler
11 11
 
12 12
 class _TokenPullExprCompiler(ExprCompiler):
@@ -25,15 +25,15 @@ def visit_expr_subscript(self, node):
25 25
 		
26 26
 		if not isinstance(node.slice, ast.Index):
27 27
 			raise NotImplementedError
28  
-		field = ast.literal_eval(node.slice.value)
  28
+		field = eval_ast(node.slice.value, self.symdict)
29 29
 		signal = getattr(self.ep.payload, field)
30 30
 		
31 31
 		return signal
32 32
 
33 33
 def _gen_df_io(compiler, modelname, to_model, from_model):
34  
-	epname = ast.literal_eval(to_model["endpoint"])
  34
+	epname = eval_ast(to_model["endpoint"], compiler.symdict)
35 35
 	values = to_model["value"]
36  
-	idle_wait = ast.literal_eval(to_model["idle_wait"])
  36
+	idle_wait = eval_ast(to_model["idle_wait"], compiler.symdict)
37 37
 	ep = getattr(compiler.ioo, epname)
38 38
 	if idle_wait:
39 39
 		state = [compiler.ioo.busy.eq(0)]
@@ -62,7 +62,7 @@ def _gen_df_io(compiler, modelname, to_model, from_model):
62 62
 		if not isinstance(values, ast.Dict):
63 63
 			raise NotImplementedError
64 64
 		for akey, value in zip(values.keys, values.values):
65  
-			key = ast.literal_eval(akey)
  65
+			key = eval_ast(akey, compiler.symdict)
66 66
 			signal = getattr(ep.payload, key)
67 67
 			state.append(signal.eq(compiler.ec.visit_expr(value)))
68 68
 		state += [
@@ -136,7 +136,7 @@ def _gen_memory_io(compiler, modelname, model, to_model, from_model, port):
136 136
 		return [s1, s2], [s2]
137 137
 
138 138
 def _gen_bus_io(compiler, modelname, model, to_model, from_model):
139  
-	busname = ast.literal_eval(to_model["busname"])
  139
+	busname = eval_ast(to_model["busname"], compiler.symdict)
140 140
 	if busname is None:
141 141
 		buses = compiler.ioo.get_buses()
142 142
 		if len(buses) != 1:
@@ -183,8 +183,8 @@ def gen_io(compiler, modelname, model, to_model, to_model_kw, from_model):
183 183
 	if model == Token:
184 184
 		desc = [
185 185
 			"endpoint",
186  
-			("value", ast.Name("None", ast.Load())),
187  
-			("idle_wait", ast.Name("False", ast.Load()))
  186
+			("value", ast.Name("None", ast.Load(), lineno=0, col_offset=0)),
  187
+			("idle_wait", ast.Name("False", ast.Load(), lineno=0, col_offset=0))
188 188
 		]
189 189
 		args = _decode_args(desc, to_model, to_model_kw)
190 190
 		return _gen_df_io(compiler, modelname, args, from_model)
@@ -192,8 +192,8 @@ def gen_io(compiler, modelname, model, to_model, to_model_kw, from_model):
192 192
 		desc = [
193 193
 			"address",
194 194
 			("data", ast.Num(0)),
195  
-			("sel", ast.Name("None", ast.Load())),
196  
-			("busname", ast.Name("None", ast.Load()))
  195
+			("sel", ast.Name("None", ast.Load(), lineno=0, col_offset=0)),
  196
+			("busname", ast.Name("None", ast.Load(), lineno=0, col_offset=0))
197 197
 		]
198 198
 		args = _decode_args(desc, to_model, to_model_kw)
199 199
 		return _gen_bus_io(compiler, modelname, model, args, from_model)
8  migen/pytholite/fsm.py → migen/pytholite/util.py
... ...
@@ -1,3 +1,5 @@
  1
+import ast
  2
+
1 3
 from migen.genlib.fsm import FSM, NextState
2 4
 
3 5
 def id_next_state(l):
@@ -23,3 +25,9 @@ def implement_fsm(states):
23 25
 	for state in states:
24 26
 		fsm.act(id(state), state)
25 27
 	return fsm
  28
+
  29
+def eval_ast(expr, symdict):
  30
+	if not isinstance(expr, ast.Expression):
  31
+		expr = ast.Expression(expr)
  32
+	code = compile(expr, "<ast>", "eval")
  33
+	return eval(code, symdict)

0 notes on commit b0d467d

Please sign in to comment.
Something went wrong with that request. Please try again.