add formatting for unary expression

This commit is contained in:
collin 2020-08-07 15:05:00 -07:00
parent e84aa7b782
commit 599c886d23
3 changed files with 18 additions and 1 deletions

View File

@ -66,7 +66,7 @@ impl<'ast> fmt::Display for Expression<'ast> {
match *self {
Expression::Value(ref expression) => write!(f, "{}", expression),
Expression::Identifier(ref expression) => write!(f, "{}", expression),
Expression::Unary(ref expression) => write!(f, "!{}", expression.expression),
Expression::Unary(ref expression) => write!(f, "{}", expression),
Expression::Binary(ref expression) => write!(f, "{} == {}", expression.left, expression.right),
Expression::Ternary(ref expression) => write!(
f,

View File

@ -3,6 +3,7 @@ use crate::{ast::Rule, expressions::Expression, operations::UnaryOperation, Span
use pest::Span;
use pest_ast::FromPest;
use serde::Serialize;
use std::fmt;
#[derive(Clone, Debug, FromPest, PartialEq, Serialize)]
#[pest_ast(rule(Rule::expression_unary))]
@ -13,3 +14,9 @@ pub struct UnaryExpression<'ast> {
#[serde(with = "SpanDef")]
pub span: Span<'ast>,
}
impl<'ast> fmt::Display for UnaryExpression {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}{}", self.operation, self.expression)
}
}

View File

@ -2,6 +2,7 @@ use crate::ast::Rule;
use pest_ast::FromPest;
use serde::Serialize;
use std::fmt;
#[derive(Clone, Debug, FromPest, PartialEq, Serialize)]
#[pest_ast(rule(Rule::operation_unary))]
@ -10,6 +11,15 @@ pub enum UnaryOperation {
Not(Not),
}
impl fmt::Display for UnaryOperation {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
UnaryOperation::Minus(_) => write!(f, "-"),
UnaryOperation::Not(_) => write!(f, "!"),
}
}
}
#[derive(Clone, Debug, FromPest, PartialEq, Serialize)]
#[pest_ast(rule(Rule::operation_not))]
pub struct Not {}