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)

// Logic thread

static class LThread {
  new L<Lisp> statements;
  new SNLMatches 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;
  }
  
  boolean stepImpl(Lisp code) {
    if (code.isic("if *", "and *")) {
      Lisp a = code.get(0);
      Lisp b = snlApply(a, m.map);
      
      // try all statements in context
      
      for (Lisp s : statements) {
        S x;
        if (snlMatch2(b, s, m))
          ret true;
      }

      // try the low-level engine
      
      pcall {
        if (yo(b, m)) // todo: rename vars
          ret true;
      }

      // Not matched, fail
      ret false;
    }
    
    if (code.isic("say *")) {
      Lisp b = code.get(0);
      //print ("m: " + structure (m) + ", b: " + structure (b));
      output.add(snlApply(b, m.map));
      ret true;
    }
    
    if (code.isic("memorize *")) {
      Lisp b = snlApply(code.get(0), m.map);
      callOpt(mc(), "memorize", b);
      ret true;
    }
    
    // unknown 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 #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: 687 / 860
Referenced in: #1002746 - SNL Logic Engine 3 (C is a Language)
#1002752 - LThread with SNLMatches and Engine link (CURRENT)
#3000382 - Answer for ferdie (>> t = 1, f = 0)
#3000383 - Answer for funkoverflow (>> t=1, f=0 okay)