use std::fmt::Display;
use std::fmt::Formatter;
use std::fmt::Result as fResult;
use crate::ast::AST;
use crate::util::encdec::ToText;
use crate::util::strtype::StrType;
use std::str::FromStr;
use crate::common::error::{ErrMsg, MsgResult};
#[derive(Debug, PartialEq, Eq, Hash)]
pub enum BinOpSymbol {
Plus,
Dash,
Asterisk,
Slash,
}
impl BinOpSymbol {
pub fn new(symbol_txt: &str) -> MsgResult<Self> {
match symbol_txt {
"+" => Ok(BinOpSymbol::Plus),
"-" => Ok(BinOpSymbol::Dash),
"*" => Ok(BinOpSymbol::Asterisk),
"/" => Ok(BinOpSymbol::Slash),
_ => Err(ErrMsg::new(format!("Unknown symbol: '{}'", symbol_txt))),
}
}
}
impl Display for BinOpSymbol {
fn fmt(&self, f: &mut Formatter) -> fResult {
write!(
f,
"{}",
match *self {
BinOpSymbol::Plus => "+",
BinOpSymbol::Dash => "-",
BinOpSymbol::Asterisk => "*",
BinOpSymbol::Slash => "/",
}
)
}
}
#[derive(Debug, PartialEq, Eq, Hash)]
pub struct OperatorAST {
symbol: BinOpSymbol,
}
impl FromStr for OperatorAST {
type Err = ErrMsg;
fn from_str(symbol_txt: &str) -> Result<OperatorAST, ErrMsg> {
Ok(OperatorAST::from_symbol(BinOpSymbol::new(symbol_txt)?))
}
}
impl OperatorAST {
pub fn from_symbol(symbol: BinOpSymbol) -> OperatorAST {
OperatorAST { symbol }
}
pub fn is_add_sub(&self) -> bool {
self.symbol == BinOpSymbol::Plus || self.symbol == BinOpSymbol::Dash
}
pub fn is_mul_div(&self) -> bool {
self.symbol == BinOpSymbol::Asterisk || self.symbol == BinOpSymbol::Slash
}
}
impl ToText for OperatorAST {
fn to_text(&self) -> String {
format!(" {} ", self.symbol)
}
}
impl AST for OperatorAST {}