Skip to content

Commit

Permalink
impl Print for GlobalVars;
Browse files Browse the repository at this point in the history
  • Loading branch information
greenhat committed Aug 11, 2023
1 parent 323fbeb commit 31a3da2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
15 changes: 15 additions & 0 deletions ergotree-ir/src/mir/global_vars.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Global variables

use std::fmt::Display;

use crate::has_opcode::HasOpCode;
use crate::serialization::op_code::OpCode;
use crate::types::stype::SType;
Expand Down Expand Up @@ -49,6 +51,19 @@ impl HasOpCode for GlobalVars {
}
}

impl Display for GlobalVars {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
GlobalVars::SelfBox => write!(f, "SELF_BOX"),
GlobalVars::Inputs => write!(f, "INPUTS"),
GlobalVars::Outputs => write!(f, "OUTPUTS"),
GlobalVars::Height => write!(f, "HEIGHT"),
GlobalVars::MinerPubKey => write!(f, "MINER_PUBKEY"),
GlobalVars::GroupGenerator => write!(f, "GROUP_GENERATOR"),
}
}
}

#[cfg(test)]
#[cfg(feature = "arbitrary")]
mod tests {
Expand Down
9 changes: 9 additions & 0 deletions ergotree-ir/src/pretty_printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::mir::block::BlockValue;
use crate::mir::coll_append::Append;
use crate::mir::constant::Constant;
use crate::mir::expr::Expr;
use crate::mir::global_vars::GlobalVars;
use crate::mir::val_def::ValDef;
use crate::mir::val_use::ValUse;
use crate::source_span::SourceSpan;
Expand Down Expand Up @@ -128,6 +129,13 @@ impl Print for BinOp {
}
}

impl Print for GlobalVars {
fn print(&self, w: &mut dyn Printer) -> Result<Expr, PrintError> {
write!(w, "{}", self)?;
Ok(self.clone().into())
}
}

#[allow(clippy::panic)]
impl Print for Expr {
fn print(&self, w: &mut dyn Printer) -> Result<Expr, PrintError> {
Expand All @@ -138,6 +146,7 @@ impl Print for Expr {
Expr::ValUse(v) => v.print(w),
Expr::Const(v) => v.print(w),
Expr::BinOp(v) => v.expr().print(w),
Expr::GlobalVars(v) => v.print(w),
e => panic!("Not implemented: {:?}", e),
}
}
Expand Down

0 comments on commit 31a3da2

Please sign in to comment.