#include <iostream>
#include "uCalc.h"

using namespace std;
using namespace uCalcSoftware;

// This handler automatically defines variables when they are first used.
void ucalc_call AutoDefineHandler(Handle_uCalc h) {
   auto uc = uCalc(h);
   // Check if the error is specifically an undefined identifier
   if (uc.Error().Code() == ErrorCode::Undefined_Identifier) {
      cout << "Handler: '" << uc.Error().Symbol() << "' is undefined. Defining it now." << endl;
      uc.DefineVariable(uc.Error().Symbol());

      // Tell the engine to resume the operation
      uc.Error().Response(ErrorHandlerResponse::Resume);
   }
}

int main() {
   uCalc uc;
   uc.Error().AddHandler(AutoDefineHandler);

   // 'my_var' doesn't exist yet, but the handler will create it.
   auto result = uc.EvalStr("my_var = 100; my_var = my_var * 2");

   cout << "Final result: " << result << endl;
}