use crate::operators::Op;
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
pub enum AST {
Sym(Symbol),
Number(String),
BinaryExpr(BinaryOp, Box<AST>, Box<AST>),
UnaryExpr(UnaryOp, Box<AST>),
Function(Symbol, Vec<AST>),
}
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
pub struct Symbol {
pub unicode_repr: String,
pub ascii_repr: String,
pub latex_repr: String,
pub other_reprs: Vec<String>,
}
impl Symbol {
pub fn new(unicode: &str, ascii: &str, latex: &str, others: Vec<&str>) -> Symbol {
Symbol {
unicode_repr: unicode.into(),
ascii_repr: ascii.into(),
latex_repr: latex.into(),
other_reprs: others.iter().map(|&x| x.to_string()).collect(),
}
}
pub fn reprs(&self) -> Vec<&str> {
let mut reprs = vec![
self.unicode_repr.as_str(),
self.ascii_repr.as_str(),
self.latex_repr.as_str(),
];
reprs.extend_from_slice(
self.other_reprs
.iter()
.map(|x| x.as_str())
.collect::<Vec<&str>>()
.as_slice(),
);
reprs
}
pub fn match_front(&self, input: &str) -> Option<&str> {
for repr in self.reprs() {
if input.starts_with(repr) {
return Some(repr);
}
}
None
}
}
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
pub enum Fixity {
Prefix,
Infix,
Postfix,
}
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
pub struct SymbolBinaryOp {
pub op: Op,
pub fixity: Fixity,
}
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
pub enum BinaryOp {
Generic(SymbolBinaryOp),
Power,
Frac,
Log,
Concat,
}
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
pub enum UnaryOp {
Generic(Symbol),
}