Skip to content

Commit

Permalink
Display impl for SType;
Browse files Browse the repository at this point in the history
  • Loading branch information
greenhat committed Aug 16, 2023
1 parent 8784394 commit 3110470
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
14 changes: 14 additions & 0 deletions ergotree-ir/src/types/sfunc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,20 @@ pub struct SFunc {
pub tpe_params: Vec<STypeParam>,
}

impl std::fmt::Display for SFunc {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "(")?;
for (i, item) in self.t_dom.iter().enumerate() {
if i > 0 {
write!(f, ", ")?;
}
item.fmt(f)?;
}
write!(f, ") => ")?;
self.t_range.fmt(f)
}
}

impl SFunc {
/// Create new SFunc
pub fn new(t_dom: Vec<SType>, t_range: SType) -> Self {
Expand Down
13 changes: 13 additions & 0 deletions ergotree-ir/src/types/stuple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,19 @@ impl std::fmt::Debug for STuple {
}
}

impl std::fmt::Display for STuple {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "(")?;
for (i, item) in self.items.iter().enumerate() {
if i > 0 {
write!(f, ", ")?;
}
item.fmt(f)?;
}
write!(f, ")")
}
}

impl STuple {
/// Create a tuple type for a given type pair
pub fn pair(t1: SType, t2: SType) -> Self {
Expand Down
29 changes: 29 additions & 0 deletions ergotree-ir/src/types/stype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use std::collections::HashMap;
use std::convert::TryInto;
use std::fmt::Debug;

use impl_trait_for_tuples::impl_for_tuples;

Expand Down Expand Up @@ -126,6 +127,34 @@ impl From<SFunc> for SType {
}
}

impl std::fmt::Display for SType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
SType::STypeVar(t) => write!(f, "{}", t.as_string()),
SType::SAny => write!(f, "Any"),
SType::SUnit => write!(f, "Unit"),
SType::SBoolean => write!(f, "Boolean"),
SType::SByte => write!(f, "Byte"),
SType::SShort => write!(f, "Short"),
SType::SInt => write!(f, "Int"),
SType::SLong => write!(f, "Long"),
SType::SBigInt => write!(f, "BigInt"),
SType::SGroupElement => write!(f, "GroupElement"),
SType::SSigmaProp => write!(f, "SigmaProp"),
SType::SBox => write!(f, "Box"),
SType::SAvlTree => write!(f, "AvlTree"),
SType::SOption(t) => write!(f, "Option[{}]", t),
SType::SColl(t) => write!(f, "Coll[{}]", t),
SType::STuple(t) => write!(f, "{}", t),
SType::SFunc(t) => write!(f, "{}", t),
SType::SContext => write!(f, "Context"),
SType::SHeader => write!(f, "Header"),
SType::SPreHeader => write!(f, "PreHeader"),
SType::SGlobal => write!(f, "Global"),
}
}
}

/// Conversion to SType
pub trait LiftIntoSType {
/// get SType
Expand Down

0 comments on commit 3110470

Please sign in to comment.