#[repr(C, align(8))]pub enum AstExpr<'arena> {
Constant(Real),
Variable(&'arena str),
Function {
name: &'arena str,
args: &'arena [AstExpr<'arena>],
},
Array {
name: &'arena str,
index: &'arena AstExpr<'arena>,
},
Attribute {
base: &'arena str,
attr: &'arena str,
},
LogicalOp {
op: LogicalOperator,
left: &'arena AstExpr<'arena>,
right: &'arena AstExpr<'arena>,
},
Conditional {
condition: &'arena AstExpr<'arena>,
true_branch: &'arena AstExpr<'arena>,
false_branch: &'arena AstExpr<'arena>,
},
}Expand description
Abstract Syntax Tree (AST) node representing an expression.
The AST is the core data structure used for representing parsed expressions. Each variant of this enum represents a different type of expression node, forming a tree structure that can be evaluated to produce a result.
This type uses arena allocation for all strings and recursive structures, eliminating all dynamic allocations during evaluation.
Using repr(C) with explicit discriminant type and alignment to avoid ARM alignment issues
Variants§
Constant(Real)
A literal numerical value.
Examples: 3.14, 42, -1.5
Variable(&'arena str)
A named variable reference.
Examples: x, temperature, result
Function
A function call with a name and list of argument expressions.
Examples: sin(x), max(a, b), sqrt(x*x + y*y)
Fields
Array
An array element access.
Examples: array[0], values[i+1]
Fields
Attribute
An attribute access on an object.
Examples: point.x, settings.value
LogicalOp
A logical operation with short-circuit evaluation.
Represents logical AND (&&) and OR (||) operations with short-circuit behavior.
Unlike function-based operators, these operators have special evaluation semantics
where the right operand may not be evaluated based on the value of the left operand.
§Examples
a && b: Evaluatesa, then evaluatesbonly ifais non-zero (true)c || d: Evaluatesc, then evaluatesdonly ifcis zero (false)x > 0 && y < 10: Checks if both conditions are true, with short-circuitflag || calculate_value(): Skips calculation if flag is true
§Boolean Logic
The engine represents boolean values as floating-point numbers:
0.0is consideredfalse- Any non-zero value is considered
true, typically1.0is used
§Operator Precedence
&& has higher precedence than ||, consistent with most programming languages:
a || b && cis interpreted asa || (b && c)- Use parentheses to override default precedence:
(a || b) && c
Fields
op: LogicalOperatorThe logical operator (AND or OR)
Conditional
A ternary conditional operation (condition ? true_expr : false_expr).
Represents a conditional expression with three parts: a condition to evaluate, an expression to return if the condition is true, and an expression to return if the condition is false. This uses short-circuit evaluation, meaning only the relevant branch is evaluated.
§Examples
x > 0 ? 1 : -1: Returns 1 if x is positive, -1 otherwiseflag ? value1 : value2: Chooses between two values based on flaga > b ? a : b: Returns the maximum of a and b
§Boolean Logic
Like logical operations, the ternary operator uses floating-point values for boolean logic:
0.0is consideredfalse- Any non-zero value is considered
true
§Short-Circuit Evaluation
Only one branch is evaluated based on the condition result:
- If condition is non-zero (true), only the true_branch is evaluated
- If condition is zero (false), only the false_branch is evaluated
§Operator Precedence
The ternary operator has low precedence:
a + b ? c : d * eis interpreted as(a + b) ? c : (d * e)- Use parentheses for clarity when nesting operations
Implementations§
Source§impl<'arena> AstExpr<'arena>
impl<'arena> AstExpr<'arena>
Sourcepub fn pow(&self, exp: Real) -> Real
pub fn pow(&self, exp: Real) -> Real
Helper method that raises a constant expression to a power.
This is primarily used in testing to evaluate power operations on constants. For non-constant expressions, it returns 0.0 as a default value.
§Parameters
exp- The exponent to raise the constant to
§Returns
The constant raised to the given power, or 0.0 for non-constant expressions