33 Companion zmachine.js (12/28)

  // ---------------------------------------------------------------- input

  tokenise(textAddr, parseAddr, dict = 0, skipUnknown = false) {
    const v = this.version;
    const start = v >= 5 ? textAddr + 2 : textAddr + 1;
    const len = v >= 5 ? this.mem[textAddr + 1] : (() => {
      let n = 0;
      while (this.mem[start + n]) n++;
      return n;
    })();
    const d = dict || this.dictionary;
    const nsep = this.mem[d];
    const seps = new Set(this.mem.subarray(d + 1, d + 1 + nsep));
    const entryLen = this.mem[d + 1 + nsep];
    const count = s16(this.rw(d + 2 + nsep));
    const entries = d + 4 + nsep;

    const words = [];
    let i = 0;
    while (i < len) {
      const c = this.mem[start + i];
      if (c === 32) { i++; continue; }
      if (seps.has(c)) { words.push({ pos: i, len: 1 }); i++; continue; }
      const s = i;
      while (i < len && this.mem[start + i] !== 32 && !seps.has(this.mem[start + i])) i++;
      words.push({ pos: s, len: i - s });
    }
‹ PreviousReader file (.txt)Next ›