simple_math() test

This commit is contained in:
mars 2022-03-26 09:50:49 -06:00
parent 3626e2c994
commit f45cbe64f3
1 changed files with 12 additions and 5 deletions

View File

@ -52,7 +52,7 @@ impl Engine {
pub fn translate(&mut self, fn_impl: &ast::FnImpl) -> Result<(), String> {
let int = self.module.target_config().pointer_type();
let builder = FunctionBuilder::new(&mut self.ctx.func, &mut self.builder_context);
let mut trans = super::translate::FunctionTranslator { int, builder };
let trans = super::translate::FunctionTranslator { int, builder };
trans.translate(fn_impl);
Ok(())
}
@ -62,19 +62,26 @@ impl Engine {
mod tests {
use super::*;
#[test]
fn simple_math() {
fn jit_compile(source: &str) -> *const u8 {
use crate::parse::lexer::Lexer;
use crate::parse::rd::RecursiveDescent;
let source = "simple_math() i64 { (1 + 2) * (3 / (4 + 5)) }";
println!("JIT-compiling source: {}", source);
let lexer = Lexer::new(source);
let mut rd = RecursiveDescent::new(lexer);
let def = rd.build_fn(false, None);
if let ast::Definition::Function { implementation, .. } = def {
Engine::new().compile(&implementation).unwrap();
Engine::new().compile(&implementation).unwrap()
} else {
panic!("Failed to parse function from test source");
}
}
#[test]
fn simple_math() {
let source = "simple_math() i64 { (1 + 2) * ((4 + 5) / 3) }";
let code_ptr = jit_compile(source);
let code_fn = unsafe { std::mem::transmute::<_, fn() -> i64>(code_ptr) };
assert_eq!(code_fn(), 9);
}
}