ast_tester/
ast_tester.rs

1//! Test script for the AST implementation with Display trait
2//!
3//! Run this to verify that the AST structure works correctly and displays properly
4
5#![allow(unused)]
6
7use dev_utils::{app_dt, debug, dlog::*, error, info, trace, warn};
8use logic_tracer::*;  // todo: Add some 'prelude' module to avoid importing everything each time
9// use logic_tracer::ast::*;
10// use logic_tracer::tokens::*;
11
12/// Test 1: Simple binary operation "23 - 45"
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}
141
142/// Run all the tests
143fn main() {
144    app_dt!(file!());
145    set_max_level(Level::Trace); // Set log level to Trace
146
147    info!("Testing AST implementation with Display trait");
148
149    // test_simple_binary_operation();
150    // test_binary_operation_with_different_types();
151    test_complex_expression();
152    // test_logic_expression();
153    // test_manipulating_ast();
154}