1#![allow(unused)]
6
7use dev_utils::{app_dt, debug, dlog::*, error, info, trace, warn};
8use logic_tracer::*; fn test_simple_binary_operation() {
14 debug!("Test: Simple binary operation '23 - 45'");
15
16 let num_23 = Natural::from_n(23);
18 let subtract_op = MathOp::Subtract;
19 let num_45 = Natural::from_n(45);
20
21 let leaf_23 = Node::leaf(num_23);
23 let leaf_45 = Node::leaf(num_45);
24
25 let mut op_node = Node::new(subtract_op);
27 op_node.add_child(leaf_23);
28 op_node.add_child(leaf_45);
29
30 let ast = AST::with_root(op_node);
32
33 trace!("{}", ast);
35}
36
37fn test_binary_operation_with_different_types() {
39 debug!("Test: Binary operation with different number types '3.4 + 1'");
40
41 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 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 let ast = AST::with_root(op_node);
54
55 trace!("{}", ast);
57}
58
59fn test_complex_expression() {
61 debug!("Test: Complex expression '25.1 * 42 - 13'");
62
63 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 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 let mul_node = Node::branch(multiply_op, vec![leaf_25_1, leaf_42]);
77
78 let root_node = Node::branch(subtract_op, vec![mul_node, leaf_13]);
80
81 let ast = AST::with_root(root_node);
83
84 trace!("{}", ast);
86}
87
88fn test_logic_expression() {
90 debug!("Test: Logic expression 'A & (B | C)'");
91
92 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 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 let or_node = Node::branch(or_op, vec![leaf_b, leaf_c]);
106
107 let root_node = Node::branch(and_op, vec![leaf_a, or_node]);
109
110 let ast = AST::with_root(root_node);
112
113 trace!("{}", ast);
115}
116
117fn test_manipulating_ast() {
119 debug!("Test: Manipulating an AST");
120
121 let mut ast = AST::new();
123 trace!("Empty AST is_empty(): {}", ast.is_empty());
124
125 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 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 trace!("{}", ast);
140}
141
142fn main() {
144 app_dt!(file!());
145 set_max_level(Level::Trace); info!("Testing AST implementation with Display trait");
148
149 test_complex_expression();
152 }