repl method

void repl(
  1. String? sourceLine, {
  2. double timeLimit = 60,
})

Read Eval Print Loop. Run the given source until it either terminates, or hits the given time limit. When it terminates, if we have new implicit output, print that to the implicitOutput stream.

Implementation

void repl(String? sourceLine, {double timeLimit = 60}) {
  parser ??= Parser();
  if (vm == null) {
    vm = parser!.createVM(standardOutput);
    vm!.interpreter = WeakReference(this);
  } else if (vm!.done && !parser!.needMoreInput()) {
    // Since the machine and parser are both done, we don't really need the
    // previously-compiled code. So let's clear it out, as a memory optimization.
    vm!.getTopContext().clearCodeAndTemps();
    parser!.partialReset();
  }
  if (sourceLine == "#DUMP") {
    vm!.dumpTopContext();
    return;
  }

  double startTime = vm!.runTime;
  int startImpResultCount = vm!.globalContext!.implicitResultCounter;
  vm!.storeImplicit = (implicitOutput != null);
  vm!.yielding = false;

  try {
    if (sourceLine != null) parser!.parse(sourceLine, replMode: true);
    if (!parser!.needMoreInput()) {
      while (!vm!.done && !vm!.yielding) {
        if (vm!.runTime - startTime > timeLimit) {
          return; // time's up for now!
        }
        vm!.step();
      }
      checkImplicitResult(startImpResultCount);
    }
  } on MiniscriptException catch (e) {
    reportError(e);
    // Attempt to recover from an error by jumping to the end of the code.
    stop();
  }
}