Skip to content

Commit

Permalink
Apply main and optimizer diagnostics
Browse files Browse the repository at this point in the history
  • Loading branch information
Rigidity committed Apr 17, 2024
1 parent 77bf4a7 commit e4987f6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
24 changes: 20 additions & 4 deletions crates/rue-compiler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,22 @@ pub fn analyze(root: Root) -> Vec<Diagnostic> {
let scope_id = db.alloc_scope(Scope::default());
let mut lowerer = Lowerer::new(&mut db);
lowerer.compile_root(root, scope_id);
lowerer.finish()
let mut diagnostics = lowerer.finish();

let Some(main_id) = db.scope_mut(scope_id).symbol("main") else {
diagnostics.push(Diagnostic::new(
DiagnosticKind::Error(ErrorKind::MissingMain),
0..0,
));

return diagnostics;
};

let mut optimizer = Optimizer::new(&mut db);
optimizer.opt_main(main_id);
diagnostics.extend(optimizer.finish());

diagnostics
}

pub fn compile(allocator: &mut Allocator, root: Root, parsing_succeeded: bool) -> Output {
Expand All @@ -63,10 +78,11 @@ pub fn compile(allocator: &mut Allocator, root: Root, parsing_succeeded: bool) -
};
};

let node_ptr = if !diagnostics.iter().any(Diagnostic::is_error) && parsing_succeeded {
let mut optimizer = Optimizer::new(&mut db);
let lir_id = optimizer.opt_main(main_id);
let mut optimizer = Optimizer::new(&mut db);
let lir_id = optimizer.opt_main(main_id);
diagnostics.extend(optimizer.finish());

let node_ptr = if !diagnostics.iter().any(Diagnostic::is_error) && parsing_succeeded {
let mut codegen = Codegen::new(&mut db, allocator);
codegen.gen_lir(lir_id)
} else {
Expand Down
7 changes: 7 additions & 0 deletions crates/rue-compiler/src/optimizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::{
hir::{BinOp, Hir},
lir::Lir,
symbol::Symbol,
Diagnostic,
};

#[derive(Default)]
Expand All @@ -31,16 +32,22 @@ impl Environment {
pub struct Optimizer<'a> {
db: &'a mut Database,
environments: HashMap<ScopeId, Environment>,
diagnostics: Vec<Diagnostic>,
}

impl<'a> Optimizer<'a> {
pub fn new(db: &'a mut Database) -> Self {
Self {
db,
environments: HashMap::new(),
diagnostics: Vec::new(),
}
}

pub fn finish(self) -> Vec<Diagnostic> {
self.diagnostics
}

fn env(&self, scope_id: ScopeId) -> &Environment {
self.environments.get(&scope_id).unwrap()
}
Expand Down

0 comments on commit e4987f6

Please sign in to comment.