mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-26 21:39:07 +00:00
Pretty print symbols
This commit is contained in:
parent
b0300e4e90
commit
01dea13b49
5 changed files with 109 additions and 110 deletions
|
@ -2177,7 +2177,7 @@ macro_rules! debug_print_ir {
|
||||||
let procs_string = $state
|
let procs_string = $state
|
||||||
.procedures
|
.procedures
|
||||||
.values()
|
.values()
|
||||||
.map(|proc| proc.to_pretty($interner, 200))
|
.map(|proc| proc.to_pretty($interner, 200, true))
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
let result = procs_string.join("\n");
|
let result = procs_string.join("\n");
|
||||||
|
|
|
@ -58,7 +58,7 @@ where
|
||||||
docs.sort_by_key(|(line, _)| *line);
|
docs.sort_by_key(|(line, _)| *line);
|
||||||
|
|
||||||
let src = proc
|
let src = proc
|
||||||
.to_doc(f, interner, Parens::NotNeeded)
|
.to_doc(f, interner, true, Parens::NotNeeded)
|
||||||
.1
|
.1
|
||||||
.pretty(80)
|
.pretty(80)
|
||||||
.to_string();
|
.to_string();
|
||||||
|
@ -127,12 +127,12 @@ where
|
||||||
docs_before = vec![(
|
docs_before = vec![(
|
||||||
old_line,
|
old_line,
|
||||||
f.concat([
|
f.concat([
|
||||||
f.as_string(symbol.as_str(interns)),
|
format_symbol(f, interns, symbol),
|
||||||
f.reflow(" first defined here"),
|
f.reflow(" first defined here"),
|
||||||
]),
|
]),
|
||||||
)];
|
)];
|
||||||
f.concat([
|
f.concat([
|
||||||
f.as_string(symbol.as_str(interns)),
|
format_symbol(f, interns, symbol),
|
||||||
f.reflow(" re-defined here"),
|
f.reflow(" re-defined here"),
|
||||||
])
|
])
|
||||||
}
|
}
|
||||||
|
@ -140,7 +140,7 @@ where
|
||||||
title = "SYMBOL NOT DEFINED";
|
title = "SYMBOL NOT DEFINED";
|
||||||
docs_before = vec![];
|
docs_before = vec![];
|
||||||
f.concat([
|
f.concat([
|
||||||
f.as_string(symbol.as_str(interns)),
|
format_symbol(f, interns, symbol),
|
||||||
f.reflow(" not found in the present scope"),
|
f.reflow(" not found in the present scope"),
|
||||||
])
|
])
|
||||||
}
|
}
|
||||||
|
@ -155,13 +155,13 @@ where
|
||||||
docs_before = vec![(
|
docs_before = vec![(
|
||||||
def_line,
|
def_line,
|
||||||
f.concat([
|
f.concat([
|
||||||
f.as_string(symbol.as_str(interns)),
|
format_symbol(f, interns, symbol),
|
||||||
f.reflow(" defined here with layout "),
|
f.reflow(" defined here with layout "),
|
||||||
def_layout.to_doc(f, interner, Parens::NotNeeded),
|
def_layout.to_doc(f, interner, Parens::NotNeeded),
|
||||||
]),
|
]),
|
||||||
)];
|
)];
|
||||||
f.concat([
|
f.concat([
|
||||||
f.as_string(symbol.as_str(interns)),
|
format_symbol(f, interns, symbol),
|
||||||
f.reflow(" used as a "),
|
f.reflow(" used as a "),
|
||||||
f.reflow(format_use_kind(use_kind)),
|
f.reflow(format_use_kind(use_kind)),
|
||||||
f.reflow(" here with layout "),
|
f.reflow(" here with layout "),
|
||||||
|
@ -176,7 +176,7 @@ where
|
||||||
title = "SYMBOL INITIALIZER HAS THE WRONG LAYOUT";
|
title = "SYMBOL INITIALIZER HAS THE WRONG LAYOUT";
|
||||||
docs_before = vec![];
|
docs_before = vec![];
|
||||||
f.concat([
|
f.concat([
|
||||||
f.as_string(symbol.as_str(interns)),
|
format_symbol(f, interns, symbol),
|
||||||
f.reflow(" is defined as "),
|
f.reflow(" is defined as "),
|
||||||
def_layout.to_doc(f, interner, Parens::NotNeeded),
|
def_layout.to_doc(f, interner, Parens::NotNeeded),
|
||||||
f.reflow(" but its initializer is "),
|
f.reflow(" but its initializer is "),
|
||||||
|
@ -261,7 +261,7 @@ where
|
||||||
f,
|
f,
|
||||||
[f.concat([
|
[f.concat([
|
||||||
f.reflow("The following specializations of "),
|
f.reflow("The following specializations of "),
|
||||||
f.as_string(symbol.as_str(interns)),
|
format_symbol(f, interns, symbol),
|
||||||
f.reflow(" were built:"),
|
f.reflow(" were built:"),
|
||||||
stack(f, similars),
|
stack(f, similars),
|
||||||
])],
|
])],
|
||||||
|
@ -285,7 +285,7 @@ where
|
||||||
def_line,
|
def_line,
|
||||||
f.concat([
|
f.concat([
|
||||||
f.reflow("The struct "),
|
f.reflow("The struct "),
|
||||||
f.as_string(structure.as_str(interns)),
|
format_symbol(f, interns, structure),
|
||||||
f.reflow(" defined here has "),
|
f.reflow(" defined here has "),
|
||||||
f.as_string(size),
|
f.as_string(size),
|
||||||
f.reflow(" fields"),
|
f.reflow(" fields"),
|
||||||
|
@ -305,7 +305,7 @@ where
|
||||||
def_line,
|
def_line,
|
||||||
f.concat([
|
f.concat([
|
||||||
f.reflow("The value "),
|
f.reflow("The value "),
|
||||||
f.as_string(structure.as_str(interns)),
|
format_symbol(f, interns, structure),
|
||||||
f.reflow(" defined here"),
|
f.reflow(" defined here"),
|
||||||
]),
|
]),
|
||||||
)];
|
)];
|
||||||
|
@ -322,7 +322,7 @@ where
|
||||||
def_line,
|
def_line,
|
||||||
f.concat([
|
f.concat([
|
||||||
f.reflow("The union "),
|
f.reflow("The union "),
|
||||||
f.as_string(structure.as_str(interns)),
|
format_symbol(f, interns, structure),
|
||||||
f.reflow(" defined here has layout "),
|
f.reflow(" defined here has layout "),
|
||||||
Layout::Union(union_layout).to_doc(f, interner, Parens::NotNeeded),
|
Layout::Union(union_layout).to_doc(f, interner, Parens::NotNeeded),
|
||||||
]),
|
]),
|
||||||
|
@ -341,7 +341,7 @@ where
|
||||||
def_line,
|
def_line,
|
||||||
f.concat([
|
f.concat([
|
||||||
f.reflow("The union "),
|
f.reflow("The union "),
|
||||||
f.as_string(structure.as_str(interns)),
|
format_symbol(f, interns, structure),
|
||||||
f.reflow(" defined here has "),
|
f.reflow(" defined here has "),
|
||||||
f.as_string(size),
|
f.as_string(size),
|
||||||
f.reflow(" payloads at ID "),
|
f.reflow(" payloads at ID "),
|
||||||
|
@ -365,7 +365,7 @@ where
|
||||||
def_line,
|
def_line,
|
||||||
f.concat([
|
f.concat([
|
||||||
f.reflow("The union "),
|
f.reflow("The union "),
|
||||||
f.as_string(structure.as_str(interns)),
|
format_symbol(f, interns, structure),
|
||||||
f.reflow(" defined here has layout "),
|
f.reflow(" defined here has layout "),
|
||||||
Layout::Union(union_layout).to_doc(f, interner, Parens::NotNeeded),
|
Layout::Union(union_layout).to_doc(f, interner, Parens::NotNeeded),
|
||||||
]),
|
]),
|
||||||
|
@ -380,10 +380,7 @@ where
|
||||||
title = "ATTEMPTING TO UNBOX A NON-BOX";
|
title = "ATTEMPTING TO UNBOX A NON-BOX";
|
||||||
docs_before = vec![(
|
docs_before = vec![(
|
||||||
def_line,
|
def_line,
|
||||||
f.concat([
|
f.concat([format_symbol(f, interns, symbol), f.reflow(" is not a box")]),
|
||||||
f.as_string(symbol.as_str(interns)),
|
|
||||||
f.reflow(" is not a box"),
|
|
||||||
]),
|
|
||||||
)];
|
)];
|
||||||
f.reflow("but is being unboxed here")
|
f.reflow("but is being unboxed here")
|
||||||
}
|
}
|
||||||
|
@ -417,6 +414,12 @@ where
|
||||||
(title, docs_before, doc)
|
(title, docs_before, doc)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn format_symbol<'d>(f: &'d Arena<'d>, interns: &'d Interns, symbol: Symbol) -> Doc<'d> {
|
||||||
|
f.text(symbol.module_string(interns).to_string())
|
||||||
|
.append(f.text("."))
|
||||||
|
.append(f.text(symbol.as_str(interns)))
|
||||||
|
}
|
||||||
|
|
||||||
fn format_use_kind(use_kind: UseKind) -> &'static str {
|
fn format_use_kind(use_kind: UseKind) -> &'static str {
|
||||||
match use_kind {
|
match use_kind {
|
||||||
UseKind::Ret => "return value",
|
UseKind::Ret => "return value",
|
||||||
|
|
|
@ -326,6 +326,7 @@ impl<'a> Proc<'a> {
|
||||||
&'b self,
|
&'b self,
|
||||||
alloc: &'b D,
|
alloc: &'b D,
|
||||||
interner: &'b I,
|
interner: &'b I,
|
||||||
|
pretty: bool,
|
||||||
_parens: Parens,
|
_parens: Parens,
|
||||||
) -> DocBuilder<'b, D, A>
|
) -> DocBuilder<'b, D, A>
|
||||||
where
|
where
|
||||||
|
@ -335,7 +336,7 @@ impl<'a> Proc<'a> {
|
||||||
I: Interner<'a, Layout<'a>>,
|
I: Interner<'a, Layout<'a>>,
|
||||||
{
|
{
|
||||||
let args_doc = self.args.iter().map(|(layout, symbol)| {
|
let args_doc = self.args.iter().map(|(layout, symbol)| {
|
||||||
let arg_doc = symbol_to_doc(alloc, *symbol);
|
let arg_doc = symbol_to_doc(alloc, *symbol, pretty);
|
||||||
if pretty_print_ir_symbols() {
|
if pretty_print_ir_symbols() {
|
||||||
arg_doc.append(alloc.reflow(": ")).append(layout.to_doc(
|
arg_doc.append(alloc.reflow(": ")).append(layout.to_doc(
|
||||||
alloc,
|
alloc,
|
||||||
|
@ -350,36 +351,36 @@ impl<'a> Proc<'a> {
|
||||||
if pretty_print_ir_symbols() {
|
if pretty_print_ir_symbols() {
|
||||||
alloc
|
alloc
|
||||||
.text("procedure : ")
|
.text("procedure : ")
|
||||||
.append(symbol_to_doc(alloc, self.name.name()))
|
.append(symbol_to_doc(alloc, self.name.name(), pretty))
|
||||||
.append(" ")
|
.append(" ")
|
||||||
.append(self.ret_layout.to_doc(alloc, interner, Parens::NotNeeded))
|
.append(self.ret_layout.to_doc(alloc, interner, Parens::NotNeeded))
|
||||||
.append(alloc.hardline())
|
.append(alloc.hardline())
|
||||||
.append(alloc.text("procedure = "))
|
.append(alloc.text("procedure = "))
|
||||||
.append(symbol_to_doc(alloc, self.name.name()))
|
.append(symbol_to_doc(alloc, self.name.name(), pretty))
|
||||||
.append(" (")
|
.append(" (")
|
||||||
.append(alloc.intersperse(args_doc, ", "))
|
.append(alloc.intersperse(args_doc, ", "))
|
||||||
.append("):")
|
.append("):")
|
||||||
.append(alloc.hardline())
|
.append(alloc.hardline())
|
||||||
.append(self.body.to_doc(alloc, interner).indent(4))
|
.append(self.body.to_doc(alloc, interner, pretty).indent(4))
|
||||||
} else {
|
} else {
|
||||||
alloc
|
alloc
|
||||||
.text("procedure ")
|
.text("procedure ")
|
||||||
.append(symbol_to_doc(alloc, self.name.name()))
|
.append(symbol_to_doc(alloc, self.name.name(), pretty))
|
||||||
.append(" (")
|
.append(" (")
|
||||||
.append(alloc.intersperse(args_doc, ", "))
|
.append(alloc.intersperse(args_doc, ", "))
|
||||||
.append("):")
|
.append("):")
|
||||||
.append(alloc.hardline())
|
.append(alloc.hardline())
|
||||||
.append(self.body.to_doc(alloc, interner).indent(4))
|
.append(self.body.to_doc(alloc, interner, pretty).indent(4))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn to_pretty<I>(&self, interner: &I, width: usize) -> String
|
pub fn to_pretty<I>(&self, interner: &I, width: usize, pretty: bool) -> String
|
||||||
where
|
where
|
||||||
I: Interner<'a, Layout<'a>>,
|
I: Interner<'a, Layout<'a>>,
|
||||||
{
|
{
|
||||||
let allocator = BoxAllocator;
|
let allocator = BoxAllocator;
|
||||||
let mut w = std::vec::Vec::new();
|
let mut w = std::vec::Vec::new();
|
||||||
self.to_doc::<_, (), _>(&allocator, interner, Parens::NotNeeded)
|
self.to_doc::<_, (), _>(&allocator, interner, pretty, Parens::NotNeeded)
|
||||||
.1
|
.1
|
||||||
.render(width, &mut w)
|
.render(width, &mut w)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
@ -1675,35 +1676,13 @@ pub enum BranchInfo<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> BranchInfo<'a> {
|
impl<'a> BranchInfo<'a> {
|
||||||
pub fn to_doc<'b, D, A>(&'b self, alloc: &'b D) -> DocBuilder<'b, D, A>
|
pub fn to_doc<'b, D, A>(&'b self, alloc: &'b D, _pretty: bool) -> DocBuilder<'b, D, A>
|
||||||
where
|
where
|
||||||
D: DocAllocator<'b, A>,
|
D: DocAllocator<'b, A>,
|
||||||
D::Doc: Clone,
|
D::Doc: Clone,
|
||||||
A: Clone,
|
A: Clone,
|
||||||
{
|
{
|
||||||
use BranchInfo::*;
|
alloc.text("")
|
||||||
|
|
||||||
match self {
|
|
||||||
Constructor {
|
|
||||||
tag_id,
|
|
||||||
scrutinee,
|
|
||||||
layout: _,
|
|
||||||
} if pretty_print_ir_symbols() => alloc
|
|
||||||
.hardline()
|
|
||||||
.append(" BranchInfo: { scrutinee: ")
|
|
||||||
.append(symbol_to_doc(alloc, *scrutinee))
|
|
||||||
.append(", tag_id: ")
|
|
||||||
.append(format!("{}", tag_id))
|
|
||||||
.append("} "),
|
|
||||||
|
|
||||||
_ => {
|
|
||||||
if pretty_print_ir_symbols() {
|
|
||||||
alloc.text(" <no branch info>")
|
|
||||||
} else {
|
|
||||||
alloc.text("")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1724,7 +1703,7 @@ pub enum ModifyRc {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ModifyRc {
|
impl ModifyRc {
|
||||||
pub fn to_doc<'a, D, A>(self, alloc: &'a D) -> DocBuilder<'a, D, A>
|
pub fn to_doc<'a, D, A>(self, alloc: &'a D, pretty: bool) -> DocBuilder<'a, D, A>
|
||||||
where
|
where
|
||||||
D: DocAllocator<'a, A>,
|
D: DocAllocator<'a, A>,
|
||||||
D::Doc: Clone,
|
D::Doc: Clone,
|
||||||
|
@ -1735,20 +1714,20 @@ impl ModifyRc {
|
||||||
match self {
|
match self {
|
||||||
Inc(symbol, 1) => alloc
|
Inc(symbol, 1) => alloc
|
||||||
.text("inc ")
|
.text("inc ")
|
||||||
.append(symbol_to_doc(alloc, symbol))
|
.append(symbol_to_doc(alloc, symbol, pretty))
|
||||||
.append(";"),
|
.append(";"),
|
||||||
Inc(symbol, n) => alloc
|
Inc(symbol, n) => alloc
|
||||||
.text("inc ")
|
.text("inc ")
|
||||||
.append(alloc.text(format!("{} ", n)))
|
.append(alloc.text(format!("{} ", n)))
|
||||||
.append(symbol_to_doc(alloc, symbol))
|
.append(symbol_to_doc(alloc, symbol, pretty))
|
||||||
.append(";"),
|
.append(";"),
|
||||||
Dec(symbol) => alloc
|
Dec(symbol) => alloc
|
||||||
.text("dec ")
|
.text("dec ")
|
||||||
.append(symbol_to_doc(alloc, symbol))
|
.append(symbol_to_doc(alloc, symbol, pretty))
|
||||||
.append(";"),
|
.append(";"),
|
||||||
DecRef(symbol) => alloc
|
DecRef(symbol) => alloc
|
||||||
.text("decref ")
|
.text("decref ")
|
||||||
.append(symbol_to_doc(alloc, symbol))
|
.append(symbol_to_doc(alloc, symbol, pretty))
|
||||||
.append(";"),
|
.append(";"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1808,7 +1787,7 @@ pub struct Call<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Call<'a> {
|
impl<'a> Call<'a> {
|
||||||
pub fn to_doc<'b, D, A>(&'b self, alloc: &'b D) -> DocBuilder<'b, D, A>
|
pub fn to_doc<'b, D, A>(&'b self, alloc: &'b D, pretty: bool) -> DocBuilder<'b, D, A>
|
||||||
where
|
where
|
||||||
D: DocAllocator<'b, A>,
|
D: DocAllocator<'b, A>,
|
||||||
D::Doc: Clone,
|
D::Doc: Clone,
|
||||||
|
@ -1822,19 +1801,19 @@ impl<'a> Call<'a> {
|
||||||
CallType::ByName { name, .. } => {
|
CallType::ByName { name, .. } => {
|
||||||
let it = std::iter::once(name.name())
|
let it = std::iter::once(name.name())
|
||||||
.chain(arguments.iter().copied())
|
.chain(arguments.iter().copied())
|
||||||
.map(|s| symbol_to_doc(alloc, s));
|
.map(|s| symbol_to_doc(alloc, s, pretty));
|
||||||
|
|
||||||
alloc.text("CallByName ").append(alloc.intersperse(it, " "))
|
alloc.text("CallByName ").append(alloc.intersperse(it, " "))
|
||||||
}
|
}
|
||||||
LowLevel { op: lowlevel, .. } => {
|
LowLevel { op: lowlevel, .. } => {
|
||||||
let it = arguments.iter().map(|s| symbol_to_doc(alloc, *s));
|
let it = arguments.iter().map(|s| symbol_to_doc(alloc, *s, pretty));
|
||||||
|
|
||||||
alloc
|
alloc
|
||||||
.text(format!("lowlevel {:?} ", lowlevel))
|
.text(format!("lowlevel {:?} ", lowlevel))
|
||||||
.append(alloc.intersperse(it, " "))
|
.append(alloc.intersperse(it, " "))
|
||||||
}
|
}
|
||||||
HigherOrder(higher_order) => {
|
HigherOrder(higher_order) => {
|
||||||
let it = arguments.iter().map(|s| symbol_to_doc(alloc, *s));
|
let it = arguments.iter().map(|s| symbol_to_doc(alloc, *s, pretty));
|
||||||
|
|
||||||
alloc
|
alloc
|
||||||
.text(format!("lowlevel {:?} ", higher_order.op))
|
.text(format!("lowlevel {:?} ", higher_order.op))
|
||||||
|
@ -1843,7 +1822,7 @@ impl<'a> Call<'a> {
|
||||||
Foreign {
|
Foreign {
|
||||||
ref foreign_symbol, ..
|
ref foreign_symbol, ..
|
||||||
} => {
|
} => {
|
||||||
let it = arguments.iter().map(|s| symbol_to_doc(alloc, *s));
|
let it = arguments.iter().map(|s| symbol_to_doc(alloc, *s, pretty));
|
||||||
|
|
||||||
alloc
|
alloc
|
||||||
.text(format!("foreign {:?} ", foreign_symbol.as_str()))
|
.text(format!("foreign {:?} ", foreign_symbol.as_str()))
|
||||||
|
@ -2034,10 +2013,10 @@ impl<'a> Literal<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn symbol_to_doc_string(symbol: Symbol) -> String {
|
pub(crate) fn symbol_to_doc_string(symbol: Symbol, force_pretty: bool) -> String {
|
||||||
use roc_module::ident::ModuleName;
|
use roc_module::ident::ModuleName;
|
||||||
|
|
||||||
if pretty_print_ir_symbols() {
|
if pretty_print_ir_symbols() || force_pretty {
|
||||||
format!("{:?}", symbol)
|
format!("{:?}", symbol)
|
||||||
} else {
|
} else {
|
||||||
let text = format!("{}", symbol);
|
let text = format!("{}", symbol);
|
||||||
|
@ -2051,26 +2030,30 @@ pub(crate) fn symbol_to_doc_string(symbol: Symbol) -> String {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn symbol_to_doc<'b, D, A>(alloc: &'b D, symbol: Symbol) -> DocBuilder<'b, D, A>
|
fn symbol_to_doc<'b, D, A>(alloc: &'b D, symbol: Symbol, force_pretty: bool) -> DocBuilder<'b, D, A>
|
||||||
where
|
where
|
||||||
D: DocAllocator<'b, A>,
|
D: DocAllocator<'b, A>,
|
||||||
D::Doc: Clone,
|
D::Doc: Clone,
|
||||||
A: Clone,
|
A: Clone,
|
||||||
{
|
{
|
||||||
alloc.text(symbol_to_doc_string(symbol))
|
alloc.text(symbol_to_doc_string(symbol, force_pretty))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn join_point_to_doc<'b, D, A>(alloc: &'b D, symbol: JoinPointId) -> DocBuilder<'b, D, A>
|
fn join_point_to_doc<'b, D, A>(
|
||||||
|
alloc: &'b D,
|
||||||
|
symbol: JoinPointId,
|
||||||
|
pretty: bool,
|
||||||
|
) -> DocBuilder<'b, D, A>
|
||||||
where
|
where
|
||||||
D: DocAllocator<'b, A>,
|
D: DocAllocator<'b, A>,
|
||||||
D::Doc: Clone,
|
D::Doc: Clone,
|
||||||
A: Clone,
|
A: Clone,
|
||||||
{
|
{
|
||||||
symbol_to_doc(alloc, symbol.0)
|
symbol_to_doc(alloc, symbol.0, pretty)
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Expr<'a> {
|
impl<'a> Expr<'a> {
|
||||||
pub fn to_doc<'b, D, A>(&'b self, alloc: &'b D) -> DocBuilder<'b, D, A>
|
pub fn to_doc<'b, D, A>(&'b self, alloc: &'b D, pretty: bool) -> DocBuilder<'b, D, A>
|
||||||
where
|
where
|
||||||
D: DocAllocator<'b, A>,
|
D: DocAllocator<'b, A>,
|
||||||
D::Doc: Clone,
|
D::Doc: Clone,
|
||||||
|
@ -2081,7 +2064,7 @@ impl<'a> Expr<'a> {
|
||||||
match self {
|
match self {
|
||||||
Literal(lit) => lit.to_doc(alloc),
|
Literal(lit) => lit.to_doc(alloc),
|
||||||
|
|
||||||
Call(call) => call.to_doc(alloc),
|
Call(call) => call.to_doc(alloc, pretty),
|
||||||
|
|
||||||
Tag {
|
Tag {
|
||||||
tag_id, arguments, ..
|
tag_id, arguments, ..
|
||||||
|
@ -2091,7 +2074,7 @@ impl<'a> Expr<'a> {
|
||||||
.append(alloc.text(tag_id.to_string()))
|
.append(alloc.text(tag_id.to_string()))
|
||||||
.append(")");
|
.append(")");
|
||||||
|
|
||||||
let it = arguments.iter().map(|s| symbol_to_doc(alloc, *s));
|
let it = arguments.iter().map(|s| symbol_to_doc(alloc, *s, pretty));
|
||||||
|
|
||||||
doc_tag
|
doc_tag
|
||||||
.append(alloc.space())
|
.append(alloc.space())
|
||||||
|
@ -2109,11 +2092,11 @@ impl<'a> Expr<'a> {
|
||||||
.append(alloc.text(tag_id.to_string()))
|
.append(alloc.text(tag_id.to_string()))
|
||||||
.append(")");
|
.append(")");
|
||||||
|
|
||||||
let it = arguments.iter().map(|s| symbol_to_doc(alloc, *s));
|
let it = arguments.iter().map(|s| symbol_to_doc(alloc, *s, pretty));
|
||||||
|
|
||||||
alloc
|
alloc
|
||||||
.text("Reuse ")
|
.text("Reuse ")
|
||||||
.append(symbol_to_doc(alloc, *symbol))
|
.append(symbol_to_doc(alloc, *symbol, pretty))
|
||||||
.append(alloc.space())
|
.append(alloc.space())
|
||||||
.append(format!("{:?}", update_mode))
|
.append(format!("{:?}", update_mode))
|
||||||
.append(alloc.space())
|
.append(alloc.space())
|
||||||
|
@ -2130,7 +2113,7 @@ impl<'a> Expr<'a> {
|
||||||
)),
|
)),
|
||||||
|
|
||||||
Struct(args) => {
|
Struct(args) => {
|
||||||
let it = args.iter().map(|s| symbol_to_doc(alloc, *s));
|
let it = args.iter().map(|s| symbol_to_doc(alloc, *s, pretty));
|
||||||
|
|
||||||
alloc
|
alloc
|
||||||
.text("Struct {")
|
.text("Struct {")
|
||||||
|
@ -2140,7 +2123,7 @@ impl<'a> Expr<'a> {
|
||||||
Array { elems, .. } => {
|
Array { elems, .. } => {
|
||||||
let it = elems.iter().map(|e| match e {
|
let it = elems.iter().map(|e| match e {
|
||||||
ListLiteralElement::Literal(l) => l.to_doc(alloc),
|
ListLiteralElement::Literal(l) => l.to_doc(alloc),
|
||||||
ListLiteralElement::Symbol(s) => symbol_to_doc(alloc, *s),
|
ListLiteralElement::Symbol(s) => symbol_to_doc(alloc, *s, pretty),
|
||||||
});
|
});
|
||||||
|
|
||||||
alloc
|
alloc
|
||||||
|
@ -2154,17 +2137,21 @@ impl<'a> Expr<'a> {
|
||||||
index, structure, ..
|
index, structure, ..
|
||||||
} => alloc
|
} => alloc
|
||||||
.text(format!("StructAtIndex {} ", index))
|
.text(format!("StructAtIndex {} ", index))
|
||||||
.append(symbol_to_doc(alloc, *structure)),
|
.append(symbol_to_doc(alloc, *structure, pretty)),
|
||||||
|
|
||||||
RuntimeErrorFunction(s) => alloc.text(format!("ErrorFunction {}", s)),
|
RuntimeErrorFunction(s) => alloc.text(format!("ErrorFunction {}", s)),
|
||||||
|
|
||||||
GetTagId { structure, .. } => alloc
|
GetTagId { structure, .. } => alloc
|
||||||
.text("GetTagId ")
|
.text("GetTagId ")
|
||||||
.append(symbol_to_doc(alloc, *structure)),
|
.append(symbol_to_doc(alloc, *structure, pretty)),
|
||||||
|
|
||||||
ExprBox { symbol, .. } => alloc.text("Box ").append(symbol_to_doc(alloc, *symbol)),
|
ExprBox { symbol, .. } => alloc
|
||||||
|
.text("Box ")
|
||||||
|
.append(symbol_to_doc(alloc, *symbol, pretty)),
|
||||||
|
|
||||||
ExprUnbox { symbol, .. } => alloc.text("Unbox ").append(symbol_to_doc(alloc, *symbol)),
|
ExprUnbox { symbol, .. } => alloc
|
||||||
|
.text("Unbox ")
|
||||||
|
.append(symbol_to_doc(alloc, *symbol, pretty)),
|
||||||
|
|
||||||
UnionAtIndex {
|
UnionAtIndex {
|
||||||
tag_id,
|
tag_id,
|
||||||
|
@ -2173,14 +2160,14 @@ impl<'a> Expr<'a> {
|
||||||
..
|
..
|
||||||
} => alloc
|
} => alloc
|
||||||
.text(format!("UnionAtIndex (Id {}) (Index {}) ", tag_id, index))
|
.text(format!("UnionAtIndex (Id {}) (Index {}) ", tag_id, index))
|
||||||
.append(symbol_to_doc(alloc, *structure)),
|
.append(symbol_to_doc(alloc, *structure, pretty)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn to_pretty(&self, width: usize) -> String {
|
pub fn to_pretty(&self, width: usize, pretty: bool) -> String {
|
||||||
let allocator = BoxAllocator;
|
let allocator = BoxAllocator;
|
||||||
let mut w = std::vec::Vec::new();
|
let mut w = std::vec::Vec::new();
|
||||||
self.to_doc::<_, ()>(&allocator)
|
self.to_doc::<_, ()>(&allocator, pretty)
|
||||||
.1
|
.1
|
||||||
.render(width, &mut w)
|
.render(width, &mut w)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
@ -2200,7 +2187,12 @@ impl<'a> Stmt<'a> {
|
||||||
from_can(env, var, can_expr, procs, layout_cache)
|
from_can(env, var, can_expr, procs, layout_cache)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn to_doc<'b, D, A, I>(&'b self, alloc: &'b D, interner: &I) -> DocBuilder<'b, D, A>
|
pub fn to_doc<'b, D, A, I>(
|
||||||
|
&'b self,
|
||||||
|
alloc: &'b D,
|
||||||
|
interner: &I,
|
||||||
|
pretty: bool,
|
||||||
|
) -> DocBuilder<'b, D, A>
|
||||||
where
|
where
|
||||||
D: DocAllocator<'b, A>,
|
D: DocAllocator<'b, A>,
|
||||||
D::Doc: Clone,
|
D::Doc: Clone,
|
||||||
|
@ -2212,19 +2204,19 @@ impl<'a> Stmt<'a> {
|
||||||
match self {
|
match self {
|
||||||
Let(symbol, expr, layout, cont) => alloc
|
Let(symbol, expr, layout, cont) => alloc
|
||||||
.text("let ")
|
.text("let ")
|
||||||
.append(symbol_to_doc(alloc, *symbol))
|
.append(symbol_to_doc(alloc, *symbol, pretty))
|
||||||
.append(" : ")
|
.append(" : ")
|
||||||
.append(layout.to_doc(alloc, interner, Parens::NotNeeded))
|
.append(layout.to_doc(alloc, interner, Parens::NotNeeded))
|
||||||
.append(" = ")
|
.append(" = ")
|
||||||
.append(expr.to_doc(alloc))
|
.append(expr.to_doc(alloc, pretty))
|
||||||
.append(";")
|
.append(";")
|
||||||
.append(alloc.hardline())
|
.append(alloc.hardline())
|
||||||
.append(cont.to_doc(alloc, interner)),
|
.append(cont.to_doc(alloc, interner, pretty)),
|
||||||
|
|
||||||
Refcounting(modify, cont) => modify
|
Refcounting(modify, cont) => modify
|
||||||
.to_doc(alloc)
|
.to_doc(alloc, pretty)
|
||||||
.append(alloc.hardline())
|
.append(alloc.hardline())
|
||||||
.append(cont.to_doc(alloc, interner)),
|
.append(cont.to_doc(alloc, interner, pretty)),
|
||||||
|
|
||||||
Expect {
|
Expect {
|
||||||
condition,
|
condition,
|
||||||
|
@ -2232,10 +2224,10 @@ impl<'a> Stmt<'a> {
|
||||||
..
|
..
|
||||||
} => alloc
|
} => alloc
|
||||||
.text("expect ")
|
.text("expect ")
|
||||||
.append(symbol_to_doc(alloc, *condition))
|
.append(symbol_to_doc(alloc, *condition, pretty))
|
||||||
.append(";")
|
.append(";")
|
||||||
.append(alloc.hardline())
|
.append(alloc.hardline())
|
||||||
.append(remainder.to_doc(alloc, interner)),
|
.append(remainder.to_doc(alloc, interner, pretty)),
|
||||||
|
|
||||||
ExpectFx {
|
ExpectFx {
|
||||||
condition,
|
condition,
|
||||||
|
@ -2243,14 +2235,14 @@ impl<'a> Stmt<'a> {
|
||||||
..
|
..
|
||||||
} => alloc
|
} => alloc
|
||||||
.text("expect-fx ")
|
.text("expect-fx ")
|
||||||
.append(symbol_to_doc(alloc, *condition))
|
.append(symbol_to_doc(alloc, *condition, pretty))
|
||||||
.append(";")
|
.append(";")
|
||||||
.append(alloc.hardline())
|
.append(alloc.hardline())
|
||||||
.append(remainder.to_doc(alloc, interner)),
|
.append(remainder.to_doc(alloc, interner, pretty)),
|
||||||
|
|
||||||
Ret(symbol) => alloc
|
Ret(symbol) => alloc
|
||||||
.text("ret ")
|
.text("ret ")
|
||||||
.append(symbol_to_doc(alloc, *symbol))
|
.append(symbol_to_doc(alloc, *symbol, pretty))
|
||||||
.append(";"),
|
.append(";"),
|
||||||
|
|
||||||
Switch {
|
Switch {
|
||||||
|
@ -2264,23 +2256,23 @@ impl<'a> Stmt<'a> {
|
||||||
let fail = default_branch.1;
|
let fail = default_branch.1;
|
||||||
alloc
|
alloc
|
||||||
.text("if ")
|
.text("if ")
|
||||||
.append(symbol_to_doc(alloc, *cond_symbol))
|
.append(symbol_to_doc(alloc, *cond_symbol, pretty))
|
||||||
.append(" then")
|
.append(" then")
|
||||||
.append(info.to_doc(alloc))
|
.append(info.to_doc(alloc, pretty))
|
||||||
.append(alloc.hardline())
|
.append(alloc.hardline())
|
||||||
.append(pass.to_doc(alloc, interner).indent(4))
|
.append(pass.to_doc(alloc, interner, pretty).indent(4))
|
||||||
.append(alloc.hardline())
|
.append(alloc.hardline())
|
||||||
.append(alloc.text("else"))
|
.append(alloc.text("else"))
|
||||||
.append(default_branch.0.to_doc(alloc))
|
.append(default_branch.0.to_doc(alloc, pretty))
|
||||||
.append(alloc.hardline())
|
.append(alloc.hardline())
|
||||||
.append(fail.to_doc(alloc, interner).indent(4))
|
.append(fail.to_doc(alloc, interner, pretty).indent(4))
|
||||||
}
|
}
|
||||||
|
|
||||||
_ => {
|
_ => {
|
||||||
let default_doc = alloc
|
let default_doc = alloc
|
||||||
.text("default:")
|
.text("default:")
|
||||||
.append(alloc.hardline())
|
.append(alloc.hardline())
|
||||||
.append(default_branch.1.to_doc(alloc, interner).indent(4))
|
.append(default_branch.1.to_doc(alloc, interner, pretty).indent(4))
|
||||||
.indent(4);
|
.indent(4);
|
||||||
|
|
||||||
let branches_docs = branches
|
let branches_docs = branches
|
||||||
|
@ -2289,14 +2281,14 @@ impl<'a> Stmt<'a> {
|
||||||
alloc
|
alloc
|
||||||
.text(format!("case {}:", tag))
|
.text(format!("case {}:", tag))
|
||||||
.append(alloc.hardline())
|
.append(alloc.hardline())
|
||||||
.append(expr.to_doc(alloc, interner).indent(4))
|
.append(expr.to_doc(alloc, interner, pretty).indent(4))
|
||||||
.indent(4)
|
.indent(4)
|
||||||
})
|
})
|
||||||
.chain(std::iter::once(default_doc));
|
.chain(std::iter::once(default_doc));
|
||||||
//
|
//
|
||||||
alloc
|
alloc
|
||||||
.text("switch ")
|
.text("switch ")
|
||||||
.append(symbol_to_doc(alloc, *cond_symbol))
|
.append(symbol_to_doc(alloc, *cond_symbol, pretty))
|
||||||
.append(":")
|
.append(":")
|
||||||
.append(alloc.hardline())
|
.append(alloc.hardline())
|
||||||
.append(alloc.intersperse(
|
.append(alloc.intersperse(
|
||||||
|
@ -2308,7 +2300,9 @@ impl<'a> Stmt<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Crash(s, _src) => alloc.text("Crash ").append(symbol_to_doc(alloc, *s)),
|
Crash(s, _src) => alloc
|
||||||
|
.text("Crash ")
|
||||||
|
.append(symbol_to_doc(alloc, *s, pretty)),
|
||||||
|
|
||||||
Join {
|
Join {
|
||||||
id,
|
id,
|
||||||
|
@ -2316,29 +2310,31 @@ impl<'a> Stmt<'a> {
|
||||||
body: continuation,
|
body: continuation,
|
||||||
remainder,
|
remainder,
|
||||||
} => {
|
} => {
|
||||||
let it = parameters.iter().map(|p| symbol_to_doc(alloc, p.symbol));
|
let it = parameters
|
||||||
|
.iter()
|
||||||
|
.map(|p| symbol_to_doc(alloc, p.symbol, pretty));
|
||||||
|
|
||||||
alloc.intersperse(
|
alloc.intersperse(
|
||||||
vec![
|
vec![
|
||||||
alloc
|
alloc
|
||||||
.text("joinpoint ")
|
.text("joinpoint ")
|
||||||
.append(join_point_to_doc(alloc, *id))
|
.append(join_point_to_doc(alloc, *id, pretty))
|
||||||
.append(" ".repeat(parameters.len().min(1)))
|
.append(" ".repeat(parameters.len().min(1)))
|
||||||
.append(alloc.intersperse(it, alloc.space()))
|
.append(alloc.intersperse(it, alloc.space()))
|
||||||
.append(":"),
|
.append(":"),
|
||||||
continuation.to_doc(alloc, interner).indent(4),
|
continuation.to_doc(alloc, interner, pretty).indent(4),
|
||||||
alloc.text("in"),
|
alloc.text("in"),
|
||||||
remainder.to_doc(alloc, interner),
|
remainder.to_doc(alloc, interner, pretty),
|
||||||
],
|
],
|
||||||
alloc.hardline(),
|
alloc.hardline(),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
Jump(id, arguments) => {
|
Jump(id, arguments) => {
|
||||||
let it = arguments.iter().map(|s| symbol_to_doc(alloc, *s));
|
let it = arguments.iter().map(|s| symbol_to_doc(alloc, *s, pretty));
|
||||||
|
|
||||||
alloc
|
alloc
|
||||||
.text("jump ")
|
.text("jump ")
|
||||||
.append(join_point_to_doc(alloc, *id))
|
.append(join_point_to_doc(alloc, *id, pretty))
|
||||||
.append(" ".repeat(arguments.len().min(1)))
|
.append(" ".repeat(arguments.len().min(1)))
|
||||||
.append(alloc.intersperse(it, alloc.space()))
|
.append(alloc.intersperse(it, alloc.space()))
|
||||||
.append(";")
|
.append(";")
|
||||||
|
@ -2346,13 +2342,13 @@ impl<'a> Stmt<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn to_pretty<I>(&self, interner: &I, width: usize) -> String
|
pub fn to_pretty<I>(&self, interner: &I, width: usize, pretty: bool) -> String
|
||||||
where
|
where
|
||||||
I: Interner<'a, Layout<'a>>,
|
I: Interner<'a, Layout<'a>>,
|
||||||
{
|
{
|
||||||
let allocator = BoxAllocator;
|
let allocator = BoxAllocator;
|
||||||
let mut w = std::vec::Vec::new();
|
let mut w = std::vec::Vec::new();
|
||||||
self.to_doc::<_, (), _>(&allocator, interner)
|
self.to_doc::<_, (), _>(&allocator, interner, pretty)
|
||||||
.1
|
.1
|
||||||
.render(width, &mut w)
|
.render(width, &mut w)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
|
@ -1154,7 +1154,7 @@ struct SetElement<'a> {
|
||||||
|
|
||||||
impl std::fmt::Debug for SetElement<'_> {
|
impl std::fmt::Debug for SetElement<'_> {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
let symbol_string = crate::ir::symbol_to_doc_string(self.symbol);
|
let symbol_string = crate::ir::symbol_to_doc_string(self.symbol, false);
|
||||||
|
|
||||||
write!(f, "( {}, {:?})", symbol_string, self.layout)
|
write!(f, "( {}, {:?})", symbol_string, self.layout)
|
||||||
}
|
}
|
||||||
|
|
|
@ -180,7 +180,7 @@ fn verify_procedures<'a>(
|
||||||
|
|
||||||
let mut procs_string = procedures
|
let mut procs_string = procedures
|
||||||
.values()
|
.values()
|
||||||
.map(|proc| proc.to_pretty(&interner, 200))
|
.map(|proc| proc.to_pretty(&interner, 200, false))
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
let main_fn = procs_string.swap_remove(index);
|
let main_fn = procs_string.swap_remove(index);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue