Refactoring in compiling binops

This commit is contained in:
greg 2017-01-13 22:29:17 -08:00
parent d804efdc5e
commit a6773d59bd

View File

@ -59,8 +59,8 @@ fn compile_ast(ast: AST, filename: &str) {
let mut data = CompilationData { let mut data = CompilationData {
context: context, context: context,
module: module,
builder: builder, builder: builder,
module: module,
variables: names, variables: names,
func: None, func: None,
}; };
@ -148,16 +148,19 @@ impl CodeGen for Expression {
BinExp(ref op, ref left, ref right) => { BinExp(ref op, ref left, ref right) => {
let lhs = left.codegen(data); let lhs = left.codegen(data);
let rhs = right.codegen(data); let rhs = right.codegen(data);
let generator = match *op { macro_rules! simple_binop {
Add => LLVMWrap::BuildAdd, ($fnname: expr) => {
Sub => LLVMWrap::BuildSub, $fnname(data.builder, lhs, rhs, "temp")
Mul => LLVMWrap::BuildMul, }
Div => LLVMWrap::BuildUDiv, }
Mod => LLVMWrap::BuildSRem, match *op {
Add => simple_binop!(LLVMWrap::BuildAdd),
Sub => simple_binop!(LLVMWrap::BuildSub),
Mul => simple_binop!(LLVMWrap::BuildMul),
Div => simple_binop!(LLVMWrap::BuildUDiv),
Mod => simple_binop!(LLVMWrap::BuildSRem),
_ => panic!("Bad operator {:?}", op), _ => panic!("Bad operator {:?}", op),
}; }
generator(data.builder, lhs, rhs, "temp")
} }
Number(ref n) => { Number(ref n) => {
let native_val = *n as u64; let native_val = *n as u64;