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

using namespace std;
using namespace uCalcSoftware;

void ucalc_call MyRepeat(uCalcBase::Callback cb) {
   auto count = cb.ArgInt32(1);
   auto action = cb.ArgExpr(2);

   for (int i = 1; i <= count; i++) {
      action.Execute(); // Evaluate the passed-in expression
   }
}

int main() {
   uCalc uc;
   // 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)", 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
   cout << "Final counter value: " << uc.Eval("counter") << endl;
}