nac3core: AugAssign support (#82)

This commit is contained in:
ychenfo 2021-11-12 01:44:12 +08:00 committed by Sebastien Bourdeauducq
parent 9ae08d6e3d
commit 8ab3ee9cce
3 changed files with 26 additions and 2 deletions

View File

@ -143,7 +143,7 @@ impl<'ctx, 'a> CodeGenContext<'ctx, 'a> {
} }
} }
fn gen_int_ops( pub fn gen_int_ops(
&mut self, &mut self,
op: &Operator, op: &Operator,
lhs: BasicValueEnum<'ctx>, lhs: BasicValueEnum<'ctx>,
@ -178,7 +178,7 @@ impl<'ctx, 'a> CodeGenContext<'ctx, 'a> {
} }
} }
fn gen_float_ops( pub fn gen_float_ops(
&mut self, &mut self,
op: &Operator, op: &Operator,
lhs: BasicValueEnum<'ctx>, lhs: BasicValueEnum<'ctx>,

View File

@ -340,6 +340,26 @@ pub fn gen_stmt<'ctx, 'a, G: CodeGenerator + ?Sized>(
StmtKind::While { .. } => return generator.gen_while(ctx, stmt), StmtKind::While { .. } => return generator.gen_while(ctx, stmt),
StmtKind::For { .. } => return generator.gen_for(ctx, stmt), StmtKind::For { .. } => return generator.gen_for(ctx, stmt),
StmtKind::With { .. } => return generator.gen_with(ctx, stmt), StmtKind::With { .. } => return generator.gen_with(ctx, stmt),
StmtKind::AugAssign { target, op, value, .. } => {
let value = {
let ty1 = ctx.unifier.get_representative(target.custom.unwrap());
let ty2 = ctx.unifier.get_representative(value.custom.unwrap());
let left = generator.gen_expr(ctx, target).unwrap();
let right = generator.gen_expr(ctx, value).unwrap();
// we can directly compare the types, because we've got their representatives
// which would be unchanged until further unification, which we would never do
// when doing code generation for function instances
if ty1 == ty2 && [ctx.primitives.int32, ctx.primitives.int64].contains(&ty1) {
ctx.gen_int_ops(op, left, right)
} else if ty1 == ty2 && ctx.primitives.float == ty1 {
ctx.gen_float_ops(op, left, right)
} else {
unimplemented!()
}
};
generator.gen_assign(ctx, target, value);
}
_ => unimplemented!(), _ => unimplemented!(),
}; };
false false

View File

@ -313,6 +313,10 @@ impl<'a> fold::Fold<()> for Inferencer<'a> {
} }
(None, None) => {} (None, None) => {}
}, },
ast::StmtKind::AugAssign { target, op, value, .. } => {
let res_ty = self.infer_bin_ops(stmt.location, target, op, value)?;
self.unify(res_ty, target.custom.unwrap(), &stmt.location)?;
}
_ => return report_error("Unsupported statement type", stmt.location), _ => return report_error("Unsupported statement type", stmt.location),
}; };
Ok(stmt) Ok(stmt)