compiler/ir: support AugAssign

This commit is contained in:
Sebastien Bourdeauducq 2014-07-16 09:43:46 -06:00
parent 591509d31b
commit a9b96cefbe
1 changed files with 15 additions and 8 deletions

View File

@ -22,6 +22,14 @@ class _Namespace:
def load(self, builder, name): def load(self, builder, name):
return builder.load(self.bindings[name]) return builder.load(self.bindings[name])
_binop_to_builder = {
ast.Add: "add",
ast.Sub: "sub",
ast.Mult: "mul",
ast.FloorDiv: "sdiv",
ast.Mod: "srem"
}
def _emit_expr(env, builder, ns, node): def _emit_expr(env, 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)
@ -30,14 +38,7 @@ def _emit_expr(env, builder, ns, node):
elif isinstance(node, ast.BinOp): elif isinstance(node, ast.BinOp):
left = _emit_expr(env, builder, ns, node.left) left = _emit_expr(env, builder, ns, node.left)
right = _emit_expr(env, builder, ns, node.right) right = _emit_expr(env, builder, ns, node.right)
mapping = { bf = getattr(builder, _binop_to_builder[type(node.op)])
ast.Add: builder.add,
ast.Sub: builder.sub,
ast.Mult: builder.mul,
ast.FloorDiv: builder.sdiv,
ast.Mod: builder.srem
}
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 = []
@ -80,6 +81,12 @@ def _emit_statements(env, builder, ns, stmts):
ns.store(builder, val, target.id) ns.store(builder, val, target.id)
else: else:
raise NotImplementedError raise NotImplementedError
elif isinstance(stmt, ast.AugAssign):
left = _emit_expr(env, builder, ns, stmt.target)
right = _emit_expr(env, builder, ns, stmt.value)
bf = getattr(builder, _binop_to_builder[type(stmt.op)])
result = bf(left, right)
ns.store(builder, result, stmt.target.id)
elif isinstance(stmt, ast.If): elif isinstance(stmt, ast.If):
function = builder.basic_block.function function = builder.basic_block.function
then_block = function.append_basic_block("i_then") then_block = function.append_basic_block("i_then")