stuff
This commit is contained in:
+60
-51
@@ -1,60 +1,69 @@
|
||||
use crate::{
|
||||
io::{CompilerOutput, Span},
|
||||
io::{CompilerMsg, CompilerOutput, Span},
|
||||
parser::{Cursor, nodes::*},
|
||||
};
|
||||
|
||||
mod id;
|
||||
mod parse;
|
||||
pub use id::*;
|
||||
pub use parse::*;
|
||||
mod ctx;
|
||||
mod dsp;
|
||||
pub use ctx::*;
|
||||
pub use dsp::*;
|
||||
|
||||
impl Nodes {
|
||||
pub fn parse_root(path: &str, output: &mut CompilerOutput) -> Option<(Self, Id<Body>)> {
|
||||
let root_code = match std::fs::read_to_string(path) {
|
||||
Ok(code) => code,
|
||||
Err(err) => {
|
||||
output.error(format!("Failed to read input file: {err}"));
|
||||
return None;
|
||||
}
|
||||
};
|
||||
output.files.push(path.to_string());
|
||||
let mut ctx = ParseCtx::new(Cursor::new(&root_code, 0));
|
||||
let root = match ctx.parse() {
|
||||
Ok(expr) => expr,
|
||||
Err(msg) => {
|
||||
output.error(msg);
|
||||
return None;
|
||||
}
|
||||
};
|
||||
Some((ctx.finish(), root))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct NodeVec<N> {
|
||||
vec: Vec<N>,
|
||||
spans: Vec<Span>,
|
||||
}
|
||||
|
||||
impl<N> NodeVec<N> {
|
||||
pub fn add(&mut self, v: N, span: Span) -> Id<N> {
|
||||
let id = self.vec.len();
|
||||
self.vec.push(v);
|
||||
self.spans.push(span);
|
||||
Id::new(id)
|
||||
}
|
||||
}
|
||||
|
||||
impl<N> Default for NodeVec<N> {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
vec: Default::default(),
|
||||
spans: Default::default(),
|
||||
}
|
||||
}
|
||||
pub struct Parsed<N> {
|
||||
pub node: N,
|
||||
pub span: Span,
|
||||
}
|
||||
|
||||
pub trait Node: Sized {
|
||||
fn vec(nodes: &Nodes) -> &NodeVec<Self>;
|
||||
fn vec_mut(nodes: &mut Nodes) -> &mut NodeVec<Self>;
|
||||
fn parse(ctx: &mut ParseCtx) -> Result<Self, CompilerMsg>;
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter, ctx: DisplayCtx) -> std::fmt::Result;
|
||||
}
|
||||
|
||||
pub fn parse_root(path: &str, output: &mut CompilerOutput) -> Option<Parsed<Body>> {
|
||||
let root_code = match std::fs::read_to_string(path) {
|
||||
Ok(code) => code,
|
||||
Err(err) => {
|
||||
output.error(format!("Failed to read input file: {err}"));
|
||||
return None;
|
||||
}
|
||||
};
|
||||
output.files.push(path.to_string());
|
||||
let mut ctx = ParseCtx::new(Cursor::new(&root_code, 0));
|
||||
let root = match ctx.parse() {
|
||||
Ok(v) => v,
|
||||
Err(msg) => {
|
||||
output.error(msg);
|
||||
return None;
|
||||
}
|
||||
};
|
||||
Some(root)
|
||||
}
|
||||
|
||||
impl<N> Parsed<N> {
|
||||
pub fn new(node: N, span: Span) -> Self {
|
||||
Self { node, span }
|
||||
}
|
||||
|
||||
pub fn boxed(self) -> Box<Self> {
|
||||
Box::new(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl<N> std::ops::Deref for Parsed<N> {
|
||||
type Target = N;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.node
|
||||
}
|
||||
}
|
||||
|
||||
impl<N> std::ops::DerefMut for Parsed<N> {
|
||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||
&mut self.node
|
||||
}
|
||||
}
|
||||
|
||||
impl<N: Node> std::fmt::Display for Parsed<N> {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
self.node.fmt(f, DisplayCtx { indent: 0 })
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user