compiler/ir: simplify type dictionaries

This commit is contained in:
Sebastien Bourdeauducq 2014-06-22 00:04:46 +02:00
parent b28fdf5fb0
commit 6ea0430cec

View File

@ -21,12 +21,6 @@ class _Namespace:
def load(self, builder, name): def load(self, builder, name):
return builder.load(self.bindings[name]) return builder.load(self.bindings[name])
def _dict_isinstance(d, instance):
for k, v in d.items():
if isinstance(instance, getattr(ast, k)):
return v
raise NotImplementedError
def _emit_expr(builder, ns, node): def _emit_expr(builder, ns, node):
if isinstance(node, ast.Name): if isinstance(node, ast.Name):
return ns.load(builder, node.id) return ns.load(builder, node.id)
@ -36,13 +30,13 @@ def _emit_expr(builder, ns, node):
left = _emit_expr(builder, ns, node.left) left = _emit_expr(builder, ns, node.left)
right = _emit_expr(builder, ns, node.right) right = _emit_expr(builder, ns, node.right)
mapping = { mapping = {
"Add": builder.add, ast.Add: builder.add,
"Sub": builder.sub, ast.Sub: builder.sub,
"Mult": builder.mul, ast.Mult: builder.mul,
"FloorDiv": builder.sdiv, ast.FloorDiv: builder.sdiv,
"Mod": builder.srem ast.Mod: builder.srem
} }
bf = _dict_isinstance(mapping, node.op) bf = mapping[type(node.op)]
return bf(left, right) return bf(left, right)
elif isinstance(node, ast.Compare): elif isinstance(node, ast.Compare):
comparisons = [] comparisons = []
@ -50,14 +44,14 @@ def _emit_expr(builder, ns, node):
for op, comparator_a in zip(node.ops, node.comparators): for op, comparator_a in zip(node.ops, node.comparators):
comparator = _emit_expr(builder, ns, comparator_a) comparator = _emit_expr(builder, ns, comparator_a)
mapping = { mapping = {
"Eq": lc.ICMP_EQ, ast.Eq: lc.ICMP_EQ,
"NotEq": lc.ICMP_NE, ast.NotEq: lc.ICMP_NE,
"Lt": lc.ICMP_SLT, ast.Lt: lc.ICMP_SLT,
"LtE": lc.ICMP_SLE, ast.LtE: lc.ICMP_SLE,
"Gt": lc.ICMP_SGT, ast.Gt: lc.ICMP_SGT,
"GtE": lc.ICMP_SGE ast.GtE: lc.ICMP_SGE
} }
comparison = builder.icmp(_dict_isinstance(mapping, op), old_comparator, comparator) comparison = builder.icmp(mapping[type(op)], old_comparator, comparator)
comparisons.append(comparison) comparisons.append(comparison)
old_comparator = comparator old_comparator = comparator
r = comparisons[0] r = comparisons[0]