Skip to content

Commit

Permalink
impl Print for ValUse, fix indentations and new lines in pretty printer;
Browse files Browse the repository at this point in the history
  • Loading branch information
greenhat committed Aug 7, 2023
1 parent 9c3809a commit f744c67
Showing 1 changed file with 22 additions and 15 deletions.
37 changes: 22 additions & 15 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::coll_append::Append;
use crate::mir::constant::Constant;
use crate::mir::expr::Expr;
use crate::mir::val_def::ValDef;
use crate::mir::val_use::ValUse;
use crate::source_span::Span;
use crate::source_span::Spanned;

Expand All @@ -31,13 +32,17 @@ impl Print for BlockValue {
let start = w.current_pos();
writeln!(w, "{{")?;
w.inc_ident();
let indent = w.get_indent();
for item in &self.items {
write!(w, "{:indent$}", "", indent = indent)?;
item.print(w)?;
writeln!(w)?;
}
// indent for result
write!(w, "{:indent$}", "", indent = indent)?;
self.result.print(w)?;
w.dec_ident();
write!(w, "\n}}")?;
writeln!(w, "\n}}")?;
let end = w.current_pos();
Ok(Spanned {
source_span: Span { start, end },
Expand All @@ -53,7 +58,6 @@ impl Print for ValDef {
write!(w, "val v{} = ", self.id)?;
self.rhs.print(w)?;
let end = w.current_pos();
writeln!(w)?;
Ok(Spanned {
source_span: Span { start, end },
expr: self.clone(),
Expand All @@ -70,6 +74,13 @@ impl Print for Constant {
}
}

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

impl Print for Append {
fn print(&self, w: &mut dyn Printer) -> Result<Expr, PrintError> {
let start = w.current_pos();
Expand All @@ -93,6 +104,7 @@ impl Print for Expr {
Expr::Append(v) => v.expr().print(w),
Expr::BlockValue(v) => v.expr().print(w),
Expr::ValDef(v) => v.expr().print(w),
Expr::ValUse(v) => v.print(w),
Expr::Const(v) => v.print(w),
e => panic!("Not implemented: {:?}", e),
}
Expand All @@ -108,6 +120,8 @@ pub trait Printer: Write {
fn inc_ident(&mut self);
/// Decrease indent
fn dec_ident(&mut self);
/// Get current indent
fn get_indent(&self) -> usize;
}

/// Printer implementation with tracking of current position and indent
Expand All @@ -119,20 +133,9 @@ pub struct PosTrackingWriter {

impl Write for PosTrackingWriter {
fn write_str(&mut self, s: &str) -> std::fmt::Result {
let indented_str = s
.lines()
.map(|l| {
let mut indent = String::new();
for _ in 0..self.current_indent {
indent.push(' ');
}
format!("{}{}", indent, l)
})
.collect::<Vec<String>>()
.join("\n");
let len = s.len();
self.current_pos += len;
write!(self.print_buf, "{}", indented_str)
write!(self.print_buf, "{}", s)
}
}

Expand All @@ -148,6 +151,10 @@ impl Printer for PosTrackingWriter {
fn dec_ident(&mut self) {
self.current_indent -= Self::INDENT;
}

fn get_indent(&self) -> usize {
self.current_indent
}
}

impl PosTrackingWriter {
Expand Down Expand Up @@ -201,7 +208,7 @@ mod tests {

#[test]
fn print_block() {
let val_id = 2.into();
let val_id = 1.into();
let expr = Expr::BlockValue(
BlockValue {
items: vec![ValDef {
Expand Down

0 comments on commit f744c67

Please sign in to comment.