factorio_ir/ast/
expression.rs1use crate::ast::{block::Block, literal::Literal, operator::Operator};
2
3#[derive(Debug, PartialEq, Eq, Clone, Copy, Default)]
4pub enum MethodDispatch {
5 #[default]
6 Infer,
7 Colon,
9 Factorio,
11 StorageGet,
13 StorageSet,
15 SettingsGet,
17}
18
19#[derive(Debug, PartialEq, Clone)]
20pub enum Expression {
21 Literal(Literal),
22 Identifier(String),
23 QualifiedPath {
24 segments: Vec<String>,
25 },
26 FieldAccess {
27 base: Box<Self>,
28 field: String,
29 },
30 Call {
31 func: Box<Self>,
32 args: Vec<Self>,
33 },
34 MethodCall {
35 receiver: Box<Self>,
36 method: String,
37 args: Vec<Self>,
38 dispatch: MethodDispatch,
39 },
40 StructLiteral {
41 struct_name: Option<String>,
44 fields: Vec<(String, Self)>,
45 },
46 EnumLiteral {
48 enum_name: String,
49 variant: String,
50 fields: Vec<(String, Self)>,
52 },
53 BinaryOp {
55 lhs: Box<Self>,
56 op: Operator,
57 rhs: Box<Self>,
58 },
59 FormatConcat {
61 parts: Vec<Self>,
62 },
63 Array {
65 elements: Vec<Self>,
66 },
67 Index {
69 base: Box<Self>,
70 key: Box<Self>,
71 },
72 Not(Box<Self>),
74 Len(Box<Self>),
76 If {
78 condition: Box<Self>,
79 then_expr: Box<Self>,
80 else_expr: Box<Self>,
81 },
82 Closure {
84 params: Vec<String>,
85 body: Block,
86 },
87 FatPointer {
89 data: Box<Self>,
90 vtable: String,
92 },
93 DynMethodCall {
95 receiver: Box<Self>,
96 method: String,
97 args: Vec<Self>,
98 },
99}
100
101impl Expression {
102 #[must_use]
104 pub fn method_call(receiver: Self, method: impl Into<String>, args: Vec<Self>) -> Self {
105 Self::MethodCall {
106 receiver: Box::new(receiver),
107 method: method.into(),
108 args,
109 dispatch: MethodDispatch::Infer,
110 }
111 }
112
113 #[must_use]
115 pub fn method_call_with(
116 receiver: Self,
117 method: impl Into<String>,
118 args: Vec<Self>,
119 dispatch: MethodDispatch,
120 ) -> Self {
121 Self::MethodCall {
122 receiver: Box::new(receiver),
123 method: method.into(),
124 args,
125 dispatch,
126 }
127 }
128}