-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Finish implementing
Display
for Def
and Expr
.
Except for enums.
- Loading branch information
Vlamonster
committed
Nov 11, 2023
1 parent
d6212eb
commit aaac4d9
Showing
4 changed files
with
124 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,132 @@ | ||
use std::fmt::{Display, Formatter}; | ||
use crate::passes::parse::{Def, Expr, Op, TypeDef}; | ||
use indenter::indented; | ||
use itertools::Itertools; | ||
use crate::passes::parse::{Def, Expr, Op}; | ||
use std::fmt::Write; | ||
use std::fmt::{Display, Formatter}; | ||
|
||
impl<IdentVars: Display, IdentFields: Display, Expr: Display> Display for Def<IdentVars, IdentFields, Expr> { | ||
impl<IdentVars: Display, IdentFields: Display, Expr: Display> Display | ||
for Def<IdentVars, IdentFields, Expr> | ||
{ | ||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { | ||
match self { | ||
Def::Fn { sym, params, typ, bdy } => { | ||
Def::Fn { | ||
sym, | ||
params, | ||
typ, | ||
bdy, | ||
} => { | ||
writeln!(f, "fn {sym}({}) -> {typ} {{", params.iter().format(", "))?; | ||
writeln!(indented(f), "{bdy}")?; | ||
writeln!(f, "}}")?; | ||
Ok(()) | ||
} | ||
Def::TypeDef { sym, def } => todo!(), | ||
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") | ||
)?; | ||
writeln!(f, "}}") | ||
} | ||
TypeDef::Enum { .. } => todo!(), | ||
}, | ||
} | ||
} | ||
} | ||
|
||
impl<IdentVars: Display, IdentFields: Display> Display for Expr<'_, IdentVars, IdentFields> { | ||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { | ||
match self { | ||
Expr::Lit { val } => write!(f, "{val}"), | ||
Expr::Var { sym } => write!(f, "{sym}"), | ||
Expr::Prim { op: Op::Read, ..} => write!(f, "read()"), | ||
Expr::Prim { op: Op::Print, args} => write!(f, "print({})", args[0].inner), | ||
Expr::Prim { op, args } if args.len() == 1 => write!(f, "({op} {})", args[0].inner), | ||
Expr::Prim { op, args } if args.len() == 2 => write!(f, "({} {op} {})", args[0].inner, args[1].inner), | ||
Expr::Prim { .. } => unreachable!(), | ||
Expr::Let { sym, mutable, bnd, bdy } => { | ||
writeln!(f, "let {}{sym} = {bnd};", if *mutable { "mut " } else { "" })?; | ||
Expr::Lit { val } => { | ||
write!(f, "{val}") | ||
} | ||
Expr::Var { sym } => { | ||
write!(f, "{sym}") | ||
} | ||
Expr::Prim { op: Op::Read, .. } => { | ||
write!(f, "read()") | ||
} | ||
Expr::Prim { | ||
op: Op::Print, | ||
args, | ||
} => { | ||
write!(f, "print({})", args[0].inner) | ||
} | ||
Expr::Prim { op, args } if args.len() == 1 => { | ||
write!(f, "({op} {})", args[0].inner) | ||
} | ||
Expr::Prim { op, args } if args.len() == 2 => { | ||
write!(f, "({} {op} {})", args[0].inner, args[1].inner) | ||
} | ||
Expr::Prim { .. } => { | ||
unreachable!() | ||
} | ||
Expr::Let { | ||
sym, | ||
mutable, | ||
bnd, | ||
bdy, | ||
} => { | ||
writeln!( | ||
f, | ||
"let {}{sym} = {bnd};", | ||
if *mutable { "mut " } else { "" } | ||
)?; | ||
write!(f, "{bdy}") | ||
}, | ||
} | ||
Expr::If { cnd, thn, els } => { | ||
writeln!(f, "if {cnd} {{")?; | ||
writeln!(indented(f), "{thn}")?; | ||
writeln!(f, "}} else {{")?; | ||
writeln!(indented(f), "{els}")?; | ||
write!(f, "}}") | ||
} | ||
Expr::Apply { .. } => todo!(), | ||
Expr::Loop { .. } => todo!(), | ||
Expr::Break { .. } => todo!(), | ||
Expr::Continue => todo!(), | ||
Expr::Return { .. } => todo!(), | ||
Expr::Seq { .. } => todo!(), | ||
Expr::Assign { .. } => todo!(), | ||
Expr::Struct { .. } => todo!(), | ||
Expr::Apply { fun, args } => { | ||
write!(f, "{fun}({})", args.iter().format(", ")) | ||
} | ||
Expr::Loop { bdy } => { | ||
writeln!(f, "loop {{")?; | ||
writeln!(indented(f), "{bdy}")?; | ||
write!(f, "}}") | ||
} | ||
Expr::Break { bdy } => { | ||
write!(f, "break {bdy}") | ||
} | ||
Expr::Continue => { | ||
write!(f, "continue") | ||
} | ||
Expr::Return { bdy } => { | ||
write!(f, "return {bdy}") | ||
} | ||
Expr::Seq { stmt, cnt } => { | ||
writeln!(f, "{stmt};")?; | ||
write!(f, "{cnt}") | ||
} | ||
Expr::Assign { sym, bnd } => { | ||
write!(f, "{sym} = {bnd}") | ||
} | ||
Expr::Struct { sym, fields } => { | ||
writeln!(f, "{sym} {{")?; | ||
writeln!( | ||
indented(f), | ||
"{}", | ||
fields | ||
.iter() | ||
.map(|(sym, bnd)| format!("{sym}: {bnd},")) | ||
.format("\n") | ||
)?; | ||
write!(f, "}}") | ||
} | ||
Expr::AccessField { strct, field } => { | ||
write!(f, "{strct}.{field}") | ||
} | ||
Expr::Variant { .. } => todo!(), | ||
Expr::AccessField { .. } => todo!(), | ||
Expr::Switch { .. } => todo!(), | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters