Libraryless. Click here for Pure Java version (2161L/14K/47K).
1 | !752 |
2 | |
3 | static L<S> thescript = toLinesFullTrim([[ |
4 | match("Hi Eleutheria"); |
5 | say("Hi there!"); |
6 | match("Hi Eleutheria"); |
7 | say("You have said that before :)"); |
8 | ]]); |
9 | |
10 | static long position; |
11 | |
12 | p { |
13 | load("position"); |
14 | makeBot("A Dialog Bot"); |
15 | printList(thescript); |
16 | } |
17 | |
18 | // does not increase the index |
19 | static FC parseFunctionCall() { |
20 | ret parseFunctionCall(currentScriptLine()); |
21 | } |
22 | |
23 | static S currentScriptLine() { |
24 | int idx = (int) (position % l(thescript)); |
25 | ret thescript.get(idx); |
26 | } |
27 | |
28 | synchronized answer { |
29 | int safety = 0; |
30 | while (safety++ < 1000) try { |
31 | FC fc = parseFunctionCall(); |
32 | S f = fc.f; |
33 | if (eq(f, "match")) { |
34 | S pat = getString(fc.args, 0); |
35 | if (!match(pat, s, m)) { |
36 | softFail("I only understand: *", pat); |
37 | null; |
38 | } |
39 | nextPosition(); // only after match succeeds, otherwise stay at that point |
40 | } else if (eq(f, "say")) { |
41 | nextPosition(); |
42 | S answer = getString(fc.args, 0); |
43 | ret answer; |
44 | } else |
45 | throw fail("Unknown function in script: *", fc.f); |
46 | } catch (Throwable e) { |
47 | printFormat("Was parsing: *", currentScriptLine()); |
48 | throw asRuntimeException(e); |
49 | } |
50 | fail("hard limit reached - 1000 script lines at once - possible endless loop?"); |
51 | } |
52 | |
53 | // a parsed function call |
54 | static class FC { |
55 | int idx; // index of next token |
56 | S f; |
57 | new L args; |
58 | } |
59 | |
60 | static FC parseFunctionCall(S s) { |
61 | L<S> tok = javaTok(s); |
62 | new FC fc; |
63 | fc.f = assertIsIdentifier(tok.get(1)); |
64 | int i = 5; |
65 | assertEquals("(", tok.get(3)); |
66 | while (i < l(tok) && !eq(tok.get(i), ")")) { |
67 | fc.args.add(unquote(tok.get(i))); |
68 | i += 2; |
69 | if (tok.get(i).equals(",")) // otherwise it's kinda mangled, eh. well! we just keep on parsing... |
70 | i += 2; |
71 | } |
72 | if (eq(tok.get(i), ")")) // we even allow a missing closing bracket! |
73 | i += 2; |
74 | fc.idx = i; // save index so some other parser can continue parsing |
75 | ret fc; |
76 | } |
77 | |
78 | static void nextPosition() { |
79 | ++position; |
80 | save("position"); |
81 | print("Position in script now: " + position % l(thescript)); |
82 | } |
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: | 755 / 854 |
Referenced in: | [show references] |