diff --git a/plang2_lib/src/ast.rs b/plang2_lib/src/ast.rs index 1811d21..e9e8cc0 100644 --- a/plang2_lib/src/ast.rs +++ b/plang2_lib/src/ast.rs @@ -1,3 +1,5 @@ +use std::fmt::Display; + use crate::token::Literal; /// Binary Operator Types. For operations that have two operands @@ -35,8 +37,6 @@ pub enum BinOpType { And, /// Boolean Or "||" Or, - /// Boolean Not "!" - Not, /// Boolean Xor "^^" Xor, } @@ -108,3 +108,47 @@ impl Ast { Self { prog } } } + +impl Display for Ast { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + writeln!(f, "Ast[")?; + for stmt in &self.prog { + writeln!(f, "{:#?},", stmt)?; + } + writeln!(f, "]") + } +} + +impl Display for BinOpType { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let sop = match self { + BinOpType::Add => "+", + BinOpType::Sub => "-", + BinOpType::Mul => "*", + BinOpType::Div => "/", + BinOpType::Mod => "%", + BinOpType::Eq => "==", + BinOpType::Neq => "!=", + BinOpType::Gt => ">", + BinOpType::Lt => "<", + BinOpType::Ge => ">=", + BinOpType::Le => "<=", + BinOpType::And => "&&", + BinOpType::Or => "||", + BinOpType::Xor => "^^", + }; + + write!(f, "{}", sop) + } +} + +impl Display for UnOpType { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let sop = match self { + UnOpType::Neg => "-", + UnOpType::Not => "!", + }; + + write!(f, "{}", sop) + } +} diff --git a/plang2_lib/src/interpreter.rs b/plang2_lib/src/interpreter.rs index d386a13..11f3b65 100644 --- a/plang2_lib/src/interpreter.rs +++ b/plang2_lib/src/interpreter.rs @@ -83,7 +83,6 @@ impl Interpreter { BinOpType::And => todo!(), BinOpType::Or => todo!(), - BinOpType::Not => todo!(), BinOpType::Xor => todo!(), };