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

using namespace std;
using namespace uCalcSoftware;

void ucalc_call CustomIIf(uCalcBase::Callback cb) {
   bool condition = cb.ArgBool(1);
   auto truePart = cb.ArgExpr(2);
   auto falsePart = cb.ArgExpr(3);

   if (condition) {
      cb.Return(truePart.Evaluate());
   } else {
      cb.Return(falsePart.Evaluate());
   }
}
int main() {
   uCalc uc;
   uc.DefineFunction("MyIIf(condition As Bool, ByExpr thenExpr, ByExpr elseExpr)", CustomIIf);

   // The 'else' branch contains a division by zero, but it should NOT be evaluated
   // because the condition (1 < 2) is true.
   auto result = uc.Eval("MyIIf(1 < 2, 100, 1/0)");
   cout << "Result 1: ";
   cout << result << endl;

   // Now test the false branch. The 'then' branch with the error is skipped.
   result = uc.Eval("MyIIf(1 > 2, 1/0, 200)");
   cout << "Result 2: ";
   cout << result << endl;
}