run method

Future<void> run()

Runs the interactive loop until the session ends.

The session ends when a trimmed input line is exactly q or exit (checked before bracket buffering, also in continuation mode), when the line stream ends (EOF), or when a running program throws a BrainfuckRuntimeException — the error is printed first. An UnexpectedClosingBracketException is printed, the buffer is dropped, and the loop returns to the main prompt.

Implementation

Future<void> run() async {
  final buffer = StringBuffer();
  _out.write(_prompt);
  await for (final line in _lines) {
    final trimmed = line.trim();
    if (trimmed == 'q' || trimmed == 'exit') return;

    if (buffer.isNotEmpty) buffer.write('\n');
    buffer.write(line);

    final Program program;
    try {
      program = parse(buffer.toString());
    } on UnclosedBracketException {
      _out.write(_continuationPrompt);
      continue;
    } on UnexpectedClosingBracketException catch (e) {
      _out
        ..writeln(e)
        ..write(_prompt);
      buffer.clear();
      continue;
    }
    buffer.clear();

    try {
      _interpreter.run(program);
    } on BrainfuckRuntimeException catch (e) {
      _out.writeln(e);
      return;
    }
    _out.write(_prompt);
  }
}