pub struct Node {
pub token: Box<dyn Token>,
pub children: Vec<Node>,
}Expand description
A node in the Abstract Syntax Tree
Fields§
§token: Box<dyn Token>The token contained in this node (as a trait object)
children: Vec<Node>Child nodes of this node
Implementations§
Source§impl Node
impl Node
Sourcepub fn new<T: Token + 'static>(token: T) -> Self
pub fn new<T: Token + 'static>(token: T) -> Self
Creates a new node with the given token
Examples found in repository?
examples/ast_tester.rs (line 26)
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}
116
117/// Test 5: Manipulating an AST
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}Sourcepub fn leaf<T: Token + 'static>(token: T) -> Self
pub fn leaf<T: Token + 'static>(token: T) -> Self
Creates a new leaf node (no children) with the given token
Examples found in repository?
examples/ast_tester.rs (line 22)
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}
116
117/// Test 5: Manipulating an AST
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}Sourcepub fn branch<T: Token + 'static>(token: T, children: Vec<Node>) -> Self
pub fn branch<T: Token + 'static>(token: T, children: Vec<Node>) -> Self
Creates a new branch node with the given token and children
Examples found in repository?
examples/ast_tester.rs (line 50)
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}Sourcepub fn add_child(&mut self, child: Node)
pub fn add_child(&mut self, child: Node)
Adds a child node to this node
Examples found in repository?
examples/ast_tester.rs (line 27)
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}
116
117/// Test 5: Manipulating an AST
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}Sourcepub fn is_leaf(&self) -> bool
pub fn is_leaf(&self) -> bool
Returns true if this node is a leaf (has no children)
Examples found in repository?
examples/ast_tester.rs (line 135)
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§
Auto Trait Implementations§
impl Freeze for Node
impl !RefUnwindSafe for Node
impl !Send for Node
impl !Sync for Node
impl Unpin for Node
impl !UnwindSafe for Node
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