Skip to content

Commit

Permalink
feat(lura-hir): migrate the local of throwing errors in level to idx …
Browse files Browse the repository at this point in the history
…function
  • Loading branch information
aripiprazole committed Nov 13, 2023
1 parent d6502ac commit c0758c3
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 8 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion example/src/Main.lura
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ using Std

Main (args: List Std.String) {
println $ head args
}
}
1 change: 1 addition & 0 deletions lura-hir/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ dashmap.workspace = true
if_chain = "1.0.2"
petgraph = "0.6.3"
once_cell = "1.18.0"
thiserror.workspace = true
25 changes: 19 additions & 6 deletions lura-hir/src/debruijin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,27 @@ use crate::source::expr::Expr;
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Level(pub usize);

#[derive(thiserror::Error, Debug, Clone)]
pub enum Level2IdxError {
/// The level is greater than the index.
#[error("expected l > x")]
LevelGreaterThanIndex,
/// The level is equal to zero.
#[error("expected l > 0")]
LevelEqualToZero,
}

impl Level {
/// Transforms a level into a debruijin index.
pub fn as_idx(&self, Level(_): Level) -> Index {
let Level(_) = *self;
// assert!(l > x, "expected l > x, but {l} < {x}");
// assert!(l > 0, "l should be greater than 0");
// TODO
Index(0)
pub fn as_idx(&self, Level(l): Level) -> Result<Index, Level2IdxError> {
let Level(x) = *self;
if l > x {
return Err(Level2IdxError::LevelGreaterThanIndex);
}
if l == 0 {
return Err(Level2IdxError::LevelEqualToZero);
}
Ok(Index(0))
}
}

Expand Down
5 changes: 4 additions & 1 deletion lura-hir/src/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,10 @@ impl Scope {
/// Adds a reference to the given `definition` in the current scope.
pub fn using(&mut self, db: &dyn crate::HirDb, it: Definition, loc: Location) -> Reference {
// Create a new reference to [it] and insert it in the current scope
let idx = self.lvl.as_idx(it.defined_at(db));
// let idx = self.lvl.as_idx(it.defined_at(db)).unwrap_or_else(|error| {
// panic!("failed to create a reference to {:?}: {}", it.to_string(db), error)
// });
let idx = it.defined_at(db).as_idx(self.lvl).unwrap_or_default();
let reference = Reference::new(db, it, loc, idx);
self.references.entry(it).or_default().insert(reference);

Expand Down

0 comments on commit c0758c3

Please sign in to comment.