AST

Struct AST 

Source
pub struct AST {
    pub root: Option<Node>,
}
Expand description

Represents a complete Abstract Syntax Tree

Fields§

§root: Option<Node>

Implementations§

Source§

impl AST

Source

pub fn new() -> Self

Examples found in repository?
examples/ast_tester.rs (line 122)
118fn test_manipulating_ast() {
119    debug!("Test: Manipulating an AST");
120
121    // Create an empty AST
122    let mut ast = AST::new();
123    trace!("Empty AST is_empty(): {}", ast.is_empty());
124
125    // Create and set a root node
126    let root_node = Node::new(MathOp::Add);
127    ast.set_root(root_node);
128    trace!("After setting root, is_empty(): {}", ast.is_empty());
129
130    // Verify we can access and modify the root
131    if let Some(root) = &mut ast.root {
132        root.add_child(Node::leaf(MathOp::Multiply));
133        root.add_child(Node::leaf(MathOp::Divide));
134        trace!("Added {} children to root", root.children.len());
135        trace!("Root is_leaf(): {}", root.is_leaf());
136    }
137
138    // Print the modified AST using Display trait
139    trace!("{}", ast);
140}
Source

pub fn with_root(root: Node) -> Self

Examples found in repository?
examples/ast_tester.rs (line 31)
13fn test_simple_binary_operation() {
14    debug!("Test: Simple binary operation '23 - 45'");
15
16    // Create token instances
17    let num_23 = Natural::from_n(23);
18    let subtract_op = MathOp::Subtract;
19    let num_45 = Natural::from_n(45);
20
21    // Create leaf nodes
22    let leaf_23 = Node::leaf(num_23);
23    let leaf_45 = Node::leaf(num_45);
24
25    // Create operation node
26    let mut op_node = Node::new(subtract_op);
27    op_node.add_child(leaf_23);
28    op_node.add_child(leaf_45);
29
30    // Create AST
31    let ast = AST::with_root(op_node);
32
33    // Print AST structure using Display trait
34    trace!("{}", ast);
35}
36
37/// Test 2: Binary operation with different number types "3.4 + 1"
38fn test_binary_operation_with_different_types() {
39    debug!("Test: Binary operation with different number types '3.4 + 1'");
40
41    // Create token instances
42    let real_3_4 = Real::from_n(3.4);
43    let add_op = MathOp::Add;
44    let num_1 = Natural::from_n(1);
45
46    // Build tree directly with constructor methods
47    let leaf_3_4 = Node::leaf(real_3_4);
48    let leaf_1 = Node::leaf(num_1);
49
50    let op_node = Node::branch(add_op, vec![leaf_3_4, leaf_1]);
51
52    // Create AST
53    let ast = AST::with_root(op_node);
54
55    // Print AST structure using Display trait
56    trace!("{}", ast);
57}
58
59/// Test 3: More complex expression "25.1 * 42 - 13"
60fn test_complex_expression() {
61    debug!("Test: Complex expression '25.1 * 42 - 13'");
62
63    // Create token instances
64    let real_25_1 = Real::from_n(25.1);
65    let multiply_op = MathOp::Multiply;
66    let num_42 = Natural::from_n(42);
67    let subtract_op = MathOp::Subtract;
68    let num_13 = Natural::from_n(13);
69
70    // Create leaf nodes
71    let leaf_25_1 = Node::leaf(real_25_1);
72    let leaf_42 = Node::leaf(num_42);
73    let leaf_13 = Node::leaf(num_13);
74
75    // Create multiplication operation
76    let mul_node = Node::branch(multiply_op, vec![leaf_25_1, leaf_42]);
77
78    // Create subtraction as the root operation
79    let root_node = Node::branch(subtract_op, vec![mul_node, leaf_13]);
80
81    // Create AST
82    let ast = AST::with_root(root_node);
83
84    // Print AST structure using Display trait
85    trace!("{}", ast);
86}
87
88/// Test 4: Building an AST for a logic expression "A & (B | C)"
89fn test_logic_expression() {
90    debug!("Test: Logic expression 'A & (B | C)'");
91
92    // Create token instances
93    let var_a = AlphaUpper::A;
94    let var_b = AlphaUpper::B;
95    let var_c = AlphaUpper::C;
96    let and_op = LogicOp::And;
97    let or_op = LogicOp::Or;
98
99    // Create leaf nodes
100    let leaf_a = Node::leaf(var_a);
101    let leaf_b = Node::leaf(var_b);
102    let leaf_c = Node::leaf(var_c);
103
104    // Create or operation
105    let or_node = Node::branch(or_op, vec![leaf_b, leaf_c]);
106
107    // Create and operation as the root
108    let root_node = Node::branch(and_op, vec![leaf_a, or_node]);
109
110    // Create AST
111    let ast = AST::with_root(root_node);
112
113    // Print AST structure using Display trait
114    trace!("{}", ast);
115}
Source

pub fn set_root(&mut self, root: Node)

Examples found in repository?
examples/ast_tester.rs (line 127)
118fn test_manipulating_ast() {
119    debug!("Test: Manipulating an AST");
120
121    // Create an empty AST
122    let mut ast = AST::new();
123    trace!("Empty AST is_empty(): {}", ast.is_empty());
124
125    // Create and set a root node
126    let root_node = Node::new(MathOp::Add);
127    ast.set_root(root_node);
128    trace!("After setting root, is_empty(): {}", ast.is_empty());
129
130    // Verify we can access and modify the root
131    if let Some(root) = &mut ast.root {
132        root.add_child(Node::leaf(MathOp::Multiply));
133        root.add_child(Node::leaf(MathOp::Divide));
134        trace!("Added {} children to root", root.children.len());
135        trace!("Root is_leaf(): {}", root.is_leaf());
136    }
137
138    // Print the modified AST using Display trait
139    trace!("{}", ast);
140}
Source

pub fn is_empty(&self) -> bool

Examples found in repository?
examples/ast_tester.rs (line 123)
118fn test_manipulating_ast() {
119    debug!("Test: Manipulating an AST");
120
121    // Create an empty AST
122    let mut ast = AST::new();
123    trace!("Empty AST is_empty(): {}", ast.is_empty());
124
125    // Create and set a root node
126    let root_node = Node::new(MathOp::Add);
127    ast.set_root(root_node);
128    trace!("After setting root, is_empty(): {}", ast.is_empty());
129
130    // Verify we can access and modify the root
131    if let Some(root) = &mut ast.root {
132        root.add_child(Node::leaf(MathOp::Multiply));
133        root.add_child(Node::leaf(MathOp::Divide));
134        trace!("Added {} children to root", root.children.len());
135        trace!("Root is_leaf(): {}", root.is_leaf());
136    }
137
138    // Print the modified AST using Display trait
139    trace!("{}", ast);
140}

Trait Implementations§

Source§

impl Clone for AST

Source§

fn clone(&self) -> AST

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for AST

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for AST

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.