Imports System
Imports uCalcSoftware
Public Module Program
   
   Public Sub ShortCircuitOr(ByVal cb As uCalc.Callback)
      Dim arg1 = cb.ArgExpr(1)
      Dim arg2 = cb.ArgExpr(2)
      
      '// Evaluate the first argument
      Dim result1 = arg1.EvaluateBool()
      
      '// If the first is true, return immediately without touching the second
      If result1 Then
         cb.ReturnBool(true)
      Else
         '// Otherwise, evaluate and return the second argument's result
         cb.ReturnBool(arg2.EvaluateBool())
      End If
   End Sub
   
   Public Sub FuncA(ByVal cb As uCalc.Callback)
      cb.uCalc.Eval("countA = countA + 1")
      cb.ReturnBool(true)
   End Sub
   
   Public Sub FuncB(ByVal cb As uCalc.Callback)
      cb.uCalc.Eval("countB = countB + 1")
      cb.ReturnBool(true)
   End Sub
   
   Public Sub Main()
      Dim uc As New uCalc()
      uc.DefineVariable("countA = 0")
      uc.DefineVariable("countB = 0")
      
      uc.DefineFunction("FuncA() As Bool", AddressOf FuncA)
      uc.DefineFunction("FuncB() As Bool", AddressOf FuncB)
      
      uc.DefineFunction("SC_Or(ByExpr a, ByExpr b) As Bool", AddressOf ShortCircuitOr)
      
      Console.WriteLine("Calling SC_Or(FuncA(), FuncB())...")
      uc.Eval("SC_Or(FuncA(), FuncB())")
      
      Console.WriteLine($"FuncA was called {uc.Eval("countA")} time(s).")
      Console.WriteLine($"FuncB was called {uc.Eval("countB")} time(s).") '// Should be 0
   End Sub
End Module