Pretty print expressions partially
This commit is contained in:
parent
23796e53a9
commit
1a3772e6f7
3 changed files with 50 additions and 1 deletions
|
|
@ -1,2 +1,3 @@
|
||||||
mod basic;
|
mod basic;
|
||||||
|
mod expr;
|
||||||
mod program;
|
mod program;
|
||||||
|
|
|
||||||
48
src/pretty/expr.rs
Normal file
48
src/pretty/expr.rs
Normal file
|
|
@ -0,0 +1,48 @@
|
||||||
|
use pretty::RcDoc;
|
||||||
|
|
||||||
|
use crate::ast::Expr;
|
||||||
|
|
||||||
|
impl Expr {
|
||||||
|
pub fn to_doc(&self) -> RcDoc {
|
||||||
|
match self {
|
||||||
|
Expr::Lit(lit) => RcDoc::text("<lit>"),
|
||||||
|
Expr::Call(call) => RcDoc::text("<call>"),
|
||||||
|
Expr::Field(field) => RcDoc::text("<field>"),
|
||||||
|
Expr::Var(var) => RcDoc::text("<var>"),
|
||||||
|
Expr::TableConstr(constr) => RcDoc::text("<onstr>"),
|
||||||
|
Expr::TableDestr(destr) => RcDoc::text("<destr>"),
|
||||||
|
Expr::FuncDef(def) => RcDoc::text("<def>"),
|
||||||
|
Expr::Paren {
|
||||||
|
s0,
|
||||||
|
inner,
|
||||||
|
s1,
|
||||||
|
span: _,
|
||||||
|
} => RcDoc::text("(").append(inner.to_doc()).append(")"),
|
||||||
|
|
||||||
|
// TODO Check whether parentheses are necessary
|
||||||
|
Expr::Neg {
|
||||||
|
minus: _,
|
||||||
|
s0,
|
||||||
|
expr,
|
||||||
|
span: _,
|
||||||
|
} => RcDoc::text("-").append(expr.to_doc()),
|
||||||
|
|
||||||
|
// TODO Check whether parentheses are necessary
|
||||||
|
Expr::Not {
|
||||||
|
not: _,
|
||||||
|
s0,
|
||||||
|
expr,
|
||||||
|
span: _,
|
||||||
|
} => RcDoc::text("not ").append(expr.to_doc()),
|
||||||
|
|
||||||
|
Expr::BinOp {
|
||||||
|
left,
|
||||||
|
s0,
|
||||||
|
op,
|
||||||
|
s1,
|
||||||
|
right,
|
||||||
|
span: _,
|
||||||
|
} => RcDoc::text("<binop>"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -10,7 +10,7 @@ impl Program {
|
||||||
expr,
|
expr,
|
||||||
s1,
|
s1,
|
||||||
span: _,
|
span: _,
|
||||||
} => RcDoc::text("<expr>"),
|
} => expr.to_doc(),
|
||||||
Program::Module {
|
Program::Module {
|
||||||
s0,
|
s0,
|
||||||
s1,
|
s1,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue