Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion moon.mod.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "Yoorkin/prettyprinter",
"version": "0.4.1",
"version": "0.4.3",
"readme": "README.md",
"repository": "https://github.com/Yoorkin/prettyprinter",
"license": "Apache-2.0",
Expand Down
21 changes: 10 additions & 11 deletions src/document.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -93,24 +93,25 @@ pub impl Show for Document with output(self, buf) {

///|
fn render_document(width : Int, doc : Document) -> String {
let buf = @buffer.new()
let buf = StringBuilder::new()
let queue = [(0, false, doc)]
let mut column = 0
fn aux(indent, flat_mode, doc) {
while queue.pop() is Some((indent, flat_mode, doc)) {
match doc {
Empty => ()
Text(s) => {
buf.write_string(s)
column += s.length()
}
Concat(_, l, r) => {
aux(indent, flat_mode, l)
aux(indent, flat_mode, r)
queue.push((indent, flat_mode, r))
queue.push((indent, flat_mode, l))
}
Switch(_, l, r) =>
if flat_mode {
aux(indent, flat_mode, l)
queue.push((indent, flat_mode, l))
} else {
aux(indent, flat_mode, r)
queue.push((indent, flat_mode, r))
}
Line => {
buf.write_char('\n')
Expand All @@ -119,14 +120,12 @@ fn render_document(width : Int, doc : Document) -> String {
}
column = indent
}
Nest(_, i, d) => aux(indent + i, flat_mode, d)
Nest(_, i, d) => queue.push((indent + i, flat_mode, d))
Group(req, d) => {
let flat_mode = Space(column) + req <= Space(width)
aux(indent, flat_mode, d)
queue.push((indent, flat_mode, d))
}
}
}

aux(0, true, doc)
buf.contents().to_unchecked_string()
buf.to_string()
}
8 changes: 8 additions & 0 deletions src/test.mbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
///|
test "stack-safe test for render" {
let mut doc = empty
for i in 0..=3000 {
doc = doc + text(i.to_string()) + line
}
doc.pretty() |> ignore
}
Loading