!759 // the up-to-date JavaX translator

sclass C { // "sclass" is short for "static class" (JavaX)
  char c;
  bool output;
  
  *() {}
  *(char *c, bool *output) {}
}

sS poemID = "#1003260"; // test dialog
static new L<C> script;
sint i = 0;
sbool fail;

p { // the main program is called "p" in JavaX (for "PROGRAM", or "PUBLIC static void main")
  //addOutput("Hello world!\n"); // simple tester instead of loading poem from website
  
  for (E e : parsePoem(poemID)) {
    if (e.q != null)
      addInput(e.q + "\n");
    else if (e.a != null)
      addOutput(e.a + "\n");
  }
  
  loop();
}

svoid loop() {
  printOut();
  S line;
  while ((line = readLine()) != null) {
    for (char c : chars(line + "\n"))
      onIncomingCharacter(c);
    printOut();
  }
}

svoid addOutput(S text) {
  for (char c : asChars(text))
    script.add(new C(c, true));
}

svoid addInput(S text) {
  for (char c : asChars(text))
    script.add(new C(c, false));
}

svoid onIncomingCharacter(char c) {
  if (fail || get(script, i) == null)
    charPut("?");
  else {
    C x = get(script, i);
    assertFalse(x.output);
    if (c == x.c) {
      ++i;
      printOut();
    } else {
      fail = true;
      charPut("?");
    }
  }
}

svoid printOut() {
  while (!fail && get(script, i) != null && get(script, i).output)
    charPut(get(script, i++).c);
  charPut_flush();
}