From 10d51cc29ca5dec5c39e973a7a3858424e7e2f48 Mon Sep 17 00:00:00 2001 From: greg Date: Mon, 26 Dec 2016 00:48:57 -0800 Subject: [PATCH] Compile binexps --- main.schala | 2 +- src/compilation.rs | 18 ++++++++++++++++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/main.schala b/main.schala index 85dbcea..6d7c389 100644 --- a/main.schala +++ b/main.schala @@ -1,5 +1,5 @@ fn main() - 89 + 25 % 20 end diff --git a/src/compilation.rs b/src/compilation.rs index 823178f..329a41a 100644 --- a/src/compilation.rs +++ b/src/compilation.rs @@ -212,7 +212,6 @@ impl CodeGen for Function { } } - impl CodeGen for Expression { fn codegen(&self, context: LLVMContextRef, builder: LLVMBuilderRef) -> LLVMValueRef { use self::Expression::*; @@ -221,7 +220,22 @@ impl CodeGen for Expression { match self { &BinExp(ref op, ref left, ref right) => { - unimplemented!() + let lhs = left.codegen(context, builder); + let rhs = right.codegen(context, builder); + unsafe { + let generator = match op.as_ref() { + "+" => core::LLVMBuildAdd, + "-" => core::LLVMBuildSub, + "*" => core::LLVMBuildMul, + "/" => core::LLVMBuildUDiv, + "%" => core::LLVMBuildSRem, + _ => panic!("Bad operator {}", op), + + }; + + let reg_name = CString::new("temporary").unwrap(); + generator(builder, lhs, rhs, reg_name.as_ptr()) + } }, &Number(ref n) => { let native_val = *n as u64;