Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

82
LINES

< > BotCompany Repo | #1002205 // 1st Level Dialog Engine with example script (WORKS)

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

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

Snippet ID: #1002205
Snippet name: 1st Level Dialog Engine with example script (WORKS)
Eternal ID of this version: #1002205/1
Text MD5: 5e99551e8ff466cda0fd38f86caa8588
Transpilation MD5: 5f7f5c3a97a151f1388d5fa2e6a19689
Author: stefan
Category: javax
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-12-27 18:13:50
Source code size: 2088 bytes / 82 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 655 / 727
Referenced in: [show references]