Skip to content

Commit

Permalink
implmennt pretty printing for And, Or, LogicalNot, Xor, SubstConstant…
Browse files Browse the repository at this point in the history
…s, Atleast;
  • Loading branch information
greenhat committed Sep 22, 2023
1 parent 7c99195 commit 1f7a246
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 14 deletions.
12 changes: 6 additions & 6 deletions ergotree-ir/src/mir/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,15 @@ pub enum Expr {
/// Binary operation
BinOp(Spanned<BinOp>),
/// Logical AND
And(And),
And(Spanned<And>),
/// Logical OR
Or(Or),
Or(Spanned<Or>),
/// Byte-wise XOR
Xor(Xor),
/// THRESHOLD composition for sigma expressions
Atleast(Atleast),
/// LogicalNot
LogicalNot(LogicalNot),
LogicalNot(Spanned<LogicalNot>),
/// Negation on numeric type
Negation(Spanned<Negation>),
/// Bit inversion on numeric type
Expand Down Expand Up @@ -256,11 +256,11 @@ impl Expr {
Expr::Fold(v) => v.expr().tpe(),
Expr::SelectField(v) => v.expr().tpe(),
Expr::ExtractAmount(v) => v.tpe(),
Expr::And(v) => v.tpe(),
Expr::Or(v) => v.tpe(),
Expr::And(v) => v.expr().tpe(),
Expr::Or(v) => v.expr().tpe(),
Expr::Xor(v) => v.tpe(),
Expr::Atleast(v) => v.tpe(),
Expr::LogicalNot(v) => v.tpe(),
Expr::LogicalNot(v) => v.expr().tpe(),
Expr::Map(v) => v.expr().tpe(),
Expr::Filter(v) => v.expr().tpe(),
Expr::BoolToSigmaProp(v) => v.tpe(),
Expand Down
107 changes: 99 additions & 8 deletions ergotree-ir/src/pretty_printer/print.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use thiserror::Error;

use crate::mir::and::And;
use crate::mir::apply::Apply;
use crate::mir::atleast::Atleast;
use crate::mir::bin_op::BinOp;
use crate::mir::block::BlockValue;
use crate::mir::bool_to_sigma::BoolToSigmaProp;
Expand Down Expand Up @@ -35,15 +37,18 @@ use crate::mir::method_call::MethodCall;
use crate::mir::negation::Negation;
use crate::mir::option_get::OptionGet;
use crate::mir::option_is_defined::OptionIsDefined;
use crate::mir::or::Or;
use crate::mir::property_call::PropertyCall;
use crate::mir::select_field::SelectField;
use crate::mir::sigma_and::SigmaAnd;
use crate::mir::sigma_or::SigmaOr;
use crate::mir::subst_const::SubstConstants;
use crate::mir::tuple::Tuple;
use crate::mir::unary_op::OneArgOpTryBuild;
use crate::mir::upcast::Upcast;
use crate::mir::val_def::ValDef;
use crate::mir::val_use::ValUse;
use crate::mir::xor::Xor;
use crate::source_span::SourceSpan;
use crate::source_span::Spanned;
use crate::types::stype::SType;
Expand Down Expand Up @@ -77,7 +82,7 @@ impl Print for Expr {
Expr::GlobalVars(v) => v.print(w),
Expr::ByIndex(v) => v.expr().print(w),
Expr::ConstPlaceholder(_) => Ok(self.clone()),
Expr::SubstConstants(_) => todo!(),
Expr::SubstConstants(v) => v.expr().print(w),
Expr::ByteArrayToLong(v) => v.expr().print(w),
Expr::ByteArrayToBigInt(v) => v.expr().print(w),
Expr::LongToByteArray(v) => v.print(w),
Expand All @@ -89,17 +94,20 @@ impl Print for Expr {
write!(w, "CONTEXT")?;
Ok(self.clone())
}
Expr::Global => todo!(),
Expr::Global => {
write!(w, "GLOBAL")?;
Ok(self.clone())
}
Expr::FuncValue(v) => v.print(w),
Expr::Apply(v) => v.print(w),
Expr::MethodCall(v) => v.expr().print(w),
Expr::PropertyCall(v) => v.expr().print(w),
Expr::If(v) => v.print(w),
Expr::And(_) => todo!(),
Expr::Or(_) => todo!(),
Expr::Xor(_) => todo!(),
Expr::Atleast(_) => todo!(),
Expr::LogicalNot(v) => v.print(w),
Expr::And(v) => v.expr().print(w),
Expr::Or(v) => v.expr().print(w),
Expr::Xor(v) => v.print(w),
Expr::Atleast(v) => v.print(w),
Expr::LogicalNot(v) => v.expr().print(w),
Expr::Negation(v) => v.expr().print(w),
Expr::BitInversion(_) => todo!(),
Expr::OptionGet(v) => v.expr().print(w),
Expand Down Expand Up @@ -564,6 +572,38 @@ impl Print for SigmaOr {
}
}

impl Print for And {
fn print(&self, w: &mut dyn Printer) -> Result<Expr, PrintError> {
let offset = w.current_pos();
write!(w, "&&")?;
let input = self.input.print(w)?;
let length = w.current_pos() - offset;
Ok(Spanned {
expr: And {
input: Box::new(input),
},
source_span: SourceSpan { offset, length },
}
.into())
}
}

impl Print for Or {
fn print(&self, w: &mut dyn Printer) -> Result<Expr, PrintError> {
let offset = w.current_pos();
write!(w, "||")?;
let input = self.input.print(w)?;
let length = w.current_pos() - offset;
Ok(Spanned {
expr: Or {
input: Box::new(input),
},
source_span: SourceSpan { offset, length },
}
.into())
}
}

impl Print for CreateProveDlog {
fn print(&self, w: &mut dyn Printer) -> Result<Expr, PrintError> {
write!(w, "proveDlog(")?;
Expand Down Expand Up @@ -771,4 +811,55 @@ impl Print for CalcSha256 {
}
.into())
}
}
}

impl Print for Xor {
fn print(&self, w: &mut dyn Printer) -> Result<Expr, PrintError> {
let left = self.left.print(w)?;
write!(w, "^")?;
let right = self.right.print(w)?;
Ok(Xor {
left: left.into(),
right: right.into(),
}
.into())
}
}

impl Print for Atleast {
fn print(&self, w: &mut dyn Printer) -> Result<Expr, PrintError> {
write!(w, ".atLeast(")?;
let bound = self.bound.print(w)?;
write!(w, ", ")?;
let input = self.input.print(w)?;
write!(w, ")")?;
Ok(Atleast {
input: Box::new(input),
bound: Box::new(bound),
}
.into())
}
}

impl Print for SubstConstants {
fn print(&self, w: &mut dyn Printer) -> Result<Expr, PrintError> {
let offset = w.current_pos();
write!(w, ".substConstants(")?;
let script_bytes = self.script_bytes.print(w)?;
write!(w, ", ")?;
let positions = self.positions.print(w)?;
write!(w, ", ")?;
let new_values = self.new_values.print(w)?;
write!(w, ")")?;
let length = w.current_pos() - offset;
Ok(Spanned {
expr: SubstConstants {
script_bytes: script_bytes.into(),
positions: positions.into(),
new_values: new_values.into(),
},
source_span: SourceSpan { offset, length },
}
.into())
}
}
6 changes: 6 additions & 0 deletions ergotree-ir/src/source_span.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
//! Source position for an IR node in the source code
use crate::mir::logical_not::LogicalNot;
use crate::mir::or::Or;
use crate::mir::and::And;
use crate::mir::bin_op::BinOp;
use crate::mir::block::BlockValue;
use crate::mir::byte_array_to_bigint::ByteArrayToBigInt;
Expand Down Expand Up @@ -117,6 +120,9 @@ into_expr!(GetVar);
into_expr!(DeserializeRegister);
into_expr!(DeserializeContext);
into_expr!(TreeLookup);
into_expr!(And);
into_expr!(Or);
into_expr!(LogicalNot);

impl<T> From<T> for Spanned<T> {
fn from(v: T) -> Self {
Expand Down

0 comments on commit 1f7a246

Please sign in to comment.