Imports System
Imports uCalcSoftware
Public Module Program
   
   Public Sub MyRepeat(ByVal cb As uCalc.Callback)
      Dim count = cb.ArgInt32(1)
      Dim action = cb.ArgExpr(2)
      
      For i  As Integer = 1 To count
         action.Execute() '// Evaluate the passed-in expression
      Next
   End Sub
   
   Public Sub Main()
      Dim uc As New uCalc()
      '// Define a variable that our action will modify
      uc.DefineVariable("counter = 0")
      
      '// Define the function. The 'action' parameter is marked with ByExpr
      '// to ensure it's passed as an unevaluated expression object.
      uc.DefineFunction("Repeat(count As Int, ByExpr action)", AddressOf MyRepeat)
      
      '// Call the custom Repeat function. The expression 'counter++' is not
      '// evaluated here; it's passed to the callback to be executed in a loop.
      uc.Eval("Repeat(5, counter++)")
      
      '// Verify the side effect
      Console.WriteLine($"Final counter value: {uc.Eval("counter")}")
   End Sub
End Module