daisy/src/parser/expression/expression.rs

86 lines
2.1 KiB
Rust
Raw Normal View History

2023-06-11 13:53:45 -07:00
use std::collections::VecDeque;
use crate::quantity::Quantity;
use super::Operator;
use super::Constant;
2023-06-16 12:58:06 -07:00
/// Expressions represent logical objects in an expession.
2023-06-11 13:53:45 -07:00
#[derive(Debug)]
#[derive(Clone)]
2023-06-16 12:58:06 -07:00
pub enum Expression {
2023-06-14 14:04:32 -07:00
Variable(String),
2023-06-11 13:53:45 -07:00
Quantity(Quantity),
Constant(Constant),
2023-06-16 12:58:06 -07:00
Operator(Operator, VecDeque<Expression>),
2023-06-11 13:53:45 -07:00
}
2023-06-16 12:58:06 -07:00
impl ToString for Expression {
2023-06-11 13:53:45 -07:00
fn to_string(&self) -> String {
match self {
2023-06-16 12:58:06 -07:00
Expression::Quantity(v) => v.to_string(),
Expression::Constant(c) => c.to_string(),
Expression::Variable(s) => s.clone(),
Expression::Operator(o,a) => o.print(a)
2023-06-11 13:53:45 -07:00
}
}
}
2023-06-16 12:58:06 -07:00
impl Expression {
// This is called only when this is the outermost Expression.
2023-06-11 13:53:45 -07:00
// This sometimes leads to different--usually more verbose--behavior.
pub fn to_string_outer(&self) -> String {
match self {
2023-06-16 12:58:06 -07:00
Expression::Quantity(v) => v.to_string_outer(),
Expression::Constant(c) => c.to_string(),
Expression::Variable(s) => s.clone(),
Expression::Operator(o,a) => o.print(a)
2023-06-11 13:53:45 -07:00
}
}
2023-06-14 20:18:28 -07:00
pub fn is_quantity(&self) -> bool {
match self {
2023-06-16 12:58:06 -07:00
Expression::Quantity(_) => true,
2023-06-14 20:18:28 -07:00
_ => false
}
}
2023-06-11 13:53:45 -07:00
#[inline(always)]
2023-06-16 12:58:06 -07:00
pub fn get_args_mut(&mut self) -> Option<&mut VecDeque<Expression>> {
2023-06-11 13:53:45 -07:00
match self {
2023-06-16 12:58:06 -07:00
Expression::Operator(_, ref mut a) => Some(a),
2023-06-11 13:53:45 -07:00
_ => None
}
}
#[inline(always)]
2023-06-16 12:58:06 -07:00
pub fn get_args(&self) -> Option<&VecDeque<Expression>> {
2023-06-14 20:18:28 -07:00
match self {
2023-06-16 12:58:06 -07:00
Expression::Operator(_, ref a) => Some(a),
2023-06-14 20:18:28 -07:00
_ => None
}
}
2023-06-11 13:53:45 -07:00
2023-06-14 20:18:28 -07:00
#[inline(always)]
2023-06-16 12:58:06 -07:00
pub fn get_at_coords<'a, 'b, I>(&'a self, coords: I) -> Option<&'a Expression>
2023-06-14 20:18:28 -07:00
where I: IntoIterator<Item = &'b usize> + Sized {
let mut g = self;
for t in coords.into_iter() {
let args = g.get_args();
let Some(args) = args else { return None; };
g = &args[*t];
2023-06-11 13:53:45 -07:00
}
2023-06-14 20:18:28 -07:00
return Some(g);
}
2023-06-11 13:53:45 -07:00
2023-06-14 20:18:28 -07:00
#[inline(always)]
2023-06-16 12:58:06 -07:00
pub fn get_at_coords_mut<'a, 'b, I>(&'a mut self, coords: I) -> Option<&'a mut Expression>
2023-06-14 20:18:28 -07:00
where I: IntoIterator<Item = &'b usize> + Sized {
let mut g = self;
for t in coords.into_iter() {
let args = g.get_args_mut();
let Some(args) = args else { return None; };
g = &mut args[*t];
}
return Some(g);
2023-06-11 13:53:45 -07:00
}
}