From 62b130faabce43f092380d90556ff5dd793aa35e Mon Sep 17 00:00:00 2001 From: Vlamonster Date: Wed, 6 Dec 2023 01:27:07 +0100 Subject: [PATCH] Implement `Display` for inline assembly. --- compiler/src/passes/atomize/display.rs | 19 +++++----- compiler/src/passes/explicate/display.rs | 11 ++---- compiler/src/passes/parse/display.rs | 45 +++++++++++------------- compiler/src/passes/reveal/display.rs | 27 +++++++------- 4 files changed, 46 insertions(+), 56 deletions(-) diff --git a/compiler/src/passes/atomize/display.rs b/compiler/src/passes/atomize/display.rs index 330ad33..e79e6b1 100644 --- a/compiler/src/passes/atomize/display.rs +++ b/compiler/src/passes/atomize/display.rs @@ -59,20 +59,21 @@ impl Display for AExpr<'_> { } AExpr::Struct { sym, fields } => { writeln!(f, "{sym} {{")?; - writeln!( - indented(f), - "{}", - fields - .iter() - .map(|(sym, bnd)| format!("{sym}: {bnd},")) - .format("\n") - )?; + for (field_sym, field_bnd) in fields { + writeln!(indented(f), "{field_sym}: {field_bnd},")?; + } write!(f, "}}") } AExpr::AccessField { strct, field } => { write!(f, "{strct}.{field}") } - AExpr::Asm { .. } => todo!(), + AExpr::Asm { instrs } => { + writeln!(f, "asm {{")?; + for instr in instrs { + writeln!(indented(f), "{instr}")?; + } + write!(f, "}}") + } } } } diff --git a/compiler/src/passes/explicate/display.rs b/compiler/src/passes/explicate/display.rs index 1c9cd41..0bd6ceb 100644 --- a/compiler/src/passes/explicate/display.rs +++ b/compiler/src/passes/explicate/display.rs @@ -47,14 +47,9 @@ impl Display for ExprExplicated<'_> { } ExprExplicated::Struct { sym, fields } => { writeln!(f, "{sym} {{")?; - writeln!( - indented(f), - "{}", - fields - .iter() - .map(|(sym, bnd)| format!("{sym}: {bnd},")) - .format("\n") - )?; + for (field_sym, field_bnd) in fields { + writeln!(indented(f), "{field_sym}: {field_bnd},")?; + } write!(f, "}}") } ExprExplicated::AccessField { strct, field } => { diff --git a/compiler/src/passes/parse/display.rs b/compiler/src/passes/parse/display.rs index 2ebcca1..f0e12da 100644 --- a/compiler/src/passes/parse/display.rs +++ b/compiler/src/passes/parse/display.rs @@ -23,14 +23,9 @@ impl Display Def::TypeDef { sym, def } => match def { TypeDef::Struct { fields } => { writeln!(f, "struct {sym} {{")?; - writeln!( - indented(f), - "{}", - fields - .iter() - .map(|(sym, bnd)| format!("{sym}: {bnd},")) - .format("\n") - )?; + for (field_sym, field_typ) in fields { + writeln!(indented(f), "{field_sym}: {field_typ},")?; + } writeln!(f, "}}") } TypeDef::Enum { .. } => todo!(), @@ -66,14 +61,13 @@ impl Display bnd, bdy, } => { - writeln!( - f, - "let {}{sym}{} = {bnd};", - if *mutable { "mut " } else { "" }, - typ.as_ref() - .map(|typ| format!(": {typ}")) - .unwrap_or("".to_string()) - )?; + let mutable = if *mutable { "mut " } else { "" }; + let typ = typ + .as_ref() + .map(|typ| format!(": {typ}")) + .unwrap_or_default(); + + writeln!(f, "let {mutable}{sym}{typ} = {bnd};")?; write!(f, "{bdy}") } Expr::If { cnd, thn, els } => { @@ -109,22 +103,23 @@ impl Display } Expr::Struct { sym, fields } => { writeln!(f, "{sym} {{")?; - writeln!( - indented(f), - "{}", - fields - .iter() - .map(|(sym, bnd)| format!("{sym}: {bnd},")) - .format("\n") - )?; + for (field_sym, field_bnd) in fields { + writeln!(indented(f), "{field_sym}: {field_bnd},")?; + } write!(f, "}}") } Expr::AccessField { strct, field } => { write!(f, "{strct}.{field}") } + Expr::Asm { instrs } => { + writeln!(f, "asm {{")?; + for instr in instrs { + writeln!(indented(f), "{instr}")?; + } + write!(f, "}}") + } Expr::Variant { .. } => todo!(), Expr::Switch { .. } => todo!(), - Expr::Asm { .. } => todo!(), } } } diff --git a/compiler/src/passes/reveal/display.rs b/compiler/src/passes/reveal/display.rs index 7b60793..870b60f 100644 --- a/compiler/src/passes/reveal/display.rs +++ b/compiler/src/passes/reveal/display.rs @@ -31,11 +31,9 @@ impl Display for RExpr<'_> { bnd, bdy, } => { - writeln!( - f, - "let {}{sym} = {bnd};", - if *mutable { "mut " } else { "" } - )?; + let mutable = if *mutable { "mut " } else { "" }; + + writeln!(f, "let {mutable}{sym} = {bnd};")?; write!(f, "{bdy}") } RExpr::If { cnd, thn, els } => { @@ -71,20 +69,21 @@ impl Display for RExpr<'_> { } RExpr::Struct { sym, fields } => { writeln!(f, "{sym} {{")?; - writeln!( - indented(f), - "{}", - fields - .iter() - .map(|(sym, bnd)| format!("{sym}: {bnd},")) - .format("\n") - )?; + for (field_sym, field_bnd) in fields { + writeln!(indented(f), "{field_sym}: {field_bnd},")?; + } write!(f, "}}") } RExpr::AccessField { strct, field } => { write!(f, "{strct}.{field}") } - RExpr::Asm { .. } => todo!(), + RExpr::Asm { instrs } => { + writeln!(f, "asm {{")?; + for instr in instrs { + writeln!(indented(f), "{instr}")?; + } + write!(f, "}}") + } } } }