Skip to content

Commit

Permalink
draft the expr printing and running eval again in reduce_to_crypto;
Browse files Browse the repository at this point in the history
  • Loading branch information
greenhat committed Aug 2, 2023
1 parent a3325f6 commit 6164c4e
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 29 deletions.
67 changes: 43 additions & 24 deletions ergotree-interpreter/src/eval.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
//! Interpreter
use bounded_vec::BoundedVecOutOfBounds;
use ergotree_ir::mir::constant::TryExtractInto;
use ergotree_ir::mir::expr::Span;
use ergotree_ir::pretty_printer::PosTrackingWriter;
use ergotree_ir::pretty_printer::Print;
use ergotree_ir::sigma_protocol::sigma_boolean::SigmaProp;
use ergotree_ir::source_span::Span;
use sigma_ser::ScorexParsingError;
use sigma_ser::ScorexSerializationError;
use std::rc::Rc;
Expand Down Expand Up @@ -201,29 +203,46 @@ pub fn reduce_to_crypto(
env: &Env,
ctx: Rc<Context>,
) -> Result<ReductionResult, EvalError> {
let cost_accum = CostAccumulator::new(0, None);
let mut ectx = EvalContext::new(ctx, cost_accum);
let mut env_mut = env.clone();
expr.eval(&mut env_mut, &mut ectx)
.and_then(|v| -> Result<ReductionResult, EvalError> {
match v {
Value::Boolean(b) => Ok(ReductionResult {
sigma_prop: SigmaBoolean::TrivialProp(b),
cost: 0,
env: env_mut.clone(),
}),
Value::SigmaProp(sp) => Ok(ReductionResult {
sigma_prop: sp.value().clone(),
cost: 0,
env: env_mut.clone(),
}),
_ => Err(EvalError::InvalidResultType),
}
})
.map_err(|e| EvalError::WrappedWithEnvError {
error: Box::new(e),
env: env_mut,
})
let env_clone = env.clone();
let ctx_clone = ctx.clone();
fn inner(expr: &Expr, env: &Env, ctx: Rc<Context>) -> Result<ReductionResult, EvalError> {
let cost_accum = CostAccumulator::new(0, None);
let mut ectx = EvalContext::new(ctx, cost_accum);
let mut env_mut = env.clone();
expr.eval(&mut env_mut, &mut ectx)
.and_then(|v| -> Result<ReductionResult, EvalError> {
match v {
Value::Boolean(b) => Ok(ReductionResult {
sigma_prop: SigmaBoolean::TrivialProp(b),
cost: 0,
env: env_mut.clone(),
}),
Value::SigmaProp(sp) => Ok(ReductionResult {
sigma_prop: sp.value().clone(),
cost: 0,
env: env_mut.clone(),
}),
_ => Err(EvalError::InvalidResultType),
}
})
}

let res = inner(expr, env, ctx);
if res.is_ok() {
return res;
}

let mut printer = PosTrackingWriter::new();
let spanned_expr = expr
.print(&mut printer)
.map_err(|e| EvalError::Misc(format!("printer error: {}", e)))?;
let printed_expr_str = printer.get_buf();

Check failure on line 239 in ergotree-interpreter/src/eval.rs

View workflow job for this annotation

GitHub Actions / clippy

unused variable: `printed_expr_str`

error: unused variable: `printed_expr_str` --> ergotree-interpreter/src/eval.rs:239:9 | 239 | let printed_expr_str = printer.get_buf(); | ^^^^^^^^^^^^^^^^ help: if this is intentional, prefix it with an underscore: `_printed_expr_str` | = note: `-D unused-variables` implied by `-D warnings`

Check warning on line 239 in ergotree-interpreter/src/eval.rs

View workflow job for this annotation

GitHub Actions / Build without default features

unused variable: `printed_expr_str`

Check warning on line 239 in ergotree-interpreter/src/eval.rs

View workflow job for this annotation

GitHub Actions / Tests on ubuntu-latest

unused variable: `printed_expr_str`

Check warning on line 239 in ergotree-interpreter/src/eval.rs

View workflow job for this annotation

GitHub Actions / Tests on ubuntu-latest

unused variable: `printed_expr_str`

Check warning on line 239 in ergotree-interpreter/src/eval.rs

View workflow job for this annotation

GitHub Actions / Tests on macOS-latest

unused variable: `printed_expr_str`

Check warning on line 239 in ergotree-interpreter/src/eval.rs

View workflow job for this annotation

GitHub Actions / Tests on windows-latest

unused variable: `printed_expr_str`
// TODO: cut the part of the printed_expr_str relevant to the span of the expr where error was generated
// and include it in the returned error
inner(&spanned_expr, env, ctx_clone).map_err(|e| EvalError::WrappedWithEnvError {
error: Box::new(e),
env: env_clone,
})
}

/// Expects SigmaProp constant value and returns it's value. Otherwise, returns an error.
Expand Down
2 changes: 1 addition & 1 deletion ergotree-interpreter/src/eval/expr.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use ergotree_ir::mir::expr::Expr;
use ergotree_ir::mir::expr::Span;
use ergotree_ir::mir::value::Value;
use ergotree_ir::source_span::Span;

use super::Env;
use super::EvalContext;
Expand Down
2 changes: 1 addition & 1 deletion ergotree-ir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ pub mod source_span;
pub mod type_check;
pub mod types;
pub mod util;
mod pretty_printer;
pub mod pretty_printer;
33 changes: 30 additions & 3 deletions ergotree-ir/src/pretty_printer.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! Pretty printer for ErgoTree IR

use std::fmt::Write;

use thiserror::Error;
Expand All @@ -7,13 +9,17 @@ use crate::mir::expr::Expr;
use crate::source_span::Span;
use crate::source_span::Spanned;

/// Print error
#[allow(missing_docs)]
#[derive(PartialEq, Eq, Debug, Clone, Error)]
pub(crate) enum PrintError {
pub enum PrintError {
#[error("fmt error: {0:?}")]
FmtError(#[from] std::fmt::Error),
}

trait Print {
/// Print trait for Expr that sets the source span for the resulting Expr
pub trait Print {
/// Print the expression and return the resulting expression with source span
fn print(&self, w: &mut dyn Printer) -> Result<Expr, PrintError>;
}

Expand Down Expand Up @@ -47,12 +53,17 @@ impl Print for Expr {
}

// TODO: extract to a separate module
/// Printer trait with tracking of current position and indent
pub trait Printer: Write {
/// Current position (last printed char)
fn current_pos(&self) -> usize;
/// Increase indent
fn inc_ident(&mut self);
/// Decrease indent
fn dec_ident(&mut self);
}

/// Printer implementation with tracking of current position and indent
pub struct PosTrackingWriter {
print_buf: String,
current_pos: usize,
Expand Down Expand Up @@ -95,11 +106,27 @@ impl Printer for PosTrackingWriter {
impl PosTrackingWriter {
const INDENT: usize = 4;

fn get_buf(&self) -> &str {
/// Create new printer
pub fn new() -> Self {
Self {
print_buf: String::new(),
current_pos: 0,
current_indent: 0,
}
}

/// Get printed buffer
pub fn get_buf(&self) -> &str {
&self.print_buf
}
}

impl Default for PosTrackingWriter {
fn default() -> Self {
Self::new()
}
}

#[allow(clippy::unwrap_used)]
#[cfg(test)]
mod tests {
Expand Down

0 comments on commit 6164c4e

Please sign in to comment.