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

93
LINES

< > BotCompany Repo | #1001509 // Rules & statements (include)

JavaX fragment (include)

  static new L<S> rules;
  static new L<S> statements;
  
  // result of matchStatements ordered by variable
  static MultiMap<S, S> matchAll(S pat) {
    new MultiMap<S, S> map;
    L<Map<S, S>> l = matchStatements(pat);
    print("[" + l.size() + " matches for " + quote(pat) + "]");
    for (Map<S, S> m : l) {
      for (Map.Entry<S, S> e : m.entrySet())
        map.put(e.getKey(), e.getValue());
    }
    ret map;
  }
  
  static void process() {
    for (S rule : rules) {
      new Matches m;
      if (match3("* <=> *", rule, m)) {
        S l = m.unq(0), r = m.unq(1);
        rewrite(l, r);
        rewrite(r, l);
      }
    }
  }
  
  static void rewrite(S l, S r) {
    for (Map<S, S> map : matchStatements(l))
      addStatement(convert(r, map));
  }
  
  static L<Map<S, S>> matchStatements(S pat) {
    L<Map<S, S>> l = new ArrayList<Map<S, S>>();
    for (S s : statements) {
      Map<S, S> m = match4(pat, s);
      if (m != null) l.add(m);
    }
    ret l;
  }
  
  static Map<S, S> match4(S pat, S s) {
    new Map<S, S> m;
    ret match4(pat, s, m) ? m : null;
  }
  
  static boolean match4(S pat, S s, Map<S, S> m) {
    L<S> tok1 = parse3(pat), tok2 = parse3(s);
    if (tok1.size() != tok2.size()) ret false;
    m.clear();
    for (int i = 1; i < tok1.size(); i += 2) {
      S t1 = tok1.get(i), t2 = tok2.get(i);
      if (isVar(t1))
        m.put(t1, t2);
      else if (!tokEq(t1, t2))
        ret false;
    }
    ret true;
  }
  
  static boolean tokEq(S a, S b) {
    a = unquote(a);
    b = unquote(b);
    ret a.equalsIgnoreCase(b);
  }
  
  static boolean isVar(S s) {
    ret s.startsWith("$") && s.length() > 1;
  }
  
  static S convert(S r, Map<S, S> matches) {
    L<S> tok = javaTokPlusPeriod(r);
    for (int i = 1; i < tok.size(); i += 2) {
      if (tok.get(i).startsWith("$")) {
        S m = matches.get(tok.get(i));
        if (m != null)
          tok.set(i, m);
      }
    }
    ret join(tok);
  }
  
  static void addStatement(S s) {
    for (S st : statements)
      if (match3(st, s))
        ret;
    statements.add(s);
  }
  
  !include #2000515 // unquote
  !include #1001497 // parseBoolean
  !include #1000709 // formatSnippetID
  
  !include #1001296 // MultiMap

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: #1001509
Snippet name: Rules & statements (include)
Eternal ID of this version: #1001509/1
Text MD5: 21b0c045d60bda846f5301b2ad5573b4
Author: stefan
Category:
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-10-21 16:15:03
Source code size: 2316 bytes / 93 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 466 / 1033
Referenced in: [show references]