Skip to content

Commit

Permalink
Tighten up loc code and place it into sourceq.rs.
Browse files Browse the repository at this point in the history
  • Loading branch information
maloneymr committed Jul 22, 2024
1 parent 50a6cb6 commit 50f93a1
Show file tree
Hide file tree
Showing 16 changed files with 209 additions and 264 deletions.
14 changes: 3 additions & 11 deletions examples/foo.vir
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
pub mod Top {
incoming clock : Clock;
outgoing led_0 : Word[1];
outgoing led_1 : Word[1];
outgoing led_2 : Word[1];
outgoing led_3 : Word[1];
outgoing led_4 : Word[1];
incoming inp : Word[3];
outgoing out : Word[1];

led_0 := 1;
led_1 := 1;
led_2 := 1;
led_3 := 1;
led_4 := 1;
out := inp->get(0);
}
2 changes: 1 addition & 1 deletion examples/top.vir
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub mod Top {
reg i : Word[2] on clock;
i <= i->inc();

led_0 := counter[22..18]->get(i);
led_0 := counter[23..18]->get(i);
led_1 := counter[22..18]->get(i->inc());
led_2 := counter[22..18]->get(i->add(2));
led_3 := counter[22..18]->get(i->add(3));
Expand Down
15 changes: 9 additions & 6 deletions src/ast.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use std::marker::PhantomData;
use crate::common::*;
use crate::loc::{Span, SourceInfo};
use crate::phase::sourceq::SpanIdx;
use crate::phase::id::PackageId;

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Ast<T>(Arc<T>, Span, Id<T>);
pub struct Ast<T>(Arc<T>, SpanIdx, Id<T>);

impl<T> std::ops::Deref for Ast<T> {
type Target = T;
Expand All @@ -20,20 +20,23 @@ impl<T> AsRef<T> for Ast<T> {
}
}

impl<T> Ast<T> {
pub fn span(&self) -> SpanIdx {
self.1.clone()
}
}

pub struct AstGen {
package_id: PackageId,
source_info: SourceInfo,
next_id: usize,
}

impl AstGen {
pub fn new(package: &str) -> Self {
let package_id = PackageId::from_ident(package.into());
let source_info: SourceInfo = SourceInfo::unknown();
let next_id = 0;
AstGen {
package_id,
source_info,
next_id,
}
}
Expand All @@ -46,7 +49,7 @@ impl AstGen {

pub fn ast<T>(&mut self, t: T, span_start_idx: usize, span_end_idx: usize) -> Ast<T> {
let id = self.id();
let span: Span = Span::from(&self.source_info, span_start_idx, span_end_idx);
let span: SpanIdx = SpanIdx::new(self.package_id.clone(), span_start_idx, span_end_idx);
Ast(Arc::new(t), span, id)
}
}
Expand Down
33 changes: 29 additions & 4 deletions src/common.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub use internment::Intern;
pub use std::sync::Arc;
use crate::phase::sourceq::Span;

pub type Val = u64;
pub type Width = u64;
Expand Down Expand Up @@ -72,30 +73,54 @@ pub enum VirdantError {
TypeError(TypeError),
ParseError(String),
Io(String),
Other(String),
Other(Option<Span>, String),
At(Box<VirdantError>, Span),
Because(Box<VirdantError>, Box<VirdantError>),
Unknown,
}

#[macro_export]
macro_rules! virdant_error {
() => {
VirdantError::Other(format!("Unknown Error: in file {} on line {}", file!().to_string(), line!().to_string()))
VirdantError::Other(None, format!("Unknown Error: in file {} on line {}", file!().to_string(), line!().to_string()))
};

($fmt:literal) => {
{
let prelude = format!("Unknown Error: in file {} on line {}: ", file!().to_string(), line!().to_string());
let msg = format!($fmt);
VirdantError::Other(format!("{prelude}: {msg}"))
VirdantError::Other(None, format!("{prelude}: {msg}"))
}
};

($fmt:literal, $($arg:expr),*) => {
{
let prelude = format!("Unknown Error: in file {} on line {}: ", file!().to_string(), line!().to_string());
let msg = format!($fmt, $($arg)*);
VirdantError::Other(format!("{prelude}: {msg}"))
VirdantError::Other(None, format!("{prelude}: {msg}"))
}
};
}

#[macro_export]
macro_rules! virdant_error_at {
($span:expr) => {
VirdantError::Other(Some($span), format!("Unknown Error: in file {} on line {}", file!().to_string(), line!().to_string()))
};

($fmt:literal, $span:expr) => {
{
let prelude = format!("Unknown Error: in file {} on line {}: ", file!().to_string(), line!().to_string());
let msg = format!($fmt);
VirdantError::Other(Some($span), format!("{prelude}: {msg}"))
}
};

($fmt:literal, $($arg:expr),*, $span:expr) => {
{
let prelude = format!("Unknown Error: in file {} on line {}: ", file!().to_string(), line!().to_string());
let msg = format!($fmt, $($arg)*);
VirdantError::Other(Some($span), format!("{prelude}: {msg}"))
}
};
}
Expand Down
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
mod tests;

pub mod common;
pub mod loc;
pub mod ast;
pub mod parse;
pub mod context;
Expand Down
202 changes: 0 additions & 202 deletions src/loc.rs

This file was deleted.

6 changes: 3 additions & 3 deletions src/phase/astq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ fn moddef_ast(db: &dyn AstQ, moddef_id: ModDefId) -> VirdantResult<Ast<ast::ModD
if result.is_none() {
result = Some(moddef_ast.clone());
} else {
return Err(VirdantError::Other("Uh oh".into()));
return Err(virdant_error!("Uh oh"));
}
}
},
Expand All @@ -78,7 +78,7 @@ fn uniondef_ast(db: &dyn AstQ, uniontype_id: UnionDefId) -> VirdantResult<Ast<as
if result.is_none() {
result = Some(uniondef_ast.clone());
} else {
return Err(VirdantError::Other("Uh oh".into()));
return Err(virdant_error!("Uh oh"));
}
}
},
Expand All @@ -100,7 +100,7 @@ fn structdef_ast(db: &dyn AstQ, structdef_id: StructDefId) -> VirdantResult<Ast<
if result.is_none() {
result = Some(structdef_ast.clone());
} else {
return Err(VirdantError::Other("Uh oh".into()));
return Err(virdant_error!("Uh oh"));
}
}
},
Expand Down
Loading

0 comments on commit 50f93a1

Please sign in to comment.