Pretty print variable accesses and assignments

This commit is contained in:
Joscha 2022-11-21 00:02:10 +01:00
parent 5bd43ee37a
commit c7fc8584ff
3 changed files with 47 additions and 1 deletions

View file

@ -5,5 +5,6 @@ mod field;
mod lit; mod lit;
mod program; mod program;
mod table_constr; mod table_constr;
mod var;
const NEST_DEPTH: isize = 4; const NEST_DEPTH: isize = 4;

View file

@ -28,7 +28,7 @@ impl<'a, D: DocAllocator<'a>> Pretty<'a, D> for Expr {
Self::Lit(lit) => lit.pretty(allocator), Self::Lit(lit) => lit.pretty(allocator),
Self::Call(call) => call.pretty(allocator), Self::Call(call) => call.pretty(allocator),
Self::Field(field) => field.pretty(allocator), Self::Field(field) => field.pretty(allocator),
Self::Var(var) => allocator.text("<var>"), Self::Var(var) => var.pretty(allocator),
Self::TableConstr(constr) => constr.pretty(allocator), Self::TableConstr(constr) => constr.pretty(allocator),
Self::TableDestr(destr) => allocator.text("<destr>"), Self::TableDestr(destr) => allocator.text("<destr>"),
Self::FuncDef(def) => allocator.text("<def>"), Self::FuncDef(def) => allocator.text("<def>"),

45
src/pretty/var.rs Normal file
View file

@ -0,0 +1,45 @@
use pretty::{DocAllocator, DocBuilder, Pretty};
use crate::ast::Var;
impl<'a, D: DocAllocator<'a>> Pretty<'a, D> for Var {
fn pretty(self, allocator: &'a D) -> DocBuilder<'a, D> {
match self {
Self::Access {
s0,
index,
s1,
span: _,
} => index.pretty(allocator).brackets(),
Self::Assign {
local,
s0,
index,
s1,
s2,
s3,
value,
span: _,
} => local
.map(|s| allocator.text("local "))
.unwrap_or_else(|| allocator.nil())
.append(index.pretty(allocator).brackets())
.append(allocator.text(" = "))
.append(value.pretty(allocator)),
Self::AccessIdent(ident) => ident.pretty(allocator),
Self::AssignIdent {
local,
name,
s0,
s1,
value,
span: _,
} => local
.map(|s| allocator.text("local "))
.unwrap_or_else(|| allocator.nil())
.append(name.pretty(allocator))
.append(allocator.text(" = "))
.append(value.pretty(allocator)),
}
}
}