Skip to content

Commit

Permalink
Move integration tests out (to depend on binary) and fix application …
Browse files Browse the repository at this point in the history
…arguments
  • Loading branch information
gsps committed Jun 8, 2020
1 parent 314368d commit ad26dc1
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 45 deletions.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ path = "src/lib.rs"
name = "l3jit"
path = "src/bin/l3jit.rs"

[[test]]
name = "run_tests"
path = "src/tests/run_tests.rs"

[dependencies]
l3_llvm_runtime = { path = "runtime" }
pest = "2.1.3"
Expand Down
8 changes: 0 additions & 8 deletions src/bin/l3jit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,6 @@ fn run_str<'a>(example_str: &'a str) {
l3::codegen::compile_and_run_program(&program)
}

fn example_halt() {
run_str("(halt 3)");
}

fn example_if_basic() {
run_str("(let* ((c1 (cnt () (halt 1))) (c2 (cnt () (halt 2)))) (if (@< 2 1) c1 c2))");
}

fn main() {
use std::io::{self, Read};
let mut buffer = String::new();
Expand Down
48 changes: 11 additions & 37 deletions src/l3/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use inkwell::context::Context;
use inkwell::module::Module;
use inkwell::passes::PassManager;
use inkwell::types::{BasicTypeEnum, IntType};
use inkwell::values::{BasicValue, FunctionValue, IntValue, PointerValue};
use inkwell::values::{BasicValue, BasicValueEnum, FunctionValue, IntValue, PointerValue};
use inkwell::{IntPredicate, OptimizationLevel};

type CgResult<T> = Result<T, &'static str>;
Expand Down Expand Up @@ -143,7 +143,8 @@ impl<'a, 'ctx> Codegen<'a, 'ctx> {
impl<'a, 'ctx> State<'a, 'ctx> {
fn add_var(&mut self, name: &Name<'a>) -> PointerValue<'ctx> {
let ptr_value = self.builder.build_alloca(value_type(self.context), name.0);
self.vars.insert(*name, ptr_value).unwrap();
let old_opt = self.vars.insert(*name, ptr_value);
assert!(old_opt.is_none());
ptr_value
}

Expand Down Expand Up @@ -211,15 +212,21 @@ impl<'a, 'ctx> Codegen<'a, 'ctx> {
ret_cnt,
args,
} => {
// TODO: Handle args!
let fun = self.program.get_function(fun_name);
assert_eq!(fun.params.len(), args.len());
let fn_value = get_fn_value(self.module, fun_name.0);
let result = self.builder.build_call(fn_value, &vec![], "call_fun");
let args = args
.iter()
.map(|arg| self.arg_value(arg).into())
.collect::<Vec<BasicValueEnum>>();

let result = self.builder.build_call(fn_value, &args, "call_fun");
let result = result
.try_as_basic_value()
.left()
.expect("Failed to generate call")
.into_int_value();

if *ret_cnt == self.ret_cnt_name {
self.emit_cnt_call_indirect(result);
} else {
Expand Down Expand Up @@ -401,36 +408,3 @@ pub fn compile_and_run_program(program: &Program) -> () {
}
}
}

#[cfg(test)]
mod tests {
extern crate assert_cmd;
use assert_cmd::Command;

macro_rules! test_run {
($name:ident, $example:expr, $output:expr, $exit:expr) => {
#[test]
fn $name() {
let mut cmd = Command::cargo_bin("l3jit").unwrap();
let assert = cmd.write_stdin($example).assert().code($exit);
if let Some::<&'static str>(output) = $output {
assert.stdout(output);
}
}
};
}

test_run!(test_halt1, "(halt 0)", None, 0);
test_run!(
test_if_lt,
"(let* ((c1 (cnt () (halt 1))) (c2 (cnt () (halt 2)))) (if (@< 2 1) c1 c2))",
None,
2
);
test_run!(
test_if_le,
"(let* ((c1 (cnt () (halt 1))) (c2 (cnt () (halt 2)))) (if (@<= 2 2) c1 c2))",
None,
1
);
}
37 changes: 37 additions & 0 deletions src/tests/run_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
extern crate l3_llvm_runtime;

extern crate assert_cmd;
use assert_cmd::Command;

macro_rules! test_run {
($name:ident, $example:expr, $output:expr, $exit:expr) => {
#[test]
fn $name() {
let mut cmd = Command::cargo_bin("l3jit").unwrap();
let assert = cmd.write_stdin($example).assert().code($exit);
if let Some::<&'static str>(output) = $output {
assert.stdout(output);
}
}
};
}

test_run!(test_halt1, "(halt 0)", None, 0);
test_run!(
test_if_lt,
"(let* ((c1 (cnt () (halt 1))) (c2 (cnt () (halt 2)))) (if (@< 2 1) c1 c2))",
None,
2
);
test_run!(
test_if_le,
"(let* ((c1 (cnt () (halt 1))) (c2 (cnt () (halt 2)))) (if (@<= 2 2) c1 c2))",
None,
1
);
test_run!(
test_app_second,
"(let* ((f (fun (r x y) (r y))) (c (cnt (z) (halt z)))) (f c 1 2))",
None,
2
);

0 comments on commit ad26dc1

Please sign in to comment.