Skip to content

Commit

Permalink
fix(vm): Indent the pretty printing of Value
Browse files Browse the repository at this point in the history
  • Loading branch information
Marwes committed Dec 6, 2017
1 parent 740de56 commit 5d64651
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 41 deletions.
17 changes: 13 additions & 4 deletions base/src/types/pretty_print.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
use std::borrow::Cow;

use pretty::{Arena, DocAllocator, DocBuilder};

use ast::{is_operator_char, Comment, CommentType};
use pos::{BytePos, Span};
use source::Source;

pub fn ident<'b>(arena: &'b Arena<'b>, name: &'b str) -> DocBuilder<'b, Arena<'b>> {
pub fn ident<'b, S>(arena: &'b Arena<'b>, name: S) -> DocBuilder<'b, Arena<'b>>
where
S: Into<Cow<'b, str>>,
{
let name = name.into();
if name.starts_with(is_operator_char) {
chain![arena; "(", name, ")"]
} else {
Expand All @@ -18,9 +24,12 @@ pub fn doc_comment<'a>(
) -> DocBuilder<'a, Arena<'a>> {
match text {
Some(comment) => match comment.typ {
CommentType::Line => arena.concat(comment.content.lines().map(|line| {
arena.text("/// ").append(line).append(arena.newline())
})),
CommentType::Line => arena.concat(
comment
.content
.lines()
.map(|line| arena.text("/// ").append(line).append(arena.newline())),
),
CommentType::Block => chain![arena;
"/**",
arena.newline(),
Expand Down
97 changes: 60 additions & 37 deletions vm/src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use base::symbol::Symbol;
use base::types::{ArcType, Type, TypeEnv};
use types::*;
use base::fnv::FnvMap;
use base::types::pretty_print::ident as pretty_ident;

use interner::InternedStr;
use compiler::DebugInfo;
Expand Down Expand Up @@ -386,6 +387,8 @@ impl<'t> ValuePrinter<'t> {
}
}

const INDENT: usize = 4;

struct InternalPrinter<'a, 't> {
typ: &'t ArcType,
env: &'t TypeEnv,
Expand Down Expand Up @@ -440,7 +443,7 @@ impl<'a, 't> InternalPrinter<'a, 't> {
arena.space(),
self.p(&info.typ, Top).pretty(*field)
]
}).intersperse(arena.text(","))),
}).intersperse(arena.text(","))).nest(INDENT),
">"
],
Value::Array(ref array) => chain![arena;
Expand All @@ -450,7 +453,8 @@ impl<'a, 't> InternalPrinter<'a, 't> {
Type::App(_, ref args) => self.p(&args[0], Top).pretty(field),
_ => arena.text(format!("{:?}", field)),
}
}).intersperse(arena.text(",").append(arena.space()))),
}).intersperse(arena.text(",").append(arena.space())))
.nest(INDENT),
"]"
],
Value::PartialApplication(p) => arena.text(format!("{:?}", p)),
Expand All @@ -461,18 +465,17 @@ impl<'a, 't> InternalPrinter<'a, 't> {
use base::types::BuiltinType;
match **self.typ {
Type::Builtin(BuiltinType::Int) => arena.text(format!("{}", i)),
Type::Builtin(BuiltinType::Char) =>
match ::std::char::from_u32(i as u32) {
Some('"') => arena.text(format!("'{}'", '"')),
Some(c) => arena.text(format!("'{}'", c.escape_default())),
None => ice!(
"Invalid character (code point {}) passed to pretty printing",
i
),
},
Type::Builtin(BuiltinType::Char) => match ::std::char::from_u32(i as u32) {
Some('"') => arena.text(format!("'{}'", '"')),
Some(c) => arena.text(format!("'{}'", c.escape_default())),
None => ice!(
"Invalid character (code point {}) passed to pretty printing",
i
),
},
_ => arena.text(format!("{}", i)),
}
},
}
Value::Float(f) => arena.text(format!("{}", f)),
}
}
Expand All @@ -493,27 +496,47 @@ impl<'a, 't> InternalPrinter<'a, 't> {
doc
}
}

use base::resolve::remove_aliases_cow;
use base::types::arg_iter;

let typ = remove_aliases_cow(self.env, self.typ);
let arena = self.arena;
match **typ {
Type::Record(ref row) => chain![arena;
"{",
arena.concat(fields.into_iter().zip(row.row_iter())
.map(|(field, type_field)| {
Type::Record(ref row) => {
let mut is_empty = true;
let fields_doc = arena.concat(
fields
.into_iter()
.zip(row.row_iter())
.map(|(field, type_field)| {
is_empty = false;
chain![arena;
pretty_ident(arena, type_field.name.declared_name().to_string()),
":",
chain![arena;
arena.space(),
type_field.name.to_string(),
":",
arena.space(),
self.p(&type_field.typ, Top).pretty(field)
]
}).intersperse(arena.text(","))),
arena.space(),
self.p(&type_field.typ, Top).pretty(field),
arena.text(",")
].nest(INDENT)
].group()
})
.intersperse(arena.space()),
);
chain![arena;
"{",
chain![arena;
arena.space(),
fields_doc
].nest(INDENT),
if is_empty {
arena.nil()
} else {
arena.space()
},
"}"
],
]
}
Type::Variant(ref row) => {
let type_field = row.row_iter()
.nth(tag as usize)
Expand All @@ -526,6 +549,7 @@ impl<'a, 't> InternalPrinter<'a, 't> {
empty = false;
arena.space().append(self.p(typ, Constructor).pretty(field))
}))
.nest(INDENT)
];
if empty {
doc
Expand All @@ -537,7 +561,8 @@ impl<'a, 't> InternalPrinter<'a, 't> {
"{",
arena.concat(fields.into_iter().map(|field| {
arena.space().append(self.p(&Type::hole(), Top).pretty(field))
}).intersperse(arena.text(","))),
}).intersperse(arena.text(",")))
.nest(INDENT),
arena.space(),
"}"
],
Expand Down Expand Up @@ -853,11 +878,11 @@ impl Repr {
Value::Float(_) => Repr::Float,
Value::String(_) => Repr::String,
Value::Array(_) => Repr::Array,
Value::Data(_) |
Value::Tag(_) |
Value::Function(_) |
Value::Closure(_) |
Value::PartialApplication(_) => Repr::Unknown,
Value::Data(_)
| Value::Tag(_)
| Value::Function(_)
| Value::Closure(_)
| Value::PartialApplication(_) => Repr::Unknown,
Value::Userdata(_) => Repr::Userdata,
Value::Thread(_) => Repr::Thread,
}
Expand Down Expand Up @@ -1187,13 +1212,11 @@ impl<'t> Cloner<'t> {

fn deep_clone_str(&mut self, data: GcStr) -> Result<Value> {
unsafe {
Ok(
self.deep_clone_ptr(data.into_inner(), |gc, data| {
let ptr = GcStr::from_utf8_unchecked(gc.alloc(data)?);
Ok((String(ptr), ptr))
})?
.unwrap_or_else(String),
)
Ok(self.deep_clone_ptr(data.into_inner(), |gc, data| {
let ptr = GcStr::from_utf8_unchecked(gc.alloc(data)?);
Ok((String(ptr), ptr))
})?
.unwrap_or_else(String))
}
}
fn deep_clone_data(&mut self, data_ptr: GcPtr<DataStruct>) -> Result<GcPtr<DataStruct>> {
Expand Down

0 comments on commit 5d64651

Please sign in to comment.