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

60
LINES

< > BotCompany Repo | #1001097 // Learning android

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

Libraryless. Click here for Pure Java version (523L/5K/14K).

!747
!multi-line strings

m {
  static L<S> rules = litlist(
    [[ on "hello", say "hello you too" ]]
  );
  
  p {
    answerQuestionsOnPortAndConsole(stringfunc {
      fallback(s, answer(s))
    });
  }
  
  static class Matches {
    S[] m;
  }
  
  static S fallback(S s, S answer) {
    if (answer != null) return answer;
    return "?";
  }
  
  static S answer(S s) {
    if (match("on * say *", s)) {
      rules.add(0, s); // add rule on top to override others
      return "ok";
    }
      
    new Matches matches;
    for (S rule : rules) {
      if (match("on * say *", rule, matches)) {
        S in = matches.m[0], out = matches.m[1];
        if (match(unquote(in), s))
          return unquote(out);
      }
    }
    return null;
  }
  
  static boolean match(S pat, S s) {
    return match(pat, s, null);
  }
  
  static boolean match(S pat, S s, Matches matches) {
    L<S> tokpat = parse(pat), toks = parse(s);
    S[] m = match2(tokpat, toks);
    //print(structure(tokpat) + " on " + structure(toks) + " => " + structure(m));
    if (m == null)
      return false;
    else {
      if (matches != null) matches.m = m;
      return true;
    }
  }
  
  static L<S> parse(S s) {
    return dropPunctuation(javaTokPlusPeriod(s));
  }
}

Author comment

Began life as a copy of #1001092

download  show line numbers  debug dex  old transpilations   

Travelled to 16 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, teubizvjbppd, tslmcundralx, tvejysmllsmz, vouqrxazstgt, xinetxnxrdbb

No comments. add comment

Snippet ID: #1001097
Snippet name: Learning android
Eternal ID of this version: #1001097/1
Text MD5: c97d5fea534f8c8054369b28ed5d4a4c
Transpilation MD5: f970aecec57cf69dc7090b5e34cdd9e4
Author: stefan
Category: javax
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-09-20 23:14:39
Source code size: 1316 bytes / 60 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 591 / 563
Referenced in: [show references]