rsx_compiler/ast/
expressions.rs1use super::*;
2use crate::{SourceId, SourceSpan};
3
4#[derive(Debug, Clone, PartialEq)]
5pub enum ExpressionNode {
6 Binary(Box<BinaryExpression>),
8 Group(Box<ExpressionNode>),
10}
11
12impl GenerateTs for ExpressionNode {
13 fn generate_ts(&self, context: &mut TsGenerator) -> Result<()> {
14 match self {
15 ExpressionNode::Binary(node) => node.generate_ts(context),
16 ExpressionNode::Group(node) => node.generate_ts(context),
17 }
18 }
19}
20
21#[derive(Debug, Clone, PartialEq)]
22pub struct BinaryExpression {
23 pub span: SourceSpan,
24 pub source_id: SourceId,
25 pub operator: BinaryOperator,
26 pub lhs: ExpressionNode,
27 pub rhs: ExpressionNode,
28}
29impl GenerateTs for BinaryExpression {
30 fn generate_ts(&self, context: &mut TsGenerator) -> Result<()> {
31 self.lhs.generate_ts(context)?;
33 context.append_mapping(
35 &format!(" {} ", self.operator),
36 self.span.clone(),
37 &self.source_id,
38 )?;
39 self.rhs.generate_ts(context)?;
40 Ok(())
41 }
42}
43#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
47pub enum BinaryOperator {
48 Add, Subtract, Multiply, Divide, GreaterThan, LessThan, StrictEquals, }
57
58#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
60pub enum LiteralKind {
61 String(ArcStr),
62 Number(f64),
63 Boolean(bool),
64 Null,
65 }
67
68impl std::fmt::Display for BinaryOperator {
69 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
70 match self {
71 BinaryOperator::Add => write!(f, "+"),
72 BinaryOperator::Subtract => write!(f, "-"),
73 BinaryOperator::Multiply => write!(f, "*"),
74 BinaryOperator::Divide => write!(f, "/"),
75 BinaryOperator::GreaterThan => write!(f, ">"),
76 BinaryOperator::LessThan => write!(f, "<"),
77 BinaryOperator::StrictEquals => write!(f, "==="),
78 }
79 }
80}
81#[derive(Debug, Clone, PartialEq)]
82pub struct Identifier {
83 pub name: ArcStr,
84 pub source_id: SourceId,
85 pub span: SourceSpan,
86}
87
88#[derive(Debug, Clone, PartialEq)]
89pub struct Literal {
90 pub span: SourceSpan,
91 pub source_id: SourceId,
92 pub kind: LiteralKind,
93}
94impl GenerateTs for Identifier {
95 fn generate_ts(&self, context: &mut TsGenerator) -> Result<()> {
96 context.append_mapping(&self.name, self.span, &self.source_id)
97 }
98}
99
100impl GenerateTs for Literal {
101 fn generate_ts(&self, context: &mut TsGenerator) -> Result<()> {
102 let literal_str = match &self.kind {
103 LiteralKind::String(s) => format!("\"{}\"", s.escape_default()), LiteralKind::Number(n) => n.to_string(),
105 LiteralKind::Boolean(b) => b.to_string(),
106 LiteralKind::Null => "null".to_string(),
107 };
108 context.append_mapping(&literal_str, self.span.clone(), &self.source_id)
109 }
110}