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

92
LINES

< > BotCompany Repo | #1002750 // LThread with SNLMatches

JavaX fragment (include)

1  
// Logic thread
2  
3  
static class LThread {
4  
  new L<Lisp> statements;
5  
  new SNLMatches m; // current variable assignments
6  
  L<Lisp> ops;
7  
  int opIdx;
8  
  new L<Lisp> output;
9  
  L<S> log;
10  
  
11  
  *(Lisp code) {
12  
    ops = splitOps(code);
13  
  }
14  
  
15  
  boolean done() {
16  
    ret opIdx >= l(ops);
17  
  }
18  
  
19  
  // returns false if failed
20  
  boolean step() {
21  
    Lisp code = ops.get(opIdx++);
22  
    boolean ok = stepImpl(code);
23  
    if (log != null)
24  
      log.add((ok ? "[OK] ": "[FAIL] ") + " step " + snlFromTree(code) + " with " + structure(m));
25  
    ret ok;
26  
  }
27  
  
28  
  boolean stepImpl(Lisp code) {
29  
    if (code.isic("if *", "and *")) {
30  
      Lisp a = code.get(0);
31  
      Lisp b = snlApply(a, m.map);
32  
      
33  
      // try all statements in context
34  
      
35  
      for (Lisp s : statements) {
36  
        S x;
37  
        if (snlMatch2(b, s, m))
38  
          ret true;
39  
      }
40  
41  
      // try the low-level engine
42  
      
43  
      pcall {
44  
        if (yo(b, m)) // todo: rename vars
45  
          ret true;
46  
      }
47  
48  
      // Not matched, fail
49  
      ret false;
50  
    }
51  
    
52  
    if (code.isic("say *")) {
53  
      Lisp b = code.get(0);
54  
      //print ("m: " + structure (m) + ", b: " + structure (b));
55  
      output.add(snlApply(b, m.map));
56  
      ret true;
57  
    }
58  
    
59  
    if (code.isic("memorize *")) {
60  
      Lisp b = snlApply(code.get(0), m.map);
61  
      callOpt(mc(), "memorize", b);
62  
      ret true;
63  
    }
64  
    
65  
    // unknown code
66  
    ret false;
67  
  }
68  
  
69  
  // true if succeeded, false if failed
70  
  boolean run() {
71  
    while (!done())
72  
      if (!step()) {
73  
        print("fail");
74  
        ret false;
75  
      }
76  
    ret true;
77  
  }
78  
  
79  
  // split smth like "if * then *" into "if *" and "then *"
80  
  L<Lisp> splitOps(Lisp code) {
81  
    S ops = code.head;
82  
    L<S> tok = codeTokensOnly(javaTok(ops));
83  
    new L<Lisp> out;
84  
    for (int i = 0; i < l(tok); i += 2) {
85  
      S op = tok.get(i);
86  
      assertEquals("*", tok.get(i+1));
87  
      out.add(lisp(op + " *", code.get(i/2)));
88  
    }
89  
    print("splitOps => " + structure(out));
90  
    ret out;
91  
  }
92  
}

Author comment

Began life as a copy of #1002743

download  show line numbers  debug dex  old transpilations   

Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1002750
Snippet name: LThread with SNLMatches
Eternal ID of this version: #1002750/1
Text MD5: 6fe5a12361498a45203ae6baa859d762
Author: stefan
Category: eleu / nl
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-02-19 21:50:10
Source code size: 2052 bytes / 92 lines
Pitched / IR pitched: No / No
Views / Downloads: 686 / 860
Referenced in: [show references]