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

102
LINES

< > BotCompany Repo | #1002743 // LThread (include, OLDER)

JavaX fragment (include)

1  
// Logic thread
2  
3  
static class LThread {
4  
  new L<Lisp> statements;
5  
  new HashMap<S, Lisp> 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  
  void say(Lisp b) {
29  
    output.add(snlApply(b, m));
30  
  }
31  
  
32  
  boolean stepImpl(Lisp code) {
33  
    if (code.isic("if *", "and *", "assume *")) {
34  
      Lisp a = code.get(0);
35  
      Lisp b = snlApply(a, m);
36  
      
37  
      // try all statements known
38  
      
39  
      for (Lisp s : statements) {
40  
        S x;
41  
        if (snlMatchX(b, s, m))
42  
          ret true;
43  
      }
44  
45  
      // try the low-level engine
46  
      
47  
      pcall {
48  
        if (yo(b, m)) // todo: rename vars
49  
          ret true;
50  
      }
51  
      
52  
      if (code.isic("assume *"))
53  
        say(snlToTree("assumption < failed < : < [" + snlFromTree(b) + "]"));
54  
55  
      // Not matched, fail
56  
      ret false;
57  
    }
58  
    
59  
    if (code.isic("say *")) {
60  
      Lisp b = code.get(0);
61  
      //print ("m: " + structure (m) + ", b: " + structure (b));
62  
      say(b);
63  
      ret true;
64  
    }
65  
    
66  
    if (code.isic("memorize *")) {
67  
      Lisp b = snlApply(code.get(0), m);
68  
      callOpt(mc(), "memorize", b);
69  
      ret true;
70  
    }
71  
    
72  
    // unknown code
73  
    
74  
    say(snlToTree("unknown < instruction < : < [" + snlFromTree(code) + "]"));
75  
      
76  
    ret false;
77  
  }
78  
  
79  
  // true if succeeded, false if failed
80  
  boolean run() {
81  
    while (!done())
82  
      if (!step()) {
83  
        print("fail");
84  
        ret false;
85  
      }
86  
    ret true;
87  
  }
88  
  
89  
  // split smth like "if * then *" into "if *" and "then *"
90  
  L<Lisp> splitOps(Lisp code) {
91  
    S ops = code.head;
92  
    L<S> tok = codeTokensOnly(javaTok(ops));
93  
    new L<Lisp> out;
94  
    for (int i = 0; i < l(tok); i += 2) {
95  
      S op = tok.get(i);
96  
      assertEquals("*", tok.get(i+1));
97  
      out.add(lisp(op + " *", code.get(i/2)));
98  
    }
99  
    print("splitOps => " + structure(out));
100  
    ret out;
101  
  }
102  
}

Author comment

Began life as a copy of #1002733

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: #1002743
Snippet name: LThread (include, OLDER)
Eternal ID of this version: #1002743/1
Text MD5: 3ddd4de6fbd56e6784f67dc31191e8c7
Author: stefan
Category: eleu / nl
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-02-20 21:12:16
Source code size: 2312 bytes / 102 lines
Pitched / IR pitched: No / No
Views / Downloads: 495 / 699
Referenced in: [show references]