using uCalcSoftware; var uc = new uCalc(); static void LogBalance(uCalc.Callback cb) { // Get the message and the current balance to log var msg = cb.ArgStr(1); var bal = cb.uCalc.Eval("balance"); Console.WriteLine($"[LOG] {msg} | New Balance: {bal}"); } // 1. Initialize the balance and the logger function uc.DefineVariable("balance = 0"); uc.DefineFunction("LOG(msg As String)", LogBalance); // 2. Define DSL rules that also call the LOG function var t = uc.ExpressionTransformer; // Each rule now has a side effect: logging its action. t.FromTo("DEPOSIT {@Number:amount}", "balance = balance + {amount}; LOG('Deposited {amount}')"); t.FromTo("WITHDRAW {@Number:amount}", "balance = balance - {amount}; LOG('Withdrew {amount}')"); // 3. A longer script to test the log var script = """ DEPOSIT 500 WITHDRAW 100 DEPOSIT 250 WITHDRAW 75 """; Console.WriteLine("--- Transaction Log ---"); uc.EvalStr(script); Console.WriteLine("-----------------------"); Console.WriteLine($"Final Balance: {uc.Eval("balance")}");