!752 static L thescript = toLinesFullTrim([[ match("Hi Eleutheria"); say("Hi there!"); match("Hi Eleutheria"); say("You have said that before :)"); ]]); static class PerUser { S dialogID; long position; } static new Map perUserMap; static new ThreadLocal puCurrent; static O mainBot; p { load("perUserMap"); makeBot("A Dialog Bot"); printList(thescript); } // does not increase the index static FC parseFunctionCall() { ret parseFunctionCall(currentScriptLine()); } static S currentScriptLine() { int idx = (int) (u().position % l(thescript)); ret thescript.get(idx); } synchronized answer { S dialogID = cast call(mainBot, "getDialogID"); if (dialogID == null) ret null; // need a dialog id now PerUser pu = perUserMap.get(dialogID); if (pu == null) { pu = new PerUser; pu.dialogID = dialogID; printFormat("Created new dialog: *", dialogID); perUserMap.put(dialogID, pu); } puCurrent.set(pu); 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 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() { ++u().position; save("perUserMap"); print("Position in script now: " + u().position % l(thescript)); } // get per-user data for current thread static PerUser u() { ret puCurrent.get(); }