Clean up primitive handling, finally.
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
use crate::syntax::ast::{ConstantType, Expression, Program, Statement, TopLevel, Type, Value};
|
||||
use crate::syntax::ast::{ConstantType, Expression, Program, TopLevel, Type, Value};
|
||||
use crate::util::pretty::{derived_display, Allocator};
|
||||
use pretty::{DocAllocator, DocBuilder};
|
||||
|
||||
@@ -20,7 +20,7 @@ impl Program {
|
||||
impl TopLevel {
|
||||
pub fn pretty<'a>(&self, allocator: &'a Allocator<'a>) -> DocBuilder<'a, Allocator<'a>> {
|
||||
match self {
|
||||
TopLevel::Statement(stmt) => stmt.pretty(allocator),
|
||||
TopLevel::Expression(expr) => expr.pretty(allocator),
|
||||
TopLevel::Function(name, args, rettype, body) => allocator
|
||||
.text("function")
|
||||
.append(allocator.space())
|
||||
@@ -87,18 +87,6 @@ impl TopLevel {
|
||||
}
|
||||
}
|
||||
|
||||
impl Statement {
|
||||
pub fn pretty<'a>(&self, allocator: &'a Allocator<'a>) -> DocBuilder<'a, Allocator<'a>> {
|
||||
match self {
|
||||
Statement::Print(_, var) => allocator
|
||||
.text("print")
|
||||
.append(allocator.space())
|
||||
.append(allocator.text(var.to_string())),
|
||||
Statement::Expression(e) => e.pretty(allocator),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Expression {
|
||||
pub fn pretty<'a>(&self, allocator: &'a Allocator<'a>) -> DocBuilder<'a, Allocator<'a>> {
|
||||
match self {
|
||||
@@ -129,25 +117,7 @@ impl Expression {
|
||||
.text(t.clone())
|
||||
.angles()
|
||||
.append(e.pretty(allocator)),
|
||||
Expression::Primitive(_, op, exprs) if exprs.len() == 1 => allocator
|
||||
.text(op.to_string())
|
||||
.append(exprs[0].pretty(allocator)),
|
||||
Expression::Primitive(_, op, exprs) if exprs.len() == 2 => {
|
||||
let left = exprs[0].pretty(allocator);
|
||||
let right = exprs[1].pretty(allocator);
|
||||
|
||||
left.append(allocator.space())
|
||||
.append(allocator.text(op.to_string()))
|
||||
.append(allocator.space())
|
||||
.append(right)
|
||||
.parens()
|
||||
}
|
||||
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, allocator.text(","));
|
||||
call.append(comma_sepped_args.parens())
|
||||
}
|
||||
Expression::Primitive(_, op) => allocator.text(op.name.clone()),
|
||||
Expression::Call(_, fun, args) => {
|
||||
let args = args.iter().map(|x| x.pretty(allocator));
|
||||
let comma_sepped_args = allocator.intersperse(args, allocator.text(","));
|
||||
@@ -245,6 +215,5 @@ impl Type {
|
||||
|
||||
derived_display!(Program);
|
||||
derived_display!(TopLevel);
|
||||
derived_display!(Statement);
|
||||
derived_display!(Expression);
|
||||
derived_display!(Value);
|
||||
|
||||
Reference in New Issue
Block a user