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

47
LINES

< > BotCompany Repo | #1002842 // nlMatch

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (12912L/74K).

static boolean nlMatch(S pat, S nl, Map<S, Lisp> matches) {
  matches.clear();
  ret nlMatch_sub(nlParse/*_cached*/(pat), nlParse(nl), matches);
}

static boolean nlMatch(Lisp pat, S nl, Map<S, Lisp> matches) {
  matches.clear();
  ret nlMatch_sub(pat, nlParse(nl), matches);
}

static boolean nlMatch(S pat, Lisp nl, Map<S, Lisp> matches) {
  matches.clear();
  ret nlMatch_sub(nlParse/*_cached*/(pat), nl, matches);
}

static boolean nlMatch(Lisp pat, Lisp nl, Map<S, Lisp> matches) {
  matches.clear();
  ret nlMatch_sub(pat, nl, matches);
}

static boolean nlMatch_sub(Lisp pat, Lisp nl, Map<S, Lisp> m) {
  if (pat == null || nl == null) ret false;
  if (pat.isA("*"))
    ret true;
  if (nlIsVar(pat))
    ret nlMatch_putMatch(m, pat.head, nl);
    
  if (neq(pat.head, nl.head)) ret false;
  
  // heads identical, proceed to children
  int n = pat.size();
  if (n != nl.size()) ret false;
  
  for (int i = 0; i < n; i++)
    if (!nlMatch_sub(pat.get(i), nl.get(i), m))
      ret false;
      
  ret true;
}

static boolean nlMatch_putMatch(Map<S, Lisp> matches, S key, Lisp val) {
  key = dropPrefix("$", key);
  if (matches.containsKey(key) && neq(matches.get(key), val))
    fail("multi-matching not implemented");
  matches.put(key, val);
  ret true;
}

Author comment

Began life as a copy of #1002721

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: #1002842
Snippet name: nlMatch
Eternal ID of this version: #1002842/1
Text MD5: 947e58870acfb91fe020207518a96827
Transpilation MD5: b46422b680d71ffc11b7227f74e97dad
Author: stefan
Category: eleu / nl
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-03-02 21:04:45
Source code size: 1310 bytes / 47 lines
Pitched / IR pitched: No / No
Views / Downloads: 571 / 734
Referenced in: [show references]