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

89
LINES

< > BotCompany Repo | #1000393 // Meta-programming example (implementation)

JavaX source code - run with: x30.jar

!636
//!* constructors
//!class JavaTok
!L<S>
!quicknew
!standard functions
//!multi-line strings
!699 // improved multi-line strings

!include #1000353 // class JavaTok

main {
  static Object in;
  static new (Hash)Map<String, Function> functions;
  
  interface Function {
    public Object process(Object[] args);
  }
  
  static class Call {
    String function;
    String[] args;
    
    Object eval() {
      return functions.get(function).process(args);
    }
    
    public String toString() {
      new StringBuilder buf;
      for (int i = 0; i < args.length; i++) {
        if (i != 0) buf.append(", ");
        buf.append(quote(args[i]));
      }
      return function + "(" + buf + ")";
    }
  }
  
  psvm {
    in = [[contains("<all iois>", "<latest ioi>")]];
    parseAsJavaExpression();
    
    replaceStringConstant("<all iois>", "#681");
    replaceStringConstant("<latest ioi>", "#1000384");
    loadSnippetID1();
    
    // replace contains(x, y) with contains(toLines(x), y)
    // use standard function "contains(List, Object)"
    
    functions.put("contains", new Function() {
      public Object process(Object[] args) {
        return toLinesTrim((String) args[0]).contains(args[1]);
      }
    });
    
    System.out.println("Result: " + ((Call) in).eval());
  }
  
  static void parseAsJavaExpression() {
    L<S> tok = JavaTok.split((String) in);
    new Call c;
    c.function = tok.get(1);
    c.args = new String[] { unquote(tok.get(5)), unquote(tok.get(9)) };
    in = c;
  }
  
  static void replaceStringConstant(String before, String now) {
    Call c = (Call) in;
    new Call c2;
    c2.function = c.function;
    c2.args = new String[c.args.length];
    for (int i = 0; i < c.args.length; i++)
      c2.args[i] = c.args[i].equals(before) ? now : c.args[i];
    in = c2;
  }
  
  static void loadSnippetID1() tex {
    Call c = (Call) in;
    new Call c2;
    c2.function = c.function;
    c2.args = new String[c.args.length];
    System.arraycopy(c.args, 0, c2.args, 0, c.args.length);
    for (int i = 0; i < c.args.length; i++)
      if (c.args[i].startsWith("#") && isSnippetID(c.args[i])) {
        c2.args[i] = loadSnippet(c.args[i]);
        break;
      }
    in = c2;
  }
}

Author comment

Began life as a copy of #1000392

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1000393
Snippet name: Meta-programming example (implementation)
Eternal ID of this version: #1000393/1
Text MD5: d611159be06ecba87d3bb8900130f46b
Author: stefan
Category:
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-08-01 18:37:58
Source code size: 2318 bytes / 89 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 665 / 533
Referenced in: #1000394 - Meta-programming example (implementation, adding automatic clone method)
#3000382 - Answer for ferdie (>> t = 1, f = 0)
#3000383 - Answer for funkoverflow (>> t=1, f=0 okay)