run method
Run the program.
Implementation
Status run() {
if (_program == null) {
final result = compile();
if (result == Status.failed) {
return Status.failed;
}
}
var data = Uint8List(_kDataSize);
data.map((e) => 0);
var ptr = 0;
var index = 0;
while (_program![index].op != Operator.end && ptr < _kDataSize) {
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;
}
index++;
}
return Status.success;
}