Skip to content

Commit

Permalink
enrich Append eval error with source span;
Browse files Browse the repository at this point in the history
  • Loading branch information
greenhat committed Jul 21, 2023
1 parent 2c47b8b commit 801660d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
19 changes: 19 additions & 0 deletions ergotree-interpreter/src/eval.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Interpreter
use bounded_vec::BoundedVecOutOfBounds;
use ergotree_ir::mir::constant::TryExtractInto;
use ergotree_ir::mir::expr::SourceSpan;
use ergotree_ir::sigma_protocol::sigma_boolean::SigmaProp;
use sigma_ser::ScorexParsingError;
use sigma_ser::ScorexSerializationError;
Expand Down Expand Up @@ -163,6 +164,24 @@ pub enum EvalError {
/// environment after evaluation
env: Env,
},
/// Wrapped eval error with source span
#[error("eval error: {error}, source span: {source_span:?}")]
WrappedWithSpan {
/// eval error
error: Box<EvalError>,
/// source span
source_span: SourceSpan,
},
}

impl EvalError {
/// Wrap eval error with source span
pub fn wrap_with_span(self, source_span: SourceSpan) -> Self {
EvalError::WrappedWithSpan {
error: Box::new(self),
source_span,
}
}
}

/// Result of expression reduction procedure (see `reduce_to_crypto`).
Expand Down
13 changes: 12 additions & 1 deletion ergotree-interpreter/src/eval/expr.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use ergotree_ir::mir::expr::Expr;
use ergotree_ir::mir::expr::SourceSpan;
use ergotree_ir::mir::value::Value;

use super::Env;
Expand Down Expand Up @@ -50,7 +51,7 @@ impl Evaluable for Expr {
Expr::Upcast(op) => op.eval(env, ctx),
Expr::Downcast(op) => op.eval(env, ctx),
Expr::If(op) => op.eval(env, ctx),
Expr::Append(op) => op.expr().eval(env, ctx),
Expr::Append(op) => op.expr().eval(env, ctx).with_span(&op.source_span),
Expr::ByIndex(op) => op.eval(env, ctx),
Expr::ExtractScriptBytes(op) => op.eval(env, ctx),
Expr::SizeOf(op) => op.eval(env, ctx),
Expand Down Expand Up @@ -83,3 +84,13 @@ impl Evaluable for Expr {
}
}
}

pub trait ExtResultEvalError<T> {
fn with_span(self, span: &SourceSpan) -> Result<T, EvalError>;
}

impl<T> ExtResultEvalError<T> for Result<T, EvalError> {
fn with_span(self, span: &SourceSpan) -> Result<T, EvalError> {
self.map_err(|e| e.wrap_with_span(span.clone()))
}
}

0 comments on commit 801660d

Please sign in to comment.