diff --git a/lox-examples/expr.lox b/lox-examples/expr.lox new file mode 100644 index 0000000..8858afb --- /dev/null +++ b/lox-examples/expr.lox @@ -0,0 +1 @@ +(1 + -2) * 4 < 0 == true != !false diff --git a/src/runners/file.rs b/src/runners/file.rs index a5fea82..dd34cee 100644 --- a/src/runners/file.rs +++ b/src/runners/file.rs @@ -1,8 +1,9 @@ use std::path::Path; -use crate::scanner::scan; use miette::{IntoDiagnostic, Report, Result, WrapErr}; +use crate::{parser::parse, scanner::scan}; + #[tracing::instrument(skip_all, fields(path = %path.display()))] pub fn run_file(path: &Path) -> Result<()> { tracing::debug!("reading lox file"); @@ -11,8 +12,11 @@ pub fn run_file(path: &Path) -> Result<()> { .wrap_err_with(|| format!("failed to open file: {}", path.display()))?; tracing::debug!("scanning"); - let tokens = scan(&contents).map_err(|e| Report::new(e).with_source_code(contents))?; + let tokens = scan(&contents).map_err(|e| Report::new(e).with_source_code(contents.clone()))?; dbg!(&tokens.len()); + let ast = parse(tokens).map_err(|e| Report::new(e).with_source_code(contents))?; + dbg!(&ast); + Ok(()) } diff --git a/src/span.rs b/src/span.rs index d888de0..ffae3c6 100644 --- a/src/span.rs +++ b/src/span.rs @@ -1,4 +1,4 @@ -use std::fmt::{Display, Formatter}; +use std::fmt::{Debug, Display, Formatter}; use std::ops::{Deref, DerefMut, Range}; use miette::SourceSpan; @@ -29,7 +29,7 @@ impl Span { } } -#[derive(Clone, Debug)] +#[derive(Clone)] pub struct Spanned(T, Span); impl Spanned { @@ -55,6 +55,18 @@ impl DerefMut for Spanned { } } +impl Debug for Spanned +where + T: Debug, +{ + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + f.debug_tuple("") + .field(&self.1.as_range()) + .field(&self.0) + .finish() + } +} + impl From for SourceSpan { fn from(span: Span) -> Self { span.as_range().into()