use super::*;
use leo_span::{Symbol, sym};
#[derive(Debug, Copy, Clone, PartialEq, Eq, Ord, PartialOrd, Hash, Serialize, Deserialize)]
pub enum UnaryOperation {
Abs,
AbsWrapped,
Double,
Inverse,
Negate,
Not,
Square,
SquareRoot,
ToXCoordinate,
ToYCoordinate,
}
impl UnaryOperation {
pub fn from_symbol(symbol: Symbol) -> Option<Self> {
Some(match symbol {
sym::abs => Self::Abs,
sym::abs_wrapped => Self::AbsWrapped,
sym::double => Self::Double,
sym::inv => Self::Inverse,
sym::neg => Self::Negate,
sym::not => Self::Not,
sym::square => Self::Square,
sym::square_root => Self::SquareRoot,
sym::to_x_coordinate => Self::ToXCoordinate,
sym::to_y_coordinate => Self::ToYCoordinate,
_ => return None,
})
}
fn as_str(self) -> &'static str {
match self {
Self::Abs => "abs",
Self::AbsWrapped => "abs_wrapped",
Self::Double => "double",
Self::Inverse => "inv",
Self::Negate => "neg",
Self::Not => "not",
Self::Square => "square",
Self::SquareRoot => "square_root",
Self::ToXCoordinate => "to_x_coordinate",
Self::ToYCoordinate => "to_y_coordinate",
}
}
}
impl fmt::Display for UnaryOperation {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.as_str())
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct UnaryExpression {
pub receiver: Expression,
pub op: UnaryOperation,
pub span: Span,
pub id: NodeID,
}
impl fmt::Display for UnaryExpression {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if self.receiver.precedence() < 20 {
write!(f, "({})", self.receiver)?;
} else {
write!(f, "{}", self.receiver)?;
}
write!(f, ".{}()", self.op)
}
}
impl From<UnaryExpression> for Expression {
fn from(value: UnaryExpression) -> Self {
Expression::Unary(Box::new(value))
}
}
crate::simple_node_impl!(UnaryExpression);