Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flatten: keep references to the AST throughout #614

Merged
merged 14 commits into from
Mar 25, 2023
Merged
4 changes: 2 additions & 2 deletions ast-sanitizer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ struct NoInclCtx;
impl Visitor for NoInclCtx {
fn visit_incl(&mut self, loc: SpanTuple, _: Symbol, _: Option<Symbol>) -> Result<Ast, Error> {
Err(Error::new(ErrKind::Sanitizer)
.with_loc(Some(loc))
.with_loc(loc)
.with_msg("include expression detected when none should exist".to_string()))
}
}
Expand All @@ -24,7 +24,7 @@ impl Visitor for OnlyWhileCtx {
) -> Result<Ast, Error> {
match kind {
LoopKind::Infinite | LoopKind::For { .. } => Err(Error::new(ErrKind::Sanitizer)
.with_loc(Some(location))
.with_loc(location)
.with_msg("`for` loop or `loop` loop did not get desugared correctly".to_string())),
_ => Ok(Ast {
location,
Expand Down
11 changes: 10 additions & 1 deletion error/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ impl Error {
Error::new(ErrKind::Hint)
}

// TODO: This should take an Into<String> so we can pass strings, format!(), &str...
pub fn with_msg(self, msg: String) -> Error {
Error {
msg: Some(msg),
Expand All @@ -262,10 +263,18 @@ impl Error {
}

// FIXME: Should this really take an Option<Location>?
pub fn with_loc(self, loc: Option<SpanTuple>) -> Error {
#[deprecated]
pub fn with_opt_loc(self, loc: Option<SpanTuple>) -> Error {
Error { loc, ..self }
}

pub fn with_loc(self, loc: SpanTuple) -> Error {
Error {
loc: Some(loc),
..self
}
}

// Add a hint to emit alongside the error
pub fn with_hint(self, hint: Error) -> Error {
let mut new_hints = self.hints;
Expand Down
25 changes: 14 additions & 11 deletions fir/src/iter/mapper.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{Fir, Incomplete, Kind, Node, OriginIdx, RefIdx};

pub trait Mapper<T, U: Default + From<T>, E> {
pub trait Mapper<T, U: From<T>, E> {
fn map_constant(&mut self, data: T, origin: OriginIdx, constant: RefIdx) -> Result<Node<U>, E> {
Ok(Node {
data: U::from(data),
Expand Down Expand Up @@ -260,16 +260,19 @@ pub trait Mapper<T, U: Default + From<T>, E> {
/// all valid mapped nodes. This allows an interpreter to keep trying
/// passes and emit as many errors as possible
fn map(&mut self, fir: Fir<T>) -> Result<Fir<U>, Incomplete<U, E>> {
let (fir, errs) = fir.nodes.into_values().fold(
(Fir::default(), Vec::new()),
|(new_fir, mut errs), node| match self.map_node(node) {
Ok(node) => (new_fir.append(node), errs),
Err(e) => {
errs.push(e);
(new_fir, errs)
}
},
);
let (fir, errs) =
fir.nodes
.into_values()
.fold(
(Fir::new(), Vec::new()),
|(new_fir, mut errs), node| match self.map_node(node) {
Ok(node) => (new_fir.append(node), errs),
Err(e) => {
errs.push(e);
(new_fir, errs)
}
},
);

if errs.is_empty() {
Ok(fir)
Expand Down
7 changes: 7 additions & 0 deletions fir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,13 @@ pub struct Fir<T = ()> {
}

impl<T> Fir<T> {
/// Create a new and empty [`Fir`]
pub fn new() -> Fir<T> {
Fir {
nodes: BTreeMap::new(),
}
}

/// Append a new node to the [`Fir`]
pub fn append(self, node: Node<T>) -> Fir<T> {
Fir {
Expand Down
Loading