Function evaluate_and_promote

Source
pub fn evaluate_and_promote(
    context: &mut dyn ContextLike,
    left: &dyn ExpressionLike,
    right: &dyn ExpressionLike,
) -> Result<(Value, Value)>
Expand description

Helper function to evaluate two expressions and promote the right operand to match the type of the left operand.

This function implements AIMX’s type promotion system for binary operators. It evaluates both operands, then promotes the right operand’s type to match the left operand’s type according to AIMX grammar rules.

§Type Promotion Rules

The type promotion follows these rules:

  • If the left operand is Bool, the right operand is converted to boolean
  • If the left operand is Date, the right operand is converted to date
  • If the left operand is Number, the right operand is converted to number
  • If the left operand is Task, the right operand is converted to task
  • If the left operand is Text, the right operand is converted to text

§Usage

This function is primarily used internally by binary operator implementations (arithmetic, logical, relational, etc.) to ensure type compatibility.

§Arguments

  • context - The evaluation context used for evaluating both operands
  • left - The left operand expression
  • right - The right operand expression

§Returns

Returns a Result containing a tuple of (Value, Value) where:

  • The first value is the evaluated left operand
  • The second value is the evaluated right operand, promoted to match the left operand’s type

§Errors

This function can return errors if:

  • Either operand fails to evaluate
  • The right operand cannot be promoted to match the left operand’s type

§Examples

use aimx::{aimx_parse, evaluate::evaluate_and_promote, Context};

let mut context = Context::new();
let left_expr = aimx_parse("5");
let right_expr = aimx_parse("\"3\"");
 
let (left_val, right_val) = evaluate_and_promote(
    &mut context,
    &left_expr,
    &right_expr
).unwrap();
 
// The string "3" is promoted to number 3.0
assert!(left_val.is_number());
assert!(right_val.is_number());
assert_eq!(right_val.to_number().unwrap(), 3.0);