From 1542f0349bda46b407fc66afa420296e6258f57c Mon Sep 17 00:00:00 2001 From: imxyy_soope_ Date: Sun, 11 May 2025 20:37:12 +0800 Subject: [PATCH] feat: parse error --- src/bin/repl.rs | 12 +++++++++--- src/error.rs | 2 ++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/bin/repl.rs b/src/bin/repl.rs index 3a11149..b4229e5 100644 --- a/src/bin/repl.rs +++ b/src/bin/repl.rs @@ -1,3 +1,4 @@ +use itertools::Itertools; use inkwell::context::Context; use rustyline::error::ReadlineError; use rustyline::{DefaultEditor, Result}; @@ -5,6 +6,7 @@ use rustyline::{DefaultEditor, Result}; use nixjit::compile::compile; use nixjit::ir::downgrade; use nixjit::vm::{run, JITContext}; +use nixjit::error::Error; macro_rules! unwrap { ($e:expr) => { @@ -28,9 +30,13 @@ fn main() -> Result<()> { continue; } rl.add_history_entry(expr.as_str())?; - let downgraded = unwrap!(downgrade( - rnix::Root::parse(expr.as_str()).tree().expr().unwrap() - )); + let root = rnix::Root::parse(&expr); + if !root.errors().is_empty() { + println!("{}", Error::ParseError(root.errors().iter().map(|err| err.to_string()).join(";"))); + continue; + } + let expr = root.tree().expr().unwrap(); + let downgraded = unwrap!(downgrade(expr)); let prog = compile(downgraded); let ctx = Context::create(); let jit = JITContext::new(&ctx); diff --git a/src/error.rs b/src/error.rs index cfb760a..7e7b898 100644 --- a/src/error.rs +++ b/src/error.rs @@ -4,6 +4,8 @@ pub type Result = core::result::Result; #[derive(Error, Debug)] pub enum Error { + #[error("error occurred during parse stage: {0}")] + ParseError(String), #[error("error occurred during downgrade stage: {0}")] DowngradeError(String), #[error("error occurred during evaluation stage: {0}")]