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)

// Logic thread

static class LThread {
  new L<Lisp> statements;
  new HashMap<S, Lisp> m; // current variable assignments
  L<Lisp> ops;
  int opIdx;
  new L<Lisp> output;
  L<S> log;
  
  *(Lisp code) {
    ops = splitOps(code);
  }
  
  boolean done() {
    ret opIdx >= l(ops);
  }
  
  // returns false if failed
  boolean step() {
    Lisp code = ops.get(opIdx++);
    boolean ok = stepImpl(code);
    if (log != null)
      log.add((ok ? "[OK] ": "[FAIL] ") + " step " + snlFromTree(code) + " with " + structure(m));
    ret ok;
  }
  
  void say(Lisp b) {
    output.add(snlApply(b, m));
  }
  
  boolean stepImpl(Lisp code) {
    if (code.isic("if *", "and *", "assume *")) {
      Lisp a = code.get(0);
      Lisp b = snlApply(a, m);
      
      // try all statements known
      
      for (Lisp s : statements) {
        S x;
        if (snlMatchX(b, s, m))
          ret true;
      }

      // try the low-level engine
      
      pcall {
        if (yo(b, m)) // todo: rename vars
          ret true;
      }
      
      if (code.isic("assume *"))
        say(snlToTree("assumption < failed < : < [" + snlFromTree(b) + "]"));

      // Not matched, fail
      ret false;
    }
    
    if (code.isic("say *")) {
      Lisp b = code.get(0);
      //print ("m: " + structure (m) + ", b: " + structure (b));
      say(b);
      ret true;
    }
    
    if (code.isic("memorize *")) {
      Lisp b = snlApply(code.get(0), m);
      callOpt(mc(), "memorize", b);
      ret true;
    }
    
    // unknown code
    
    say(snlToTree("unknown < instruction < : < [" + snlFromTree(code) + "]"));
      
    ret false;
  }
  
  // true if succeeded, false if failed
  boolean run() {
    while (!done())
      if (!step()) {
        print("fail");
        ret false;
      }
    ret true;
  }
  
  // split smth like "if * then *" into "if *" and "then *"
  L<Lisp> splitOps(Lisp code) {
    S ops = code.head;
    L<S> tok = codeTokensOnly(javaTok(ops));
    new L<Lisp> out;
    for (int i = 0; i < l(tok); i += 2) {
      S op = tok.get(i);
      assertEquals("*", tok.get(i+1));
      out.add(lisp(op + " *", code.get(i/2)));
    }
    print("splitOps => " + structure(out));
    ret out;
  }
}

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: 553 / 760
Referenced in: [show references]