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

54
LINES

< > BotCompany Repo | #1001844 // SO (developing)

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Libraryless. Click here for Pure Java version (852L/6K/19K).

!752

m {
  // subject, object (S/O can be verbs, nouns, anything)
  static class SO {
    S s, o;
    int value; // 1 = asserted as true, -1 = asserted as false
    
    *(S *s, S *o) {}
    *(S *s, S *o, int *value) {}
    *() {}
    
    public boolean equals(O _o) {
      if (!(_o instanceof SO)) ret false;
      SO x = cast _o;
      ret eq(s, x.s) && eq(o, x.o);
    }
  }
  
  static new L<SO> data;
  
  static void store(S s, S o, int value) {
    data.add(new SO(s, o, value));
  }
  
  static int query(S s, S o) {
    int i = data.indexOf(new SO(s, o));
    ret i >= 0 ? data.get(i).value : 0;
  }
  
  static S answer(S s) {
    new Matches m;
    if (match("query * *", s, m))
      ret string(query(m.unq(0), m.unq(1)));
    if (match("assert * *", s, m)) {
      store(m.unq(0), m.unq(1), 1);
      ret "OK.";
    }
    if (match("assertnot * *", s, m)) {
      store(m.unq(0), m.unq(1), -1);
      ret "OK.";
    }
    ret null;
  }
  
  p {
    store("we can wait", "1 second", 1);
    store("we can wait", "60 seconds", -1);
    assertEq(1, query("we can wait", "1 second"));
    assertEq(-1, query("we can wait", "60 seconds"));
    assertEq(0, query("we can wait", "10 seconds"));
    print("OK!");
  }
}

Author comment

Began life as a copy of #1001841

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: #1001844
Snippet name: SO (developing)
Eternal ID of this version: #1001844/1
Text MD5: af24bc7c49aa03bf0291e2c5343b311c
Transpilation MD5: 854fe94955d64b78e0fa5e7f7b0a19f5
Author: stefan
Category: javax
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-11-24 19:16:25
Source code size: 1280 bytes / 54 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 617 / 915
Referenced in: [show references]