run method

Status run ()

Run the program.

Implementation

Status run() {
  if (program == null) {
    final result = compile();
    if (result == Status.failed) {
      return Status.failed;
    }
  }
  var data = List<int>.generate(dataSize, (i) => 0);
  var ptr = 0;
  var index = 0;
  while (program[index].op != Operator.end && ptr < dataSize) {
    final instruction = program[index];
    switch (instruction.op) {
      case Operator.increasePointer:
        ptr++;
        break;
      case Operator.decreasePointer:
        ptr--;
        break;
      case Operator.increaseValue:
        data[ptr]++;
        break;
      case Operator.decreaseValue:
        data[ptr]--;
        break;
      case Operator.output:
        stdout.writeCharCode(data[ptr]);
        break;
      case Operator.input:
        data[ptr] = stdin.readByteSync();
        break;
      case Operator.jumpForward:
        if (data[ptr] == 0) {
          index = program[index].operand;
        }
        break;
      case Operator.jumpBack:
        if (data[ptr] != 0) {
          index = program[index].operand;
        }
        break;
      default:
        return Status.failed;
        break;
    }
    index++;
  }

  return Status.success;
}