diff --git a/src/ast.cxx b/src/ast.cxx new file mode 100644 index 0000000..7880330 --- /dev/null +++ b/src/ast.cxx @@ -0,0 +1,63 @@ +#include "ast.hxx" +#include + +// Static initialization of variableTable in Identifier class. +std::unordered_map Identifier::variableTable; + +#ifdef ENABLE_TESTS +// Function to run a test expression evaluation. +void runTest() { + // Test with a complex expression: (2 * (a + b)) / (c - 1) ^ (d + 1) + Identifier::setVariable("a", 3.0); + Identifier::setVariable("b", 1.0); + Identifier::setVariable("c", 5.0); + Identifier::setVariable("d", 2.0); + + std::unique_ptr variableA = std::make_unique("a"); + std::unique_ptr variableB = std::make_unique("b"); + std::unique_ptr variableC = std::make_unique("c"); + std::unique_ptr variableD = std::make_unique("d"); + + std::unique_ptr expression = std::make_unique( + std::make_unique(std::make_unique(2.0), + std::make_unique(std::move(variableA), std::move(variableB))), + std::make_unique(std::make_unique(std::move(variableC), std::make_unique(1.0)), + std::make_unique(std::move(variableD), std::make_unique(1.0)))); + + // Evaluate the expression + double result = expression->evaluate(); + + // Expected result: (2 * (3 + 1)) / (5 - 1) ^ (2 + 1) = 8 / 4 ^ 3 = 8 / 64 = 0.125 + assert(result == 0.125); + + std::cout << "Test passed successfully. Result: " << result << std::endl; + + // Clear variables for the next test + Identifier::clearVariables(); +} +#endif // ENABLE_TESTS + +// Function to print the help message. +void printHelpMessage(const char *programName) { + std::cout << "Usage: " << programName << " [--run-test]\n" + << "Options:\n" + << " --run-test Run the test for the expression evaluation code.\n" + << " This option should be used without any additional arguments.\n" + << " Example: " << programName << " --run-test\n"; +} + +// Main function +int main(int argc, char *argv[]) { + // Check if the "--run-test" argument is provided + if (argc == 2 && std::strcmp(argv[1], "--run-test") == 0) { +#ifdef ENABLE_TESTS + // Run the test if ENABLE_TESTS is defined + runTest(); +#endif // ENABLE_TESTS + } else { + // Print help message if no valid arguments are provided + printHelpMessage(argv[0]); + } + + return 0; +}