Skip to main content

factorio_ir/
expression.rs

1use crate::{block::Block, literal::Literal, operator::Operator};
2
3#[derive(Debug, PartialEq, Clone)]
4pub enum Expression {
5    Literal(Literal),
6    Identifier(String),
7    QualifiedPath {
8        segments: Vec<String>,
9    },
10    FieldAccess {
11        base: Box<Self>,
12        field: String,
13    },
14    Call {
15        func: Box<Self>,
16        args: Vec<Self>,
17    },
18    MethodCall {
19        receiver: Box<Self>,
20        method: String,
21        args: Vec<Self>,
22    },
23    StructLiteral {
24        /// The Rust struct name that produced this literal, used by codegen to inject
25        /// fixed Factorio prototype fields (e.g. `type = "bool-setting"`).
26        struct_name: Option<String>,
27        fields: Vec<(String, Self)>,
28    },
29    /// Tagged enum value `{ tag = "Variant", ...payload }`.
30    EnumLiteral {
31        enum_name: String,
32        variant: String,
33        /// Named fields, or `_1` / `_2` / ... for tuple variants.
34        fields: Vec<(String, Self)>,
35    },
36    /// An operation between a `lhs` and a `rhs` with an [`Operator`]
37    BinaryOp {
38        lhs: Box<Self>,
39        op: Operator,
40        rhs: Box<Self>,
41    },
42    /// String interpolation parts joined with `..` in Lua.
43    FormatConcat {
44        parts: Vec<Self>,
45    },
46    /// Lua array literal `{ a, b, c }`.
47    Array {
48        elements: Vec<Self>,
49    },
50    /// Lua table index expression `base[key]`.
51    Index {
52        base: Box<Self>,
53        key: Box<Self>,
54    },
55    /// Logical `not EXPR` in Lua.
56    Not(Box<Self>),
57    /// Length operator `#EXPR` in Lua.
58    Len(Box<Self>),
59    /// Safe if-expression (avoids falsey `and`/`or` pitfalls).
60    If {
61        condition: Box<Self>,
62        then_expr: Box<Self>,
63        else_expr: Box<Self>,
64    },
65    /// Anonymous Lua function value (`function(params) ... end`).
66    Closure {
67        params: Vec<String>,
68        body: Block,
69    },
70}