use std::collections::HashSet;
use crate::ast::{BinaryOp, Expr, ExprKind, LogicalOp, Pattern, Stmt, StmtKind, UnaryOp};
#[derive(Debug, Clone)]
pub struct Comment {
pub line: usize,
pub own_line: bool,
pub text: String,
}
#[derive(Debug, Clone, Default)]
pub struct Trivia {
pub comments: Vec<Comment>,
pub blank_before: HashSet<usize>,
}
pub fn format_program(program: &[Stmt], trivia: &Trivia) -> String {
let mut printer = Printer {
out: String::new(),
comments: &trivia.comments,
idx: 0,
blank_before: &trivia.blank_before,
blanked: HashSet::new(),
last_blank: false,
};
printer.print_stmts(program, 0);
printer.flush_remaining(0);
printer.out
}
struct Printer<'a> {
out: String,
comments: &'a [Comment],
idx: usize,
blank_before: &'a HashSet<usize>,
blanked: HashSet<usize>,
last_blank: bool,
}
impl Printer<'_> {
fn print_stmts(&mut self, stmts: &[Stmt], indent: usize) {
for stmt in stmts {
self.emit_leading(stmt.line, indent);
self.print_stmt(stmt, indent);
}
}
fn print_stmt(&mut self, stmt: &Stmt, indent: usize) {
self.maybe_blank(stmt.line);
match &stmt.kind {
StmtKind::If {
condition,
then_branch,
else_branch,
} => {
let header = format!("if {} {{", fmt_expr(condition));
let header = self.attach_trailing(stmt.line, header);
self.push_line(indent, header);
self.print_stmts(then_branch, indent + 1);
self.print_else(else_branch, indent);
}
StmtKind::While { condition, body } => {
let header = format!("while {} {{", fmt_expr(condition));
let header = self.attach_trailing(stmt.line, header);
self.push_line(indent, header);
self.print_stmts(body, indent + 1);
self.push_line(indent, "}".to_string());
}
StmtKind::For {
name,
value,
iterable,
body,
} => {
let header = format!(
"for {} in {} {{",
fmt_bound(name, value),
fmt_expr(iterable)
);
let header = self.attach_trailing(stmt.line, header);
self.push_line(indent, header);
self.print_stmts(body, indent + 1);
self.push_line(indent, "}".to_string());
}
StmtKind::Function { name, params, body } => {
let header = format!("fn {name}({}) {{", params.join(", "));
let header = self.attach_trailing(stmt.line, header);
self.push_line(indent, header);
self.print_stmts(body, indent + 1);
self.push_line(indent, "}".to_string());
}
_ => {
let text = stmt_inline(stmt);
let text = self.attach_trailing(stmt.line, text);
self.push_line(indent, text);
}
}
}
fn print_else(&mut self, else_branch: &Option<Vec<Stmt>>, indent: usize) {
match else_branch {
None => self.push_line(indent, "}".to_string()),
Some(branch) => match single_if(branch) {
Some(nested) => {
if let StmtKind::If {
condition,
then_branch,
else_branch,
} = &nested.kind
{
let header = format!("}} else if {} {{", fmt_expr(condition));
let header = self.attach_trailing(nested.line, header);
self.push_line(indent, header);
self.print_stmts(then_branch, indent + 1);
self.print_else(else_branch, indent);
}
}
None => {
self.push_line(indent, "} else {".to_string());
self.print_stmts(branch, indent + 1);
self.push_line(indent, "}".to_string());
}
},
}
}
fn emit_leading(&mut self, before_line: usize, indent: usize) {
while self.idx < self.comments.len() {
let comment = &self.comments[self.idx];
if !comment.own_line || comment.line >= before_line {
break;
}
let line = comment.line;
let text = render_comment(&comment.text);
self.idx += 1;
self.maybe_blank(line);
self.push_line(indent, text);
}
}
fn attach_trailing(&mut self, line: usize, text: String) -> String {
if let Some(comment) = self.comments.get(self.idx) {
if !comment.own_line && comment.line == line {
let rendered = render_comment(&comment.text);
self.idx += 1;
return format!("{text} {rendered}");
}
}
text
}
fn flush_remaining(&mut self, indent: usize) {
while self.idx < self.comments.len() {
let line = self.comments[self.idx].line;
let text = render_comment(&self.comments[self.idx].text);
self.idx += 1;
self.maybe_blank(line);
self.push_line(indent, text);
}
}
fn maybe_blank(&mut self, line: usize) {
if self.blank_before.contains(&line) && self.blanked.insert(line) {
self.push_blank();
}
}
fn push_blank(&mut self) {
if self.out.is_empty() || self.last_blank {
return;
}
self.out.push('\n');
self.last_blank = true;
}
fn push_line(&mut self, indent: usize, text: String) {
for _ in 0..indent {
self.out.push_str(" ");
}
self.out.push_str(&text);
self.out.push('\n');
self.last_blank = false;
}
}
fn single_if(branch: &[Stmt]) -> Option<&Stmt> {
match branch {
[only] if matches!(only.kind, StmtKind::If { .. }) => Some(only),
_ => None,
}
}
fn render_comment(text: &str) -> String {
let content = text.trim();
if content.is_empty() {
"//".to_string()
} else {
format!("// {content}")
}
}
fn stmt_inline(stmt: &Stmt) -> String {
match &stmt.kind {
StmtKind::Import {
spec,
alias,
column: _,
} => format!("import {} as {alias}", fmt_string(spec)),
StmtKind::Let { pattern, value } => {
format!("let {} = {}", fmt_pattern(pattern), fmt_expr(value))
}
StmtKind::Assign { target, value } => {
format!("{} = {}", fmt_expr(target), fmt_expr(value))
}
StmtKind::CompoundAssign { target, op, value } => {
format!(
"{} {}= {}",
fmt_expr(target),
binary_symbol(*op),
fmt_expr(value)
)
}
StmtKind::Expr(expr) => fmt_expr(expr),
StmtKind::Return(None) => "return".to_string(),
StmtKind::Return(Some(expr)) => format!("return {}", fmt_expr(expr)),
StmtKind::Break => "break".to_string(),
StmtKind::Continue => "continue".to_string(),
StmtKind::If {
condition,
then_branch,
else_branch,
} => {
let mut text = format!("if {} {}", fmt_expr(condition), block_inline(then_branch));
text.push_str(&else_inline(else_branch));
text
}
StmtKind::While { condition, body } => {
format!("while {} {}", fmt_expr(condition), block_inline(body))
}
StmtKind::For {
name,
value,
iterable,
body,
} => format!(
"for {} in {} {}",
fmt_bound(name, value),
fmt_expr(iterable),
block_inline(body)
),
StmtKind::Function { name, params, body } => {
format!("fn {name}({}) {}", params.join(", "), block_inline(body))
}
}
}
fn else_inline(else_branch: &Option<Vec<Stmt>>) -> String {
match else_branch {
None => String::new(),
Some(branch) => match single_if(branch) {
Some(nested) => format!(" else {}", stmt_inline(nested)),
None => format!(" else {}", block_inline(branch)),
},
}
}
fn fmt_pattern(pattern: &Pattern) -> String {
match pattern {
Pattern::Name(name) => name.clone(),
Pattern::Array(items) => {
let parts: Vec<String> = items.iter().map(fmt_pattern).collect();
format!("[{}]", parts.join(", "))
}
}
}
fn fmt_bound(name: &Pattern, value: &Option<Pattern>) -> String {
match value {
Some(value) => format!("{}, {}", fmt_pattern(name), fmt_pattern(value)),
None => fmt_pattern(name),
}
}
fn block_inline(stmts: &[Stmt]) -> String {
if stmts.is_empty() {
"{}".to_string()
} else {
let parts: Vec<String> = stmts.iter().map(stmt_inline).collect();
format!("{{ {} }}", parts.join("; "))
}
}
fn precedence(expr: &Expr) -> u8 {
match &expr.kind {
ExprKind::FString(_) => u8::MAX,
ExprKind::Logical {
op: LogicalOp::Or, ..
} => 1,
ExprKind::Logical {
op: LogicalOp::And, ..
} => 2,
ExprKind::Binary { op, .. } => match op {
BinaryOp::Equal | BinaryOp::NotEqual => 3,
BinaryOp::Less | BinaryOp::Greater | BinaryOp::LessEqual | BinaryOp::GreaterEqual => 4,
BinaryOp::Add | BinaryOp::Subtract => 5,
BinaryOp::Multiply | BinaryOp::Divide | BinaryOp::Modulo => 6,
},
ExprKind::Try(_) => 0,
ExprKind::Unary { .. } => 7,
ExprKind::Call { .. } | ExprKind::Index { .. } | ExprKind::Field { .. } => 8,
_ => 9,
}
}
fn fmt_operand(expr: &Expr, min: u8) -> String {
let text = fmt_expr(expr);
if precedence(expr) < min {
format!("({text})")
} else {
text
}
}
fn fmt_expr(expr: &Expr) -> String {
match &expr.kind {
ExprKind::FString(parts) => {
let mut out = String::from("f\"");
for part in parts {
match part {
crate::token::FStringPart::Text(text) => {
let escaped = fmt_string(text);
let inner = &escaped[1..escaped.len() - 1];
out.push_str(&inner.replace('{', "{{").replace('}', "}}"));
}
crate::token::FStringPart::Name { name, .. } => {
out.push('{');
out.push_str(name);
out.push('}');
}
}
}
out.push('"');
out
}
ExprKind::Int(n) => n.to_string(),
ExprKind::Float(f) => fmt_float(*f),
ExprKind::Str(s) => fmt_string(s),
ExprKind::Bool(b) => b.to_string(),
ExprKind::Nil => "nil".to_string(),
ExprKind::Identifier(name) => name.clone(),
ExprKind::Array(elements) => {
let parts: Vec<String> = elements.iter().map(fmt_expr).collect();
format!("[{}]", parts.join(", "))
}
ExprKind::Map(entries) => {
if entries.is_empty() {
"{}".to_string()
} else {
let parts: Vec<String> = entries
.iter()
.map(|(key, value)| format!("{}: {}", fmt_expr(key), fmt_expr(value)))
.collect();
format!("{{{}}}", parts.join(", "))
}
}
ExprKind::Field { target, name } => {
format!("{}.{name}", fmt_operand(target, 8))
}
ExprKind::Index { target, index } => {
format!("{}[{}]", fmt_operand(target, 8), fmt_expr(index))
}
ExprKind::If {
condition,
then_value,
else_value,
} => {
let otherwise = match &else_value.kind {
ExprKind::If { .. } => fmt_expr(else_value),
_ => format!("{{ {} }}", fmt_expr(else_value)),
};
format!(
"if {} {{ {} }} else {otherwise}",
fmt_expr(condition),
fmt_expr(then_value)
)
}
ExprKind::Try(inner) => format!("try {}", fmt_expr(inner)),
ExprKind::Unary { op, operand } => {
let symbol = match op {
UnaryOp::Negate => "-",
UnaryOp::Not => "!",
};
format!("{symbol}{}", fmt_operand(operand, 7))
}
ExprKind::Binary { op, left, right } => {
let power = precedence(expr);
format!(
"{} {} {}",
fmt_operand(left, power),
binary_symbol(*op),
fmt_operand(right, power + 1)
)
}
ExprKind::Logical { op, left, right } => {
let power = precedence(expr);
let symbol = match op {
LogicalOp::And => "&&",
LogicalOp::Or => "||",
};
format!(
"{} {symbol} {}",
fmt_operand(left, power),
fmt_operand(right, power + 1)
)
}
ExprKind::Call { callee, arguments } => {
let args: Vec<String> = arguments.iter().map(fmt_expr).collect();
format!("{}({})", fmt_operand(callee, 8), args.join(", "))
}
ExprKind::Function { params, body } => {
format!("fn({}) {}", params.join(", "), block_inline(body))
}
}
}
fn binary_symbol(op: BinaryOp) -> &'static str {
match op {
BinaryOp::Add => "+",
BinaryOp::Subtract => "-",
BinaryOp::Multiply => "*",
BinaryOp::Divide => "/",
BinaryOp::Modulo => "%",
BinaryOp::Equal => "==",
BinaryOp::NotEqual => "!=",
BinaryOp::Less => "<",
BinaryOp::Greater => ">",
BinaryOp::LessEqual => "<=",
BinaryOp::GreaterEqual => ">=",
}
}
fn fmt_float(f: f64) -> String {
let text = f.to_string();
if text.contains(['.', 'e', 'E']) {
text
} else {
format!("{text}.0")
}
}
fn fmt_string(s: &str) -> String {
crate::value::quoted_string(s)
}
#[cfg(test)]
mod tests {
use super::*;
fn program(source: &str) -> Vec<Stmt> {
crate::parse_program(source).expect("source should parse")
}
fn fmt(source: &str) -> String {
format_program(&program(source), &Trivia::default())
}
#[test]
fn try_reprints_with_the_parentheses_it_needs() {
assert_eq!(fmt("let r = try 1/0\n"), "let r = try 1 / 0\n");
assert_eq!(fmt("let n = (try f()) + 1\n"), "let n = (try f()) + 1\n");
assert_eq!(fmt("let b = (try f()) || 2\n"), "let b = (try f()) || 2\n");
let once = fmt("let n = (try f()) + 1\n");
assert_eq!(fmt(&once), once);
}
#[test]
fn a_control_character_is_spelled_rather_than_written() {
assert_eq!(fmt("let s = \"\\u{7}\"\n"), "let s = \"\\u{7}\"\n");
assert_eq!(fmt("let s = \"\\u{1B}\"\n"), "let s = \"\\u{1B}\"\n");
assert_eq!(fmt("let s = \"\\0\"\n"), "let s = \"\\0\"\n");
let once = fmt("let s = \"\\u{1B}\"\n");
assert_eq!(fmt(&once), once);
}
#[test]
fn a_character_that_is_not_ascii_is_written_as_itself() {
assert_eq!(fmt("let e = \"\\u{1F600}\"\n"), "let e = \"\u{1F600}\"\n");
let once = fmt("let e = \"\\u{1F600}\"\n");
assert_eq!(fmt(&once), once);
assert_eq!(fmt("let s = \"a\\nb\"\n"), "let s = \"a\\nb\"\n");
}
#[test]
fn prints_arithmetic_with_minimal_parentheses() {
assert_eq!(fmt("1 + 2 * 3"), "1 + 2 * 3\n");
assert_eq!(fmt("(1 + 2) * 3"), "(1 + 2) * 3\n");
}
#[test]
fn keeps_parentheses_that_matter_for_associativity() {
assert_eq!(fmt("1 - (2 - 3)"), "1 - (2 - 3)\n");
assert_eq!(fmt("1 - 2 - 3"), "1 - 2 - 3\n");
}
#[test]
fn parenthesizes_lower_precedence_operands() {
assert_eq!(fmt("-(a + b)"), "-(a + b)\n");
assert_eq!(fmt("!(a && b)"), "!(a && b)\n");
assert_eq!(fmt("(a + b)[0]"), "(a + b)[0]\n");
}
#[test]
fn field_access_keeps_only_the_parentheses_it_needs() {
assert_eq!(fmt("(a.b).c"), "a.b.c\n");
assert_eq!(fmt("a.b()"), "a.b()\n");
assert_eq!(fmt("a[0].b"), "a[0].b\n");
assert_eq!(fmt("a.b[0]"), "a.b[0]\n");
assert_eq!(fmt("(a + b).c"), "(a + b).c\n");
assert_eq!(fmt("(-a).b"), "(-a).b\n");
assert_eq!(fmt("-a.b"), "-a.b\n");
}
#[test]
fn an_import_keeps_its_quotes_and_one_space_either_side() {
assert_eq!(
fmt("import \"./a.miru\" as a"),
"import \"./a.miru\" as a\n"
);
}
#[test]
fn normalizes_spacing_and_literals() {
assert_eq!(fmt("let x=[1,2 , 3]"), "let x = [1, 2, 3]\n");
assert_eq!(
fmt("let m = {\"b\":2,\"a\":1}"),
"let m = {\"b\": 2, \"a\": 1}\n"
);
assert_eq!(fmt("print( 1.0 , \"hi\" )"), "print(1.0, \"hi\")\n");
}
#[test]
fn indents_blocks_with_two_spaces() {
let source = "fn add(a,b){return a+b}";
assert_eq!(fmt(source), "fn add(a, b) {\n return a + b\n}\n");
}
#[test]
fn folds_else_if_chains() {
let source = "if a {\n1\n} else {\nif b {\n2\n} else {\n3\n}\n}";
assert_eq!(
fmt(source),
"if a {\n 1\n} else if b {\n 2\n} else {\n 3\n}\n"
);
}
#[test]
fn prints_anonymous_functions_inline() {
assert_eq!(
fmt("map(xs, fn(x){return x*2})"),
"map(xs, fn(x) { return x * 2 })\n"
);
}
#[test]
fn is_idempotent_on_a_range_of_constructs() {
let sources = [
"let x = 1 + 2 * 3",
"fn f(n) {\n if n < 2 {\n return n\n }\n return f(n - 1)\n}",
"for i in range(10) {\n print(i)\n}",
"for key, value in scores {\n print(key, value)\n}",
"let [width, height] = term_size()",
"let s = if a { 1 } else { 2 }",
"for [x, y] in cells {\n print(x)\n}",
"while a && b || c {\n x = x + 1\n}",
"let data = [1, 2, [3, 4], {\"k\": v}]",
"map(xs, fn(x) { return x * 2 })",
];
for source in sources {
let once = fmt(source);
assert_eq!(fmt(&once), once, "not idempotent for: {source}");
}
}
#[test]
fn prints_both_forms_of_a_for_loop() {
assert_eq!(
fmt("for k,v in m{print(k)}"),
"for k, v in m {\n print(k)\n}\n"
);
assert_eq!(
fmt("for x in xs{print(x)}"),
"for x in xs {\n print(x)\n}\n"
);
assert_eq!(
fmt("fn f() { for k, v in m { print(v) } }"),
"fn f() {\n for k, v in m {\n print(v)\n }\n}\n"
);
}
#[test]
fn prints_a_pattern_back() {
assert_eq!(fmt("let [x,y]=pair"), "let [x, y] = pair\n");
assert_eq!(fmt("let [ [a,b] , c ]=v"), "let [[a, b], c] = v\n");
assert_eq!(fmt("let []=v"), "let [] = v\n");
assert_eq!(fmt("let [x, y,] = pair"), "let [x, y] = pair\n");
assert_eq!(fmt("let [\n x,\n y,\n] = pair"), "let [x, y] = pair\n");
assert_eq!(
fmt("for [x,y] in cells{print(x)}"),
"for [x, y] in cells {\n print(x)\n}\n"
);
assert_eq!(
fmt("for i,[x,y] in cells{print(i)}"),
"for i, [x, y] in cells {\n print(i)\n}\n"
);
}
#[test]
fn prints_an_if_used_as_a_value() {
assert_eq!(
fmt("let s=if n>3{\"big\"}else{\"small\"}"),
"let s = if n > 3 { \"big\" } else { \"small\" }\n"
);
assert_eq!(
fmt("let s = if a { 1 } else if b { 2 } else { 3 }"),
"let s = if a { 1 } else if b { 2 } else { 3 }\n"
);
assert_eq!(fmt("if a {\n print(1)\n}"), "if a {\n print(1)\n}\n");
}
#[test]
fn round_trips_through_the_parser() {
let sources = [
"let x = -(a + b) * c",
"if p {\n q\n} else if r {\n s\n}",
"print(a, b, fn(x) { return x + 1 })",
"for k, v in m {\n print(k, v)\n}",
"let [x, [y, z]] = v",
"let s = if a { 1 } else if b { 2 } else { 3 }",
"for i, [x, y] in cells {\n print(i)\n}",
];
for source in sources {
let formatted = fmt(source);
assert_eq!(
program(&formatted),
program(source),
"reprint changed the tree for: {source}"
);
}
}
#[test]
fn reattaches_a_leading_comment() {
let stmts = program("let x = 1\n\nlet y = 2");
let trivia = Trivia {
comments: vec![Comment {
line: 2,
own_line: true,
text: " a note".to_string(),
}],
blank_before: HashSet::new(),
};
assert_eq!(
format_program(&stmts, &trivia),
"let x = 1\n// a note\nlet y = 2\n"
);
}
#[test]
fn reattaches_a_trailing_comment() {
let stmts = program("let x = 1\nlet y = 2");
let trivia = Trivia {
comments: vec![Comment {
line: 1,
own_line: false,
text: " trailing".to_string(),
}],
blank_before: HashSet::new(),
};
assert_eq!(
format_program(&stmts, &trivia),
"let x = 1 // trailing\nlet y = 2\n"
);
}
#[test]
fn preserves_a_single_blank_line() {
let stmts = program("let x = 1\n\nlet y = 2");
let trivia = Trivia {
comments: Vec::new(),
blank_before: HashSet::from([3]),
};
assert_eq!(format_program(&stmts, &trivia), "let x = 1\n\nlet y = 2\n");
}
#[test]
fn a_blank_above_a_shared_line_blanks_only_once() {
let stmts = program("let x = 1\n\nfn f() { return 2 }");
let trivia = Trivia {
comments: Vec::new(),
blank_before: HashSet::from([3]),
};
assert_eq!(
format_program(&stmts, &trivia),
"let x = 1\n\nfn f() {\n return 2\n}\n"
);
}
#[test]
fn indents_a_comment_inside_a_block() {
let stmts = program("if c {\n\n print(1)\n}");
let trivia = Trivia {
comments: vec![Comment {
line: 2,
own_line: true,
text: " inside".to_string(),
}],
blank_before: HashSet::new(),
};
assert_eq!(
format_program(&stmts, &trivia),
"if c {\n // inside\n print(1)\n}\n"
);
}
}