Skip to content

Commit

Permalink
Fix various bugs and get tests passing
Browse files Browse the repository at this point in the history
  • Loading branch information
Rigidity committed Apr 19, 2024
1 parent ed2cfcd commit 720bb23
Show file tree
Hide file tree
Showing 11 changed files with 366 additions and 178 deletions.
10 changes: 8 additions & 2 deletions crates/rue-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,27 @@ fn main() {
let output = compile(&mut allocator, ast, errors.is_empty());

if !output.diagnostics().is_empty() {
let mut has_error = false;

for error in output.diagnostics() {
let LineCol { line, col } = line_col(&source, error.span().start);
let line = line + 1;
let col = col + 1;

match error.kind() {
DiagnosticKind::Error(kind) => {
has_error = true;
eprintln!("Error: {} at {line}:{col}", kind)
}
DiagnosticKind::Warning(kind) => {
eprintln!("Warning: {} at {line}:{col}", kind)
}
}
}
return;

if has_error {
return;
}
}

let bytes = node_to_bytes(&allocator, output.node_ptr()).unwrap();
Expand All @@ -57,7 +63,7 @@ fn main() {
NodePtr::NIL,
0,
) {
Ok(output) => println!(
Ok(output) => eprintln!(
"Serialized output: {}",
hex::encode(node_to_bytes(&allocator, output.1).unwrap())
),
Expand Down
33 changes: 4 additions & 29 deletions crates/rue-compiler/src/database.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use std::collections::HashMap;

use id_arena::{Arena, Id};
use rue_parser::SyntaxToken;

use crate::{hir::Hir, lir::Lir, scope::Scope, symbol::Symbol, ty::Type};

Expand All @@ -27,33 +24,19 @@ pub struct Database {
types: Arena<Type>,
hir: Arena<Hir>,
lir: Arena<Lir>,
symbol_tokens: HashMap<SymbolId, SyntaxToken>,
type_tokens: HashMap<TypeId, SyntaxToken>,
}

impl Database {
pub(crate) fn alloc_scope(&mut self, scope: Scope) -> ScopeId {
ScopeId(self.scopes.alloc(scope))
}

pub(crate) fn alloc_symbol(&mut self, symbol: Symbol, token: Option<SyntaxToken>) -> SymbolId {
let id = SymbolId(self.symbols.alloc(symbol));

if let Some(token) = token {
self.symbol_tokens.insert(id, token);
}

id
pub(crate) fn alloc_symbol(&mut self, symbol: Symbol) -> SymbolId {
SymbolId(self.symbols.alloc(symbol))
}

pub(crate) fn alloc_type(&mut self, ty: Type, token: Option<SyntaxToken>) -> TypeId {
let id = TypeId(self.types.alloc(ty));

if let Some(token) = token {
self.type_tokens.insert(id, token);
}

id
pub(crate) fn alloc_type(&mut self, ty: Type) -> TypeId {
TypeId(self.types.alloc(ty))
}

pub(crate) fn alloc_hir(&mut self, hir: Hir) -> HirId {
Expand All @@ -72,10 +55,6 @@ impl Database {
&self.symbols[id.0]
}

pub fn symbol_token(&self, id: SymbolId) -> Option<SyntaxToken> {
self.symbol_tokens.get(&id).cloned()
}

pub fn ty_raw(&self, id: TypeId) -> &Type {
&self.types[id.0]
}
Expand All @@ -87,10 +66,6 @@ impl Database {
self.ty_raw(id)
}

pub fn type_token(&self, id: TypeId) -> Option<SyntaxToken> {
self.type_tokens.get(&id).cloned()
}

pub(crate) fn ty_mut(&mut self, id: TypeId) -> &mut Type {
&mut self.types[id.0]
}
Expand Down
17 changes: 17 additions & 0 deletions crates/rue-compiler/src/dependency_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,21 @@ impl Environment {
#[derive(Default)]
pub struct DependencyGraph {
env: IndexMap<ScopeId, Environment>,
symbol_usages: HashMap<SymbolId, usize>,
}

impl DependencyGraph {
pub fn env(&self, scope_id: ScopeId) -> &Environment {
&self.env[&scope_id]
}

pub fn visited_scopes(&self) -> Vec<ScopeId> {
self.env.keys().copied().collect()
}

pub fn symbol_usages(&self, symbol_id: SymbolId) -> usize {
*self.symbol_usages.get(&symbol_id).unwrap_or(&0)
}
}

pub struct GraphTraversal<'a> {
Expand Down Expand Up @@ -175,9 +184,16 @@ impl<'a> GraphTraversal<'a> {
self.visit_hir(new_scope_id, hir_id, visited);
}
Hir::Reference(symbol_id) => {
self.graph
.symbol_usages
.entry(symbol_id)
.and_modify(|count| *count += 1)
.or_insert(1);

self.propagate_capture(scope_id, symbol_id, &mut HashSet::new());

match self.db.symbol(symbol_id).clone() {
Symbol::Unknown => unreachable!(),
Symbol::Function {
scope_id: new_scope_id,
hir_id,
Expand Down Expand Up @@ -275,6 +291,7 @@ impl<'a> GraphTraversal<'a> {
self.compute_hir_edges(new_scope_id, hir_id, visited);
}
Hir::Reference(symbol_id) => match self.db.symbol(symbol_id).clone() {
Symbol::Unknown => unreachable!(),
Symbol::Function {
scope_id: new_scope_id,
hir_id,
Expand Down
3 changes: 3 additions & 0 deletions crates/rue-compiler/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ pub enum ErrorKind {
#[error("expected {expected} arguments, but found {found}")]
ArgumentMismatch { expected: usize, found: usize },

#[error("expected at least {expected} arguments, but found {found}")]
TooFewArgumentsWithVarargs { expected: usize, found: usize },

#[error("uninitializable type `{0}`")]
UninitializableType(String),

Expand Down
7 changes: 4 additions & 3 deletions crates/rue-compiler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ mod lowerer;
mod optimizer;
mod scope;
mod symbol;
mod symbol_table;
mod ty;

pub use database::*;
Expand Down Expand Up @@ -49,7 +50,7 @@ fn precompile(database: &mut Database, root: Root) -> (Vec<Diagnostic>, Option<L

let mut lowerer = Lowerer::new(database);
lowerer.compile_root(root, scope_id);
let mut diagnostics = lowerer.finish();
let (sym, mut diagnostics) = lowerer.finish();

let Some(main_id) = database.scope_mut(scope_id).symbol("main") else {
diagnostics.push(Diagnostic::new(
Expand All @@ -62,8 +63,8 @@ fn precompile(database: &mut Database, root: Root) -> (Vec<Diagnostic>, Option<L

let traversal = GraphTraversal::new(database);
let graph = traversal.build_graph(main_id);
let mut optimizer = Optimizer::new(database, graph);
let lir_id = optimizer.opt_main(main_id);
let mut optimizer = Optimizer::new(database, &sym, graph);
let lir_id = optimizer.opt_main(scope_id, main_id);
diagnostics.extend(optimizer.finish());

(diagnostics, Some(lir_id))
Expand Down
Loading

0 comments on commit 720bb23

Please sign in to comment.