1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
use crate::syntax::ast::node::Node;
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 ConditionalOp {
condition: Box<Node>,
if_true: Box<Node>,
if_false: Box<Node>,
}
impl ConditionalOp {
pub fn cond(&self) -> &Node {
&self.condition
}
pub fn if_true(&self) -> &Node {
&self.if_true
}
pub fn if_false(&self) -> &Node {
&self.if_false
}
pub fn new<C, T, F>(condition: C, if_true: T, if_false: F) -> Self
where
C: Into<Node>,
T: Into<Node>,
F: Into<Node>,
{
Self {
condition: Box::new(condition.into()),
if_true: Box::new(if_true.into()),
if_false: Box::new(if_false.into()),
}
}
}
impl ToInternedString for ConditionalOp {
fn to_interned_string(&self, interner: &Interner) -> String {
format!(
"{} ? {} : {}",
self.cond().to_interned_string(interner),
self.if_true().to_interned_string(interner),
self.if_false().to_interned_string(interner)
)
}
}
impl From<ConditionalOp> for Node {
fn from(cond_op: ConditionalOp) -> Self {
Self::ConditionalOp(cond_op)
}
}