pub enum Ast {
Num(f64),
Var(String),
BinOp(BinOp, Box<Ast>, Box<Ast>),
UnaryOp(UnaryOp, Box<Ast>),
Let {
name: String,
value: Box<Ast>,
body: Box<Ast>,
},
}Expand description
Abstract syntax tree node for expressions.
The AST represents the structure of a parsed expression. Use Expr::ast()
to access the AST after parsing.
§Variants
The available variants depend on enabled features:
- Always:
Num,Var,BinOp,UnaryOp - With
func:Call - With
cond:Compare,And,Or,If
§Example
use wick_core::{Expr, Ast, BinOp};
let expr = Expr::parse("2 + 3").unwrap();
match expr.ast() {
Ast::BinOp(BinOp::Add, left, right) => {
assert!(matches!(left.as_ref(), Ast::Num(2.0)));
assert!(matches!(right.as_ref(), Ast::Num(3.0)));
}
_ => panic!("expected addition"),
}Variants§
Num(f64)
Numeric literal (e.g., 42, 3.14).
Var(String)
Variable reference, resolved at evaluation time.
BinOp(BinOp, Box<Ast>, Box<Ast>)
Binary operation: left op right.
UnaryOp(UnaryOp, Box<Ast>)
Unary operation: op operand.
Let
Local binding: let name = value; body.
Implementations§
Source§impl Ast
impl Ast
Sourcepub fn free_vars(&self) -> HashSet<&str>
pub fn free_vars(&self) -> HashSet<&str>
Returns the set of free variables referenced in this AST node.
Traverses the entire AST and collects all variable names.
§Example
use wick_core::{Expr, Ast};
let expr = Expr::parse("sin(x) + y * z").unwrap();
let vars = expr.ast().free_vars();
assert!(vars.contains("x"));
assert!(vars.contains("y"));
assert!(vars.contains("z"));Trait Implementations§
impl StructuralPartialEq for Ast
Auto Trait Implementations§
impl Freeze for Ast
impl RefUnwindSafe for Ast
impl Send for Ast
impl Sync for Ast
impl Unpin for Ast
impl UnwindSafe for Ast
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