1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
use std::{borrow::Cow, collections::BTreeMap, fmt::Debug};
use nom::Finish;
#[cfg(feature = "serde_json")]
use serde::Deserialize;
use crate::{
Env, JsonValue, ParseError,
errors::EvalError,
parser::parse_exp,
types::{func::FunctionItem, value::Value, var::VarAccess},
};
/// Represents an Abstract Syntax Tree (AST) for sosaku expressions,
/// which can be evaluated in a given environment to produce a literal value.
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub enum Exp<'a> {
Literal(Value<'a>),
FnCall(FunctionItem<'a>),
Var(VarAccess),
Neg(Box<Self>),
Or(Box<Self>, Box<Self>),
And(Box<Self>, Box<Self>),
Eq(Box<Self>, Box<Self>),
Neq(Box<Self>, Box<Self>),
Gt(Box<Self>, Box<Self>),
Lt(Box<Self>, Box<Self>),
Geq(Box<Self>, Box<Self>),
Leq(Box<Self>, Box<Self>),
Array(Vec<Self>),
Object(BTreeMap<String, Self>),
}
impl<'exp> Exp<'exp> {
/// Create a new [`Exp`] from a string representation of an expression.
///
/// # Parameters
///
/// - `string`: The string representation of the expression to parse.
///
/// # Returns
///
/// - <code>Ok([`Exp`])</code> if the expression was successfully parsed from the string.
///
/// # Errors
///
/// - If there was an error parsing the expression from the string,
/// such as a syntax error, an `Err` will be returned containing the parsing error details.
///
/// Note that semantic errors (e.g. undefined variables, type errors) are not handled by this
/// function and will not result in an error being returned here. Those errors will be encountered
/// during evaluation of the expression, and will be returned as [`EvalError`]s from the [`Exp::eval`] method.
pub fn new(string: impl Into<&'exp str>) -> Result<Self, ParseError> {
string.into().try_into()
}
/// Turn the expression into an owned version, where all borrowed data is cloned into owned data.
///
/// This is useful for cases where you want to take ownership of an [`Exp`] that may contain
/// borrowed data (e.g. from a JSON value) and ensure that it is fully owned and independent of any original data sources.
///
/// Note that this will recursively clone all borrowed data in the expression, so it may be expensive for large expressions with a lot of borrowed data.
/// However, if the expression is already fully owned, this will simply return a clone of the expression without any additional cloning of data.
///
/// # Returns
///
/// - An owned version of this expression, where all borrowed data has been cloned into owned data.
pub fn into_owned(self) -> Exp<'static> {
match self {
Exp::Literal(lit) => Exp::Literal(lit.into_owned()),
Exp::FnCall(func) => Exp::FnCall(FunctionItem::new(
func.name().to_string(),
func.args()
.iter()
.map(|e| e.clone().into_owned())
.collect::<Vec<_>>(),
)),
Exp::Var(var) => Exp::Var(var),
Exp::Neg(e) => Exp::Neg(Box::new(e.into_owned())),
Exp::Or(l, r) => Exp::Or(Box::new(l.into_owned()), Box::new(r.into_owned())),
Exp::And(l, r) => Exp::And(Box::new(l.into_owned()), Box::new(r.into_owned())),
Exp::Eq(l, r) => Exp::Eq(Box::new(l.into_owned()), Box::new(r.into_owned())),
Exp::Neq(l, r) => Exp::Neq(Box::new(l.into_owned()), Box::new(r.into_owned())),
Exp::Gt(l, r) => Exp::Gt(Box::new(l.into_owned()), Box::new(r.into_owned())),
Exp::Lt(l, r) => Exp::Lt(Box::new(l.into_owned()), Box::new(r.into_owned())),
Exp::Geq(l, r) => Exp::Geq(Box::new(l.into_owned()), Box::new(r.into_owned())),
Exp::Leq(l, r) => Exp::Leq(Box::new(l.into_owned()), Box::new(r.into_owned())),
Exp::Array(elems) => Exp::Array(elems.into_iter().map(Exp::into_owned).collect()),
Exp::Object(map) => {
Exp::Object(map.into_iter().map(|(k, v)| (k, v.into_owned())).collect())
}
}
}
/// Evaluate the expression in the given environment and return the resulting literal value.
///
/// If you added async functions to your environment, you should use the [`Exp::eval_async`] method instead,
/// which will properly await any async functions during evaluation.
///
/// ## Parameters
///
/// - `env`: The [`Env`] to evaluate the expression in, which contains variable bindings and function definitions.
///
/// ## Returns
///
/// - <code>Ok([Cow]<'_, [Literal]>)</code> if the expression was successfully evaluated, where the `Literal` is the resulting value of the expression.
///
/// ## Errors
///
/// If there was an error during evaluation, such as a type error or undefined variable, an [`EvalError`] will be returned.
pub fn eval<'var, 'out, V: JsonValue + Clone + Debug>(
&'exp self,
env: &'var Env<'var, '_, V>,
) -> Result<Cow<'out, Value<'out>>, EvalError>
where
'exp: 'out,
'var: 'out,
{
crate::interpreter::eval(self, env)
}
/// Evaluate the expression in the given environment and return the resulting literal value.
/// Allows for the use of async functions in the expression, which will be awaited during evaluation.
///
/// Note that this does **NOT** make the entire evaluation process asynchronous - only the execution of async functions will be asynchronous.
/// The overall evaluation will still be performed in a single pass, and the resulting value will be returned once all async functions have
/// been awaited and their results incorporated into the final result.
///
/// ## Parameters
///
/// - `env`: The [`Env`] to evaluate the expression in, which contains variable bindings and function definitions.
///
/// ## Returns
///
/// - <code>Ok([Cow]<'_, [Literal]>)</code> if the expression was successfully evaluated, where the `Literal` is the resulting value of the expression.
///
/// ## Errors
///
/// If there was an error during evaluation, such as a type error or undefined variable, an [`EvalError`] will be returned.
pub async fn eval_async<'var, 'out, V: JsonValue + Clone + Debug>(
&'exp self,
env: &'var Env<'var, '_, V>,
) -> Result<Cow<'out, Value<'out>>, EvalError>
where
'exp: 'out,
'var: 'out,
{
crate::interpreter::eval_async(self, env).await
}
/// Create a new [`Exp`] representing a literal value.
///
/// ## Parameters
///
/// - `lit`: The literal value to create an expression for.
///
/// ## Returns
///
/// - An [`Exp`] enum representing the literal value.
#[inline]
pub const fn literal(lit: Value<'exp>) -> Self {
Self::Literal(lit)
}
/// Create a new [`Exp`] representing a variable access.
///
/// ## Parameters
///
/// - `accessor`: The variable access to create an expression for.
///
/// ## Returns
///
/// - An [`Exp`] enum representing the variable access.
#[inline]
pub const fn var(accessor: VarAccess) -> Self {
Self::Var(accessor)
}
/// Create a new [`Exp`] representing a function call.
///
/// ## Parameters
///
/// - `accessor`: The variable access syntax, e.g. "foo.bar[0].baz"
///
/// ## Returns
///
/// - An [`Exp`] enum representing the function call.
///
/// ## Errors
///
/// - If the variable access syntax is invalid
#[inline]
pub fn varname(accessor: &str) -> Result<Self, nom::error::Error<&str>> {
VarAccess::try_from(accessor).map(Self::var)
}
/// Create a new [`Exp`] representing an array literal.
///
/// ## Parameters
///
/// - `elems`: The elements of the array, represented as a vector of [`Exp`] expressions.
///
/// ## Returns
///
/// - An [`Exp`] enum representing the array literal.
#[inline]
pub const fn array(elems: Vec<Self>) -> Self {
Self::Array(elems)
}
/// Create a new [`Exp`] representing an object literal.
///
/// ## Parameters
///
/// - `map`: The key-value pairs of the object, represented as a `BTreeMap`
/// where the key is a string and the value is an [`Exp`] expression.
///
/// ## Returns
///
/// - An [`Exp`] enum representing the object literal.
#[inline]
pub const fn object(map: BTreeMap<String, Self>) -> Self {
Self::Object(map)
}
/// Create a new [`Exp`] representing a function call.
///
/// ## Parameters
///
/// - `func`: The function to call, which includes the function name and its arguments.
///
/// ## Returns
///
/// - An [`Exp`] enum representing the function call.
#[inline]
pub const fn fn_call(func: FunctionItem<'exp>) -> Self {
Self::FnCall(func)
}
/// Create a new [`Exp`] representing a negation of another expression.
///
/// ## Parameters
///
/// - `exp`: The expression to negate.
///
/// ## Returns
///
/// - An [`Exp`] enum representing the negation of the given expression.
#[inline]
#[expect(clippy::should_implement_trait)]
pub fn neg(exp: Self) -> Self {
Self::Neg(Box::new(exp))
}
/// Create a new [`Exp`] representing a logical OR of two expressions.
///
/// ## Parameters
///
/// - `lhs`: The left-hand side expression of the OR operation.
/// - `rhs`: The right-hand side expression of the OR operation.
///
/// ## Returns
///
/// - An [`Exp`] enum representing the logical OR of the two given expressions.
#[inline]
pub fn or(lhs: Self, rhs: Self) -> Self {
Self::Or(Box::new(lhs), Box::new(rhs))
}
/// Create a new [`Exp`] representing a logical AND of two expressions.
///
/// ## Parameters
///
/// - `lhs`: The left-hand side expression of the AND operation.
/// - `rhs`: The right-hand side expression of the AND operation.
///
/// ## Returns
/// - An [`Exp`] enum representing the logical AND of the two given expressions.
#[inline]
pub fn and(lhs: Self, rhs: Self) -> Self {
Self::And(Box::new(lhs), Box::new(rhs))
}
/// Create a new [`Exp`] representing an equality comparison of two expressions.
///
/// ## Parameters
///
/// - `lhs`: The left-hand side expression of the equality comparison.
/// - `rhs`: The right-hand side expression of the equality comparison.
///
/// ## Returns
///
/// - An [`Exp`] enum representing the equality comparison of the two given expressions.
#[inline]
pub fn eq(lhs: Self, rhs: Self) -> Self {
Self::Eq(Box::new(lhs), Box::new(rhs))
}
/// Create a new [`Exp`] representing an inequality comparison of two expressions.
///
/// ## Parameters
///
/// - `lhs`: The left-hand side expression of the inequality comparison.
/// - `rhs`: The right-hand side expression of the inequality comparison.
///
/// ## Returns
///
/// - An [`Exp`] enum representing the inequality comparison of the two given expressions.
#[inline]
pub fn neq(lhs: Self, rhs: Self) -> Self {
Self::Neq(Box::new(lhs), Box::new(rhs))
}
/// Create a new [`Exp`] representing a greater-than comparison of two expressions.
///
/// ## Parameters
///
/// - `lhs`: The left-hand side expression of the greater-than comparison.
/// - `rhs`: The right-hand side expression of the greater-than comparison.
///
/// ## Returns
///
/// - An [`Exp`] enum representing the greater-than comparison of the two given expressions.
#[inline]
pub fn gt(lhs: Self, rhs: Self) -> Self {
Self::Gt(Box::new(lhs), Box::new(rhs))
}
/// Create a new [`Exp`] representing a less-than comparison of two expressions.
///
/// ## Parameters
/// - `lhs`: The left-hand side expression of the less-than comparison.
/// - `rhs`: The right-hand side expression of the less-than comparison.
///
/// ## Returns
/// - An [`Exp`] enum representing the less-than comparison of the two given expressions.
#[inline]
pub fn lt(lhs: Self, rhs: Self) -> Self {
Self::Lt(Box::new(lhs), Box::new(rhs))
}
/// Create a new [`Exp`] representing a greater-than-or-equal-to comparison of two expressions.
///
/// ## Parameters
/// - `lhs`: The left-hand side expression of the greater-than-or-equal-to comparison.
/// - `rhs`: The right-hand side expression of the greater-than-or-equal-to comparison.
///
/// ## Returns
/// - An [`Exp`] enum representing the greater-than-or-equal-to comparison of the two given expressions.
#[inline]
pub fn geq(lhs: Self, rhs: Self) -> Self {
Self::Geq(Box::new(lhs), Box::new(rhs))
}
/// Create a new [`Exp`] representing a less-than-or-equal-to comparison of two expressions.
///
/// ## Parameters
/// - `lhs`: The left-hand side expression of the less-than-or-equal-to comparison.
/// - `rhs`: The right-hand side expression of the less-than-or-equal-to comparison.
///
/// ## Returns
/// - An [`Exp`] enum representing the less-than-or-equal-to comparison of the two given expressions.
#[inline]
pub fn leq(lhs: Self, rhs: Self) -> Self {
Self::Leq(Box::new(lhs), Box::new(rhs))
}
}
impl<'a> TryFrom<&'a str> for Exp<'a> {
type Error = nom::error::Error<String>;
fn try_from(value: &'a str) -> Result<Self, Self::Error> {
let (remainder, exp) = parse_exp(value).finish()?;
if !remainder.trim().is_empty() {
return Err(nom::error::Error::new(
remainder.to_string(),
nom::error::ErrorKind::Eof,
));
}
Ok(exp)
}
}
#[cfg(feature = "serde_json")]
impl<'de> Deserialize<'de> for Exp<'de> {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let s = <&str>::deserialize(deserializer)?;
Self::try_from(s).map_err(serde::de::Error::custom)
}
}