Clean up pretty printing and work on logging.
This commit is contained in:
@@ -1,14 +1,9 @@
|
||||
use crate::syntax::ast::{Expression, Program, Statement, Value};
|
||||
use pretty::{DocAllocator, DocBuilder, Pretty};
|
||||
use crate::syntax::ast::{ConstantType, Expression, Program, Statement, TopLevel, Value};
|
||||
use crate::util::pretty::{Allocator, derived_display};
|
||||
use pretty::{DocAllocator, DocBuilder};
|
||||
|
||||
use super::{ConstantType, TopLevel};
|
||||
|
||||
impl<'a, 'b, D, A> Pretty<'a, D, A> for &'b Program
|
||||
where
|
||||
A: 'a,
|
||||
D: ?Sized + DocAllocator<'a, A>,
|
||||
{
|
||||
fn pretty(self, allocator: &'a D) -> DocBuilder<'a, D, A> {
|
||||
impl Program {
|
||||
pub fn pretty<'a>(&self, allocator: &'a Allocator<'a>) -> DocBuilder<'a, Allocator<'a>> {
|
||||
let mut result = allocator.nil();
|
||||
|
||||
for tl in self.items.iter() {
|
||||
@@ -22,12 +17,8 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, 'b, D, A> Pretty<'a, D, A> for &'b TopLevel
|
||||
where
|
||||
A: 'a,
|
||||
D: ?Sized + DocAllocator<'a, A>,
|
||||
{
|
||||
fn pretty(self, allocator: &'a D) -> DocBuilder<'a, D, A> {
|
||||
impl TopLevel {
|
||||
pub fn pretty<'a>(&self, allocator: &'a Allocator<'a>) -> DocBuilder<'a, Allocator<'a>> {
|
||||
match self {
|
||||
TopLevel::Statement(stmt) => stmt.pretty(allocator),
|
||||
TopLevel::Function(name, arg_names, body) => allocator
|
||||
@@ -42,7 +33,7 @@ where
|
||||
allocator
|
||||
.intersperse(
|
||||
arg_names.iter().map(|x| allocator.text(x.to_string())),
|
||||
CommaSep {},
|
||||
allocator.text(","),
|
||||
)
|
||||
.parens(),
|
||||
)
|
||||
@@ -52,12 +43,8 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, 'b, D, A> Pretty<'a, D, A> for &'b Statement
|
||||
where
|
||||
A: 'a,
|
||||
D: ?Sized + DocAllocator<'a, A>,
|
||||
{
|
||||
fn pretty(self, allocator: &'a D) -> DocBuilder<'a, D, A> {
|
||||
impl Statement {
|
||||
pub fn pretty<'a>(&self, allocator: &'a Allocator<'a>) -> DocBuilder<'a, Allocator<'a>> {
|
||||
match self {
|
||||
Statement::Binding(_, var, expr) => allocator
|
||||
.text(var.to_string())
|
||||
@@ -73,12 +60,8 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, 'b, D, A> Pretty<'a, D, A> for &'b Expression
|
||||
where
|
||||
A: 'a,
|
||||
D: ?Sized + DocAllocator<'a, A>,
|
||||
{
|
||||
fn pretty(self, allocator: &'a D) -> DocBuilder<'a, D, A> {
|
||||
impl Expression {
|
||||
pub fn pretty<'a>(&self, allocator: &'a Allocator<'a>) -> DocBuilder<'a, Allocator<'a>> {
|
||||
match self {
|
||||
Expression::Value(_, val) => val.pretty(allocator),
|
||||
Expression::Reference(_, var) => allocator.text(var.to_string()),
|
||||
@@ -102,12 +85,12 @@ where
|
||||
Expression::Primitive(_, op, exprs) => {
|
||||
let call = allocator.text(op.to_string());
|
||||
let args = exprs.iter().map(|x| x.pretty(allocator));
|
||||
let comma_sepped_args = allocator.intersperse(args, CommaSep {});
|
||||
let comma_sepped_args = allocator.intersperse(args, allocator.text(","));
|
||||
call.append(comma_sepped_args.parens())
|
||||
}
|
||||
Expression::Call(_, fun, args) => {
|
||||
let args = args.iter().map(|x| x.pretty(allocator));
|
||||
let comma_sepped_args = allocator.intersperse(args, CommaSep {});
|
||||
let comma_sepped_args = allocator.intersperse(args, allocator.text(","));
|
||||
fun.pretty(allocator).append(comma_sepped_args.parens())
|
||||
}
|
||||
Expression::Block(_, stmts) => match stmts.split_last() {
|
||||
@@ -133,12 +116,8 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, 'b, D, A> Pretty<'a, D, A> for &'b Value
|
||||
where
|
||||
A: 'a,
|
||||
D: ?Sized + DocAllocator<'a, A>,
|
||||
{
|
||||
fn pretty(self, allocator: &'a D) -> DocBuilder<'a, D, A> {
|
||||
impl Value {
|
||||
pub fn pretty<'a>(&self, allocator: &'a Allocator<'a>) -> DocBuilder<'a, Allocator<'a>> {
|
||||
match self {
|
||||
Value::Number(opt_base, ty, value) => {
|
||||
let value_str = match opt_base {
|
||||
@@ -171,15 +150,8 @@ fn type_suffix(x: &Option<ConstantType>) -> &'static str {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
pub struct CommaSep {}
|
||||
|
||||
impl<'a, D, A> Pretty<'a, D, A> for CommaSep
|
||||
where
|
||||
A: 'a,
|
||||
D: ?Sized + DocAllocator<'a, A>,
|
||||
{
|
||||
fn pretty(self, allocator: &'a D) -> DocBuilder<'a, D, A> {
|
||||
allocator.text(",").append(allocator.space())
|
||||
}
|
||||
}
|
||||
derived_display!(Program);
|
||||
derived_display!(TopLevel);
|
||||
derived_display!(Statement);
|
||||
derived_display!(Expression);
|
||||
derived_display!(Value);
|
||||
Reference in New Issue
Block a user