Imports System
Imports uCalcSoftware
Public Module Program
   
   '// This handler automatically defines variables when they are first used.
   Public Sub AutoDefineHandler(ByVal h As Handle_uCalc)
      Dim uc As New uCalc(h)
      '// Check if the error is specifically an undefined identifier
      If uc.Error.Code = ErrorCode.Undefined_Identifier Then
         Console.WriteLine($"Handler: '{uc.Error.Symbol}' is undefined. Defining it now.")
         uc.DefineVariable(uc.Error.Symbol)
         
         '// Tell the engine to resume the operation
         uc.Error.Response = ErrorHandlerResponse.Resume
      End If
   End Sub
   
   Public Sub Main()
      Dim uc As New uCalc()
      uc.Error.AddHandler(AddressOf AutoDefineHandler)
      
      '// 'my_var' doesn't exist yet, but the handler will create it.
      Dim result = uc.EvalStr("my_var = 100; my_var = my_var * 2")
      
      Console.WriteLine($"Final result: {result}")
   End Sub
End Module