use crate::syntax::ast::{node::Node, op};
use boa_interner::{Interner, ToInternedString};
#[cfg(feature = "deser")]
use serde::{Deserialize, Serialize};
#[cfg_attr(feature = "deser", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq)]
pub struct BinOp {
op: op::BinOp,
lhs: Box<Node>,
rhs: Box<Node>,
}
impl BinOp {
pub(in crate::syntax) fn new<O, L, R>(op: O, lhs: L, rhs: R) -> Self
where
O: Into<op::BinOp>,
L: Into<Node>,
R: Into<Node>,
{
Self {
op: op.into(),
lhs: Box::new(lhs.into()),
rhs: Box::new(rhs.into()),
}
}
pub fn op(&self) -> op::BinOp {
self.op
}
pub fn lhs(&self) -> &Node {
&self.lhs
}
pub fn rhs(&self) -> &Node {
&self.rhs
}
}
impl ToInternedString for BinOp {
fn to_interned_string(&self, interner: &Interner) -> String {
format!(
"{} {} {}",
self.lhs.to_interned_string(interner),
self.op,
self.rhs.to_interned_string(interner)
)
}
}
impl From<BinOp> for Node {
fn from(op: BinOp) -> Self {
Self::BinOp(op)
}
}