Compare commits

...

2 Commits

4 changed files with 83 additions and 26 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,6 +15,8 @@ use super::{
Context, FunctionDefinition, Import, Metadata, Module,
};
pub static STD_SOURCE: &str = include_str!("../../lib/std.reid");
#[derive(thiserror::Error, Debug, Clone)]
pub enum ErrorKind {
#[error("Unable to import inner modules, not yet supported: {0}")]
@ -39,6 +41,15 @@ pub enum ErrorKind {
FunctionIsPrivate(String, String),
}
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 std_compiled = mir_context.modules.remove(0);
std_compiled
}
/// Struct used to implement a type-checking pass that can be performed on the
/// MIR.
pub struct LinkerPass;
@ -71,6 +82,8 @@ impl Pass for LinkerPass {
modules.insert(module.name.clone(), Rc::new(RefCell::new(module)));
}
modules.insert("std".to_owned(), Rc::new(RefCell::new(compile_std())));
let mut modules_to_process: Vec<Rc<RefCell<Module>>> = modules.values().cloned().collect();
while let Some(module) = modules_to_process.pop() {

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),
}
}