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