Skip to content

Commit

Permalink
impl pretty printing for ByIndex and add out of bounds error test;
Browse files Browse the repository at this point in the history
  • Loading branch information
greenhat committed Aug 14, 2023
1 parent f851f47 commit 60e25a6
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ proptest = { version = "=1.0", default-features = false, features = ["std"] }
proptest-derive = "0.3"
pretty_assertions = "1.3"
wasm-bindgen-test = "0.3.10"
expect-test = "1.0.1"
expect-test = "1.4.1"

[profile.release]
# Tell `rustc` to optimize for small code size.
Expand Down
50 changes: 50 additions & 0 deletions ergotree-interpreter/src/eval/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ impl<T> ExtResultEvalError<T> for Result<T, EvalError> {
mod tests {
use std::rc::Rc;

use ergotree_ir::mir::coll_by_index::ByIndex;
use ergotree_ir::mir::global_vars::GlobalVars;
use ergotree_ir::source_span::SourceSpan;
use expect_test::expect;

Expand Down Expand Up @@ -352,4 +354,52 @@ mod tests {
}
);
}

#[ignore = "expect test fails on self-generated string"]
#[test]
fn pretty_out_of_bounds() {
// OUTPUTS(1) should be out of bounds error in:
// {
// val v1 = 1
// OUPUTS(v1)
// }
let v1_id = 1.into();
let expr = Expr::BlockValue(
BlockValue {
items: vec![ValDef {
id: v1_id,
rhs: Box::new(Expr::Const(99999999i32.into())),
}
.into()],
result: Box::new(Expr::ByIndex(
ByIndex::new(
Expr::GlobalVars(GlobalVars::Outputs),
ValUse {
val_id: v1_id,
tpe: SType::SInt,
}
.into(),
None,
)
.unwrap()
.into(),
)),
}
.into(),
);
check(
expr,
expect![[r#"
x Evaluation error
,-[1:1]
1 | {
2 | val v1 = 99999999
3 | OUTPUTS(v1)
: ^^|^
: `-- error: ByIndex: index Int(99999999) out of bounds for collection size 1
4 | }
`----
"#]],
)
}
}
4 changes: 2 additions & 2 deletions ergotree-interpreter/src/eval/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
use ergotree_ir::mir::expr::Expr;
use ergotree_ir::mir::value::Value;

use super::error::ExtResultEvalError;
use super::Env;
use super::EvalContext;
use super::EvalError;
use super::Evaluable;
use super::error::ExtResultEvalError;

impl Evaluable for Expr {
fn eval(&self, env: &mut Env, ctx: &mut EvalContext) -> Result<Value, EvalError> {
Expand Down Expand Up @@ -54,7 +54,7 @@ impl Evaluable for Expr {
Expr::Downcast(op) => op.eval(env, ctx),
Expr::If(op) => op.eval(env, ctx),
Expr::Append(op) => op.expr().eval(env, ctx),
Expr::ByIndex(op) => op.eval(env, ctx),
Expr::ByIndex(op) => op.expr().eval(env, ctx),
Expr::ExtractScriptBytes(op) => op.eval(env, ctx),
Expr::SizeOf(op) => op.eval(env, ctx),
Expr::Slice(op) => op.eval(env, ctx),
Expand Down
4 changes: 2 additions & 2 deletions ergotree-ir/src/mir/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ pub enum Expr {
/// Box id, Blake2b256 hash of this box's content, basically equals to `blake2b256(bytes)`
ExtractId(ExtractId),
/// Collection, get element by index
ByIndex(ByIndex),
ByIndex(Spanned<ByIndex>),
/// Collection size
SizeOf(SizeOf),
/// Collection slice
Expand Down Expand Up @@ -265,7 +265,7 @@ impl Expr {
Expr::Upcast(v) => v.tpe(),
Expr::Downcast(v) => v.tpe(),
Expr::If(v) => v.tpe(),
Expr::ByIndex(v) => v.tpe(),
Expr::ByIndex(v) => v.expr().tpe(),
Expr::ExtractScriptBytes(v) => v.tpe(),
Expr::SizeOf(v) => v.tpe(),
Expr::Slice(v) => v.tpe(),
Expand Down
19 changes: 19 additions & 0 deletions ergotree-ir/src/pretty_printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use thiserror::Error;
use crate::mir::bin_op::BinOp;
use crate::mir::block::BlockValue;
use crate::mir::coll_append::Append;
use crate::mir::coll_by_index::ByIndex;
use crate::mir::constant::Constant;
use crate::mir::expr::Expr;
use crate::mir::global_vars::GlobalVars;
Expand Down Expand Up @@ -136,6 +137,23 @@ impl Print for GlobalVars {
}
}

impl Print for ByIndex {
fn print(&self, w: &mut dyn Printer) -> Result<Expr, PrintError> {
let input = self.input.print(w)?;
let offset = w.current_pos();
write!(w, "(")?;
let index = self.index.print(w)?;
write!(w, ")")?;
let length = w.current_pos() - offset;
#[allow(clippy::unwrap_used)] // we only added spans
Ok(Spanned {
source_span: SourceSpan { offset, length },
expr: ByIndex::new(input, index, self.default.clone()).unwrap(),
}
.into())
}
}

#[allow(clippy::panic)]
impl Print for Expr {
fn print(&self, w: &mut dyn Printer) -> Result<Expr, PrintError> {
Expand All @@ -147,6 +165,7 @@ impl Print for Expr {
Expr::Const(v) => v.print(w),
Expr::BinOp(v) => v.expr().print(w),
Expr::GlobalVars(v) => v.print(w),
Expr::ByIndex(v) => v.expr().print(w),
e => panic!("Not implemented: {:?}", e),
}
}
Expand Down
2 changes: 1 addition & 1 deletion ergotree-ir/src/serialization/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ impl SigmaSerializable for Expr {
Expr::Upcast(op) => op.sigma_serialize_w_opcode(w),
Expr::Downcast(op) => op.sigma_serialize_w_opcode(w),
Expr::If(op) => op.sigma_serialize_w_opcode(w),
Expr::ByIndex(op) => op.sigma_serialize_w_opcode(w),
Expr::ByIndex(op) => op.expr().sigma_serialize_w_opcode(w),
Expr::ExtractScriptBytes(op) => op.sigma_serialize_w_opcode(w),
Expr::SizeOf(op) => op.sigma_serialize_w_opcode(w),
Expr::Slice(op) => op.sigma_serialize_w_opcode(w),
Expand Down
6 changes: 4 additions & 2 deletions ergotree-ir/src/source_span.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use crate::mir::bin_op::BinOp;
use crate::mir::block::BlockValue;
use crate::mir::coll_append::Append;
use crate::mir::coll_by_index::ByIndex;
use crate::mir::expr::Expr;
use crate::mir::val_def::ValDef;

Expand Down Expand Up @@ -64,6 +65,7 @@ into_expr!(Append);
into_expr!(BlockValue);
into_expr!(ValDef);
into_expr!(BinOp);
into_expr!(ByIndex);

impl<T> From<T> for Spanned<T> {
fn from(v: T) -> Self {
Expand Down Expand Up @@ -92,7 +94,7 @@ impl Expr {
Expr::CalcSha256(_) => todo!(),
Expr::Context => todo!(),
Expr::Global => todo!(),
Expr::GlobalVars(_) => todo!(),
Expr::GlobalVars(_) => SourceSpan::empty(),
Expr::FuncValue(_) => todo!(),
Expr::Apply(_) => todo!(),
Expr::MethodCall(_) => todo!(),
Expand All @@ -119,7 +121,7 @@ impl Expr {
Expr::ExtractScriptBytes(_) => todo!(),
Expr::ExtractCreationInfo(_) => todo!(),
Expr::ExtractId(_) => todo!(),
Expr::ByIndex(_) => todo!(),
Expr::ByIndex(op) => op.source_span,
Expr::SizeOf(_) => todo!(),
Expr::Slice(_) => todo!(),
Expr::Fold(_) => todo!(),
Expand Down

0 comments on commit 60e25a6

Please sign in to comment.