diff --git a/src/error.rs b/src/error.rs index 20057df..a0d2f88 100644 --- a/src/error.rs +++ b/src/error.rs @@ -10,6 +10,7 @@ pub enum Error { Parse { expected: String, span: SimpleSpan }, Lex { expected: String, span: SimpleSpan }, Runtime(RuntimeError), + Validation(ValidationError), } impl From> for Error { @@ -92,7 +93,7 @@ pub fn create_report<'a>(errors: &'a [Error]) -> Report<'a> { let mut report = Report::build(ReportKind::Error, (), 0); for error in errors { - match error { + match &error { Error::Parse { expected, span } => { report = report.with_label( Label::new(span.start..span.end).with_message(format!("Expected {expected}.")), @@ -109,6 +110,7 @@ pub fn create_report<'a>(errors: &'a [Error]) -> Report<'a> { ); } Error::Runtime(_) => todo!(), + Error::Validation(_) => todo!(), } } diff --git a/src/lib.rs b/src/lib.rs index ab09e0e..9282dc2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -24,6 +24,20 @@ impl Interpreter { pub fn run(&mut self, source: &str) -> Result> { let tokens = lex(source)?; let statements = parse(&tokens)?; + let errors = statements + .iter() + .filter_map(|(statement, _span)| { + if let Some(error) = statement.validate(&self.context).err() { + Some(Error::Validation(error)) + } else { + None + } + }) + .collect::>(); + + if !errors.is_empty() { + return Err(errors); + } let mut value = Value::none();