rsx_compiler/ast/
expressions.rs

1use super::*;
2use crate::{SourceId, SourceSpan};
3
4#[derive(Debug, Clone, PartialEq)]
5pub enum ExpressionNode {
6    /// `1 + 2`
7    Binary(Box<BinaryExpression>),
8    /// `(expr)`
9    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        // TODO: Add parentheses based on operator precedence if needed
32        self.lhs.generate_ts(context)?;
33        // Map the operator to the start of the expression span for simplicity
34        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// --- Enums used by Nodes ---
44
45/// Different kinds of binary operators.
46#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
47pub enum BinaryOperator {
48    Add,         // +
49    Subtract,    // -
50    Multiply,    // *
51    Divide,      // /
52    GreaterThan, // >
53    LessThan,    // <
54    StrictEquals, // ===
55                 // Add other binary operators...
56}
57
58/// Different kinds of literals.
59#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
60pub enum LiteralKind {
61    String(ArcStr),
62    Number(f64),
63    Boolean(bool),
64    Null,
65    // Add other literal types...
66}
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()), // Basic string escaping
104            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}