From 5acd1624fd2fd309eed0a6364efc937844b68746 Mon Sep 17 00:00:00 2001 From: sofia Date: Mon, 14 Jul 2025 22:52:30 +0300 Subject: [PATCH] Move test to inside-executable, and add tests that test it compiles --- reid/{src => }/lib/std.reid | 0 reid/src/lib.rs | 67 +++++++++++++++++++++++-------------- reid/src/mir/linker.rs | 10 ++---- reid/tests/stdlib.rs | 29 ++++++++++++++++ 4 files changed, 73 insertions(+), 33 deletions(-) rename reid/{src => }/lib/std.reid (100%) create mode 100644 reid/tests/stdlib.rs diff --git a/reid/src/lib/std.reid b/reid/lib/std.reid similarity index 100% rename from reid/src/lib/std.reid rename to reid/lib/std.reid diff --git a/reid/src/lib.rs b/reid/src/lib.rs index cb344b9..a10ff79 100644 --- a/reid/src/lib.rs +++ b/reid/src/lib.rs @@ -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 { 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); diff --git a/reid/src/mir/linker.rs b/reid/src/mir/linker.rs index 7a1355b..d605aa3 100644 --- a/reid/src/mir/linker.rs +++ b/reid/src/mir/linker.rs @@ -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>> = modules.values().cloned().collect(); diff --git a/reid/tests/stdlib.rs b/reid/tests/stdlib.rs new file mode 100644 index 0000000..9a5f457 --- /dev/null +++ b/reid/tests/stdlib.rs @@ -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(value: Result) { + match value { + Ok(_) => {} + Err(err) => assert!(false, "{:?}", err), + } +}