pub struct Expr { /* private fields */ }Expand description
A parsed expression that can be evaluated or inspected.
Expr is the main entry point for the expression language. Parse a string
with Expr::parse(), then either evaluate it with Expr::eval() or
inspect the AST with Expr::ast().
§Example
use wick_core::Expr;
use std::collections::HashMap;
// Parse an expression
let expr = Expr::parse("x^2 + 2*x + 1").unwrap();
// Evaluate with different variable values
let mut vars = HashMap::new();
vars.insert("x".to_string(), 3.0);
assert_eq!(expr.eval(&vars).unwrap(), 16.0); // 9 + 6 + 1
vars.insert("x".to_string(), 0.0);
assert_eq!(expr.eval(&vars).unwrap(), 1.0); // 0 + 0 + 1Implementations§
Source§impl Expr
impl Expr
Sourcepub fn parse(input: &str) -> Result<Self, ParseError>
pub fn parse(input: &str) -> Result<Self, ParseError>
Parses an expression from a string.
§Errors
Returns ParseError if the input is not a valid expression:
ParseError::UnexpectedCharfor invalid charactersParseError::UnexpectedEndfor incomplete expressionsParseError::UnexpectedTokenfor syntax errorsParseError::InvalidNumberfor malformed numeric literals
§Example
use wick_core::{Expr, ParseError};
// Valid expression
assert!(Expr::parse("1 + 2").is_ok());
// Invalid: unexpected character
assert!(matches!(Expr::parse("1 @ 2"), Err(ParseError::UnexpectedChar('@'))));
// Invalid: incomplete expression
assert!(matches!(Expr::parse("1 +"), Err(ParseError::UnexpectedEnd)));Sourcepub fn ast(&self) -> &Ast
pub fn ast(&self) -> &Ast
Returns a reference to the parsed AST.
Use this to inspect the expression structure or to compile it to a different target (WGSL, Lua, etc.).
Sourcepub fn free_vars(&self) -> HashSet<&str>
pub fn free_vars(&self) -> HashSet<&str>
Returns the set of free variables referenced in the expression.
This is useful for determining which variables need to be provided at evaluation time, or for building dependency graphs.
§Example
use wick_core::Expr;
let expr = Expr::parse("x * 2 + y").unwrap();
let vars = expr.free_vars();
assert!(vars.contains("x"));
assert!(vars.contains("y"));
assert_eq!(vars.len(), 2);Trait Implementations§
Auto Trait Implementations§
impl Freeze for Expr
impl RefUnwindSafe for Expr
impl Send for Expr
impl Sync for Expr
impl Unpin for Expr
impl UnwindSafe for Expr
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more