1use std::sync::Arc;
7
8use crate::{CallbackFn, EvalContextFamily, PathAccessor, Value};
9
10#[derive(Debug, Clone, PartialEq, Copy)]
16pub enum CompOp {
17 Eq,
18 NotEq,
19 Less,
20 Greater,
21 LessEq,
22 GreaterEq,
23}
24
25#[derive(Debug, Clone, Copy)]
27pub enum MathOp {
28 Add,
29 Sub,
30 Mul,
31 Div,
32}
33
34#[derive(Debug, Clone)]
36pub enum IndexExpr {
37 String(String),
39 Int(usize),
41}
42
43#[derive(Debug, Clone)]
52pub struct Field {
53 pub name: String,
55 pub keys: Vec<IndexExpr>,
57}
58
59#[derive(Debug, Clone, Copy, PartialEq, Eq)]
68pub struct BoolExprRef(pub(crate) u32);
69
70#[derive(Debug, Clone, Copy, PartialEq, Eq)]
72pub struct MathExprRef(pub(crate) u32);
73
74#[derive(Debug, Clone, Copy, PartialEq, Eq)]
76pub struct ValueExprRef(pub(crate) u32);
77
78#[derive(Debug, Clone, Copy, PartialEq, Eq)]
80pub struct FunctionCallRef(pub(crate) u32);
81
82pub struct ResolvedPath<F: EvalContextFamily> {
90 pub fields: Vec<Field>,
92 pub accessor: Arc<dyn PathAccessor<F>>,
94}
95
96impl<F: EvalContextFamily> Clone for ResolvedPath<F> {
97 fn clone(&self) -> Self {
98 Self {
99 fields: self.fields.clone(),
100 accessor: self.accessor.clone(),
101 }
102 }
103}
104
105impl<F: EvalContextFamily> std::fmt::Debug for ResolvedPath<F> {
106 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
107 f.debug_struct("ResolvedPath").field("fields", &self.fields).finish()
108 }
109}
110
111#[derive(Debug)]
113pub enum ArenaBoolExpr<F: EvalContextFamily> {
114 Literal(bool),
115 Comparison {
116 left: ValueExprRef,
117 op: CompOp,
118 right: ValueExprRef,
119 },
120 Converter(FunctionCallRef),
121 Path(ResolvedPath<F>),
123 Not(BoolExprRef),
124 And(BoolExprRef, BoolExprRef),
125 Or(BoolExprRef, BoolExprRef),
126}
127
128impl<F: EvalContextFamily> Clone for ArenaBoolExpr<F> {
129 fn clone(&self) -> Self {
130 match self {
131 Self::Literal(b) => Self::Literal(*b),
132 Self::Comparison { left, op, right } => Self::Comparison {
133 left: *left,
134 op: *op,
135 right: *right,
136 },
137 Self::Converter(r) => Self::Converter(*r),
138 Self::Path(p) => Self::Path(p.clone()),
139 Self::Not(r) => Self::Not(*r),
140 Self::And(l, r) => Self::And(*l, *r),
141 Self::Or(l, r) => Self::Or(*l, *r),
142 }
143 }
144}
145
146#[derive(Debug, Clone)]
148pub enum ArenaMathExpr {
149 Primary(ValueExprRef),
150 Negate(MathExprRef),
151 Binary {
152 left: MathExprRef,
153 op: MathOp,
154 right: MathExprRef,
155 },
156}
157
158#[derive(Debug)]
160pub enum ArenaValueExpr<F: EvalContextFamily> {
161 Literal(Value),
162 Path(ResolvedPath<F>),
164 List(Vec<ValueExprRef>),
165 Map(Vec<(String, ValueExprRef)>),
166 FunctionCall(FunctionCallRef),
167 Math(MathExprRef),
168}
169
170impl<F: EvalContextFamily> Clone for ArenaValueExpr<F> {
171 fn clone(&self) -> Self {
172 match self {
173 Self::Literal(v) => Self::Literal(v.clone()),
174 Self::Path(p) => Self::Path(p.clone()),
175 Self::List(l) => Self::List(l.clone()),
176 Self::Map(m) => Self::Map(m.clone()),
177 Self::FunctionCall(r) => Self::FunctionCall(*r),
178 Self::Math(r) => Self::Math(*r),
179 }
180 }
181}
182
183pub struct ArenaFunctionCall {
185 pub name: String,
186 pub is_editor: bool,
187 pub args: Vec<ArenaArgExpr>,
188 pub indexes: Vec<IndexExpr>,
189 pub callback: Option<CallbackFn>,
190}
191
192impl Clone for ArenaFunctionCall {
193 fn clone(&self) -> Self {
194 Self {
195 name: self.name.clone(),
196 is_editor: self.is_editor,
197 args: self.args.clone(),
198 indexes: self.indexes.clone(),
199 callback: self.callback.clone(),
200 }
201 }
202}
203
204impl std::fmt::Debug for ArenaFunctionCall {
205 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
206 f.debug_struct("ArenaFunctionCall")
207 .field("name", &self.name)
208 .field("is_editor", &self.is_editor)
209 .field("args", &self.args)
210 .field("indexes", &self.indexes)
211 .field("callback", &self.callback.is_some())
212 .finish()
213 }
214}
215
216#[derive(Debug, Clone)]
218pub enum ArenaArgExpr {
219 Positional(ValueExprRef),
220 Named { name: String, value: ValueExprRef },
221}
222
223#[derive(Debug, Clone)]
225pub struct ArenaEditorStatement {
226 pub editor: FunctionCallRef,
227 pub condition: Option<BoolExprRef>,
228}
229
230#[derive(Debug, Clone)]
232pub enum ArenaRootExpr {
233 EditorStatement(ArenaEditorStatement),
234 BooleanExpression(BoolExprRef),
235 MathExpression(MathExprRef),
236}
237
238#[derive(Debug, Clone)]
246pub struct PathExpr {
247 pub fields: Vec<Field>,
249}
250
251#[derive(Clone)]
253pub struct FunctionCall {
254 pub name: String,
256 pub is_editor: bool,
258 pub args: Vec<ArgExpr>,
260 pub indexes: Vec<IndexExpr>,
262 pub callback: Option<CallbackFn>,
264}
265
266impl std::fmt::Debug for FunctionCall {
267 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
268 f.debug_struct("FunctionCall")
269 .field("name", &self.name)
270 .field("is_editor", &self.is_editor)
271 .field("args", &self.args)
272 .field("indexes", &self.indexes)
273 .field("callback", &self.callback.is_some())
274 .finish()
275 }
276}
277
278#[derive(Debug, Clone)]
280pub enum ArgExpr {
281 Positional(ValueExpr),
283 Named { name: String, value: ValueExpr },
285}
286
287#[derive(Debug, Clone)]
289pub enum ValueExpr {
290 Literal(Value),
292 Path(PathExpr),
294 List(Vec<ValueExpr>),
296 Map(Vec<(String, ValueExpr)>),
298 FunctionCall(Box<FunctionCall>),
300 Math(Box<MathExpr>),
302}
303
304#[derive(Debug, Clone)]
306pub enum MathExpr {
307 Primary(ValueExpr),
309 Negate(Box<MathExpr>),
311 Binary {
313 left: Box<MathExpr>,
314 op: MathOp,
315 right: Box<MathExpr>,
316 },
317}
318
319#[derive(Debug, Clone)]
321pub enum BoolExpr {
322 Literal(bool),
324 Comparison {
326 left: ValueExpr,
327 op: CompOp,
328 right: ValueExpr,
329 },
330 Converter(Box<FunctionCall>),
332 Path(PathExpr),
334 Not(Box<BoolExpr>),
336 And(Box<BoolExpr>, Box<BoolExpr>),
338 Or(Box<BoolExpr>, Box<BoolExpr>),
340}
341
342#[derive(Debug, Clone)]
344pub struct EditorStatement {
345 pub editor: FunctionCall,
347 pub condition: Option<BoolExpr>,
349}
350
351#[derive(Debug, Clone)]
353pub enum RootExpr {
354 EditorStatement(EditorStatement),
356 BooleanExpression(BoolExpr),
358 MathExpression(MathExpr),
360}