blots_core/
ast.rs

1use crate::functions::BuiltInFunction;
2use crate::values::LambdaArg;
3use serde::{Deserialize, Serialize};
4
5#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
6pub enum Expr {
7    // Literals
8    Number(f64),
9    String(String),
10    Bool(bool),
11    Null,
12
13    // Variables and functions
14    Identifier(String),
15    InputReference(String), // Shorthand for inputs.field (e.g., #field)
16    BuiltIn(BuiltInFunction), // Built-in function
17
18    // Collections
19    List(Vec<Expr>),
20    Record(Vec<RecordEntry>),
21
22    // Lambda
23    Lambda {
24        args: Vec<LambdaArg>,
25        body: Box<Expr>,
26    },
27
28    // Control flow
29    Conditional {
30        condition: Box<Expr>,
31        then_expr: Box<Expr>,
32        else_expr: Box<Expr>,
33    },
34
35    DoBlock {
36        statements: Vec<DoStatement>,
37        return_expr: Box<Expr>,
38    },
39
40    // Operations
41    Assignment {
42        ident: String,
43        value: Box<Expr>,
44    },
45
46    Call {
47        func: Box<Expr>,
48        args: Vec<Expr>,
49    },
50
51    Access {
52        expr: Box<Expr>,
53        index: Box<Expr>,
54    },
55
56    DotAccess {
57        expr: Box<Expr>,
58        field: String,
59    },
60
61    BinaryOp {
62        op: BinaryOp,
63        left: Box<Expr>,
64        right: Box<Expr>,
65    },
66
67    UnaryOp {
68        op: UnaryOp,
69        expr: Box<Expr>,
70    },
71
72    PostfixOp {
73        op: PostfixOp,
74        expr: Box<Expr>,
75    },
76
77    // Special
78    Spread(Box<Expr>),
79}
80
81#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
82pub struct RecordEntry {
83    pub key: RecordKey,
84    pub value: Expr,
85}
86
87#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
88pub enum RecordKey {
89    Static(String),
90    Dynamic(Box<Expr>),
91    Shorthand(String),
92    Spread(Box<Expr>),
93}
94
95#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
96pub enum DoStatement {
97    Expression(Expr),
98    Comment(String),
99}
100
101#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
102pub enum BinaryOp {
103    // Arithmetic
104    Add,
105    Subtract,
106    Multiply,
107    Divide,
108    Modulo,
109    Power,
110
111    // Comparison (with broadcasting)
112    Equal,
113    NotEqual,
114    Less,
115    LessEq,
116    Greater,
117    GreaterEq,
118
119    // Comparison (without broadcasting)
120    DotEqual,
121    DotNotEqual,
122    DotLess,
123    DotLessEq,
124    DotGreater,
125    DotGreaterEq,
126
127    // Logical
128    And,
129    NaturalAnd,
130    Or,
131    NaturalOr,
132
133    // Special
134    Via,
135    Into,
136    Coalesce,
137}
138
139#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
140pub enum UnaryOp {
141    Negate,
142    Not,
143    Invert,
144}
145
146#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
147pub enum PostfixOp {
148    Factorial,
149}