Move test to inside-executable, and add tests that test it compiles

This commit is contained in:
Sofia 2025-07-14 22:52:30 +03:00
parent 3f90b46dc8
commit 5acd1624fd
4 changed files with 73 additions and 33 deletions

View File

@ -100,6 +100,46 @@ pub fn compile_module(
Ok(ast_module.process())
}
pub fn perform_all_passes(context: &mut mir::Context) -> Result<(), ReidError> {
let state = context.pass(&mut LinkerPass);
#[cfg(debug_assertions)]
{
dbg!(&context);
println!("{}", &context);
}
if !state.errors.is_empty() {
return Err(ReidError::LinkerErrors(state.errors));
}
let refs = TypeRefs::default();
let state = context.pass(&mut TypeInference { refs: &refs });
#[cfg(debug_assertions)]
{
dbg!(&state, &refs);
dbg!(&context);
println!("{}", &context);
}
// if !state.errors.is_empty() {
// return Err(ReidError::TypeInferenceErrors(state.errors));
// }
let state = context.pass(&mut TypeCheck { refs: &refs });
#[cfg(debug_assertions)]
{
dbg!(&state);
println!("{}", &context);
}
if !state.errors.is_empty() {
return Err(ReidError::TypeCheckErrors(state.errors));
}
Ok(())
}
/// Takes in a bit of source code, parses and compiles it and produces `hello.o`
/// and `hello.asm` from it, which can be linked using `ld` to produce an
/// executable file.
@ -118,32 +158,7 @@ pub fn compile(source: &str, path: PathBuf) -> Result<String, ReidError> {
println!("{}", &mir_context);
let state = mir_context.pass(&mut LinkerPass);
dbg!(&state);
println!("{}", &mir_context);
if !state.errors.is_empty() {
return Err(ReidError::LinkerErrors(state.errors));
}
let refs = TypeRefs::default();
let state = mir_context.pass(&mut TypeInference { refs: &refs });
dbg!(&state, &refs);
dbg!(&mir_context);
println!("{}", &mir_context);
// if !state.errors.is_empty() {
// return Err(ReidError::TypeInferenceErrors(state.errors));
// }
let state = mir_context.pass(&mut TypeCheck { refs: &refs });
dbg!(&state);
println!("{}", &mir_context);
if !state.errors.is_empty() {
return Err(ReidError::TypeCheckErrors(state.errors));
}
perform_all_passes(&mut mir_context);
let mut context = Context::new();
let codegen_modules = mir_context.codegen(&mut context);

View File

@ -15,7 +15,7 @@ use super::{
Context, FunctionDefinition, Import, Metadata, Module,
};
pub static STD_SOURCE: &str = include_str!("../lib/std.reid");
pub static STD_SOURCE: &str = include_str!("../../lib/std.reid");
#[derive(thiserror::Error, Debug, Clone)]
pub enum ErrorKind {
@ -41,15 +41,11 @@ pub enum ErrorKind {
FunctionIsPrivate(String, String),
}
fn compile_main() -> super::Module {
pub fn compile_std() -> super::Module {
let module = compile_module(STD_SOURCE, "standard_library".to_owned(), None, false).unwrap();
let mut mir_context = super::Context::from(vec![module], Default::default());
let mut refs = super::typerefs::TypeRefs::default();
mir_context.pass(&mut super::typeinference::TypeInference { refs: &mut refs });
mir_context.pass(&mut super::typecheck::TypeCheck { refs: &mut refs });
let std_compiled = mir_context.modules.remove(0);
std_compiled
}
@ -86,7 +82,7 @@ impl Pass for LinkerPass {
modules.insert(module.name.clone(), Rc::new(RefCell::new(module)));
}
modules.insert("std".to_owned(), Rc::new(RefCell::new(compile_main())));
modules.insert("std".to_owned(), Rc::new(RefCell::new(compile_std())));
let mut modules_to_process: Vec<Rc<RefCell<Module>>> = modules.values().cloned().collect();

29
reid/tests/stdlib.rs Normal file
View File

@ -0,0 +1,29 @@
use reid::{
mir::{self, linker::compile_std},
perform_all_passes,
};
#[test]
fn compiles() {
let _ = compile_std();
}
#[test]
fn passes_all_passes() {
let mut std = compile_std();
// Needed to pass linker-pass
std.is_main = true;
assert_err(perform_all_passes(&mut mir::Context {
modules: vec![std],
base: Default::default(),
}));
}
fn assert_err<T, U: std::fmt::Debug>(value: Result<T, U>) {
match value {
Ok(_) => {}
Err(err) => assert!(false, "{:?}", err),
}
}