Libraryless. Click here for Pure Java version (2161L/14K/47K).
!752 static L<S> thescript = toLinesFullTrim([[ match("Hi Eleutheria"); say("Hi there!"); match("Hi Eleutheria"); say("You have said that before :)"); ]]); static long position; p { load("position"); makeBot("A Dialog Bot"); printList(thescript); } // does not increase the index static FC parseFunctionCall() { ret parseFunctionCall(currentScriptLine()); } static S currentScriptLine() { int idx = (int) (position % l(thescript)); ret thescript.get(idx); } synchronized answer { int safety = 0; while (safety++ < 1000) try { FC fc = parseFunctionCall(); S f = fc.f; if (eq(f, "match")) { S pat = getString(fc.args, 0); if (!match(pat, s, m)) { softFail("I only understand: *", pat); null; } nextPosition(); // only after match succeeds, otherwise stay at that point } else if (eq(f, "say")) { nextPosition(); S answer = getString(fc.args, 0); ret answer; } else throw fail("Unknown function in script: *", fc.f); } catch (Throwable e) { printFormat("Was parsing: *", currentScriptLine()); throw asRuntimeException(e); } fail("hard limit reached - 1000 script lines at once - possible endless loop?"); } // a parsed function call static class FC { int idx; // index of next token S f; new L args; } static FC parseFunctionCall(S s) { L<S> tok = javaTok(s); new FC fc; fc.f = assertIsIdentifier(tok.get(1)); int i = 5; assertEquals("(", tok.get(3)); while (i < l(tok) && !eq(tok.get(i), ")")) { fc.args.add(unquote(tok.get(i))); i += 2; if (tok.get(i).equals(",")) // otherwise it's kinda mangled, eh. well! we just keep on parsing... i += 2; } if (eq(tok.get(i), ")")) // we even allow a missing closing bracket! i += 2; fc.idx = i; // save index so some other parser can continue parsing ret fc; } static void nextPosition() { ++position; save("position"); print("Position in script now: " + position % l(thescript)); }
download show line numbers debug dex old transpilations
Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, teubizvjbppd, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment