Imports System
Imports uCalcSoftware
Public Module Program
   
   Public Sub LogBalance(ByVal cb As uCalc.Callback)
      '// Get the message and the current balance to log
      Dim msg = cb.ArgStr(1)
      Dim bal = cb.uCalc.Eval("balance")
      Console.WriteLine($"[LOG] {msg} | New Balance: {bal}")
   End Sub
   
   Public Sub Main()
      Dim uc As New uCalc()
      '// 1. Initialize the balance and the logger function
      uc.DefineVariable("balance = 0")
      uc.DefineFunction("LOG(msg As String)", AddressOf LogBalance)
      
      '// 2. Define DSL rules that also call the LOG function
      Dim 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
      Dim 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")}")
   End Sub
End Module