#include #include "uCalc.h" using namespace std; using namespace uCalcSoftware; int main() { uCalc uc; auto t = uc.NewTransformer(); t.DefaultRuleSet().RewindOnChange(true); // {@@Eval} evaluates the expression obtained by concatinating the captured // elements {op} for operator, and {a} and {b} for the two numbers. // These elements are strings and do not need curly braces within {@@Eval} // :1 tels it to capture one token at a time. t.FromTo("({op:1} {a:1} {b:1})", "{@@Eval: a + op + b}"); // If there are more than to numbers, as indicated by {more}, then the first // two numbers are evaluated and put back into the list. The operator remains. t.FromTo("({op:1} {a:1} {b:1} {more})", "({op} {@@Eval: a + op + b} {more})"); // If a nested expression is present, it is evaluated immediately, by using // % in {expr%} and the section is reprocessed again with the resulting value // since .@RewindOnChange(true) was set. t.FromTo("({op:1} {expr%: ({exp})}", "({op} {expr}"); t.FromTo("({op:1} {a:1} {expr%: ({exp})}", "({op} {a} {expr}"); // --- Test Cases --- cout << "--- Simple Binary ---" << endl; auto expr = "(- 100 25)"; cout << "LISP: " << expr << endl; cout << "Result: " << t.Transform(expr) << endl; cout << "" << endl; cout << "--- Variadic (Multiple Args) ---" << endl; expr = "(* 2 3 4)"; cout << "LISP: " << expr << endl; cout << "Result: " << t.Transform(expr) << endl; cout << "" << endl; cout << "--- Nested Expressions ---" << endl; expr = "(/ 100 (+ 5 5))"; cout << "LISP: " << expr << endl; cout << "Result: " << t.Transform(expr) << endl; expr = "(+ (* (- 100 (/ 80 4)) 3) (- (* (+ 5 3) (- 12 7)) (+ (* 2 3) 4)))"; cout << "LISP: " << expr << endl; cout << "Result: " << t.Transform(expr) << endl; cout << "" << endl; }