tokenise method

void tokenise()

Implementation

void tokenise() {
  //Debugger.verbose('${pcHex(-1)} [tokenise]');

  var operands = visitOperandsVar(4, true);

  if (operands.length > 2) {
    throw GameException(
        "tokenise dictionary argument is not yet support in v5+");
  }

  var maxBytes = mem.loadb(operands[0].value!);

  var textBuffer = operands[0].value! + 2;

  var maxWords = mem.loadb(operands[1].value!);

  var parseBuffer = operands[1].value! + 1;

  var line = Z.mostRecentInput.toLowerCase();

  //Debugger.verbose('    (processing: "$line")');

  var charCount = mem.loadb(textBuffer - 1);
  //Debugger.debug('existing chars: $charCount');

  if (charCount > 0) {
    //continuation of previous input
    maxBytes -= charCount;
  }

  if (line.length > maxBytes - 1) {
    final newLine = line.substring(0, maxBytes - 2);
    log.warning("text buffer truncated:  $line to $newLine");
    line = newLine;
    //Debugger.verbose('    (text buffer truncated to "$line")');
  }

  //Debugger.debug('>> $line');

  //write the total to the textBuffer (adjust if continuation)
  mem.storeb(textBuffer - 1, line.length + charCount > 0 ? charCount : 0);

  var zChars = ZSCII.toZCharList(line);

  //adjust if continuation
  textBuffer += charCount > 0 ? charCount : 0;

  //store the zscii chars in text buffer
  for (final c in zChars) {
    mem.storeb(textBuffer++, c);
  }

  var tokens = Z.engine.mem.dictionary.tokenize(line);

  //Debugger.verbose('    (tokenized: $tokens)');

  var parsed = Z.engine.mem.dictionary.parse(tokens, line);

  var maxParseBufferBytes = (4 * maxWords) + 2;

  var i = 0;
  for (final p in parsed) {
    i++;
    if (i > maxParseBufferBytes) break;
    mem.storeb(parseBuffer++, p);
  }
}