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

120
LINES

< > BotCompany Repo | #1000394 // Meta-programming example (implementation, adding automatic clone method)

JavaX source code - run with: x30.jar

!636
//!* constructors
//!class JavaTok
!L<S>
!quicknew
!standard functions
//!multi-line strings
!699 // improved multi-line strings
!700 // "no exceptions" short hand

!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) clone(in);
    for (int i = 0; i < c.args.length; i++)
      if (c.args[i].equals(before))
        c.args[i] = now;
    in = c;
  }
  
  static void loadSnippetID1() tex {
    Call c = (Call) clone(in);
    for (int i = 0; i < c.args.length; i++)
      if (c.args[i].startsWith("#") && isSnippetID(c.args[i])) {
        c.args[i] = loadSnippet(c.args[i]);
        break;
      }
    in = c;
  }
  
  static Object clone(Object o) no exceptions {
    if (o == null)
      return o;
    
    if (o instanceof List) {
      List l = new ArrayList();
      for (Object x : (List) o)
        l.add(clone(x));
      return l;
    }
    
    if (o instanceof String || o instanceof Number || o instanceof Boolean)
      return o;
      
    if (o instanceof Object[]) {
      Object[] l = (Object[]) o;
      Object[] l2 = l.clone();
      for (int i = 0; i < l.length; i++)
        l2[i] = clone(l[i]);
      return l2;
    }
    
    // TODO: go to superclasses too
    Field[] fields = o.getClass().getDeclaredFields();
    Object clone = o.getClass().newInstance();
    for (Field field : fields) {
      if ((field.getModifiers() & Modifier.STATIC) != 0)
        continue;
      Object value;
      value = field.get(o);
      field.set(clone, clone(value));
    }
    return clone;
  }
  
}

Author comment

Began life as a copy of #1000393

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

Comments [hide]

ID Author/Program Comment Date
1047 stefan TODO: check if it still works (translators have changed) 2015-08-21 18:56:23

add comment

Snippet ID: #1000394
Snippet name: Meta-programming example (implementation, adding automatic clone method)
Eternal ID of this version: #1000394/1
Text MD5: 20b40abb47d667f619f3e3a401833742
Author: stefan
Category:
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-08-02 00:15:35
Source code size: 3050 bytes / 120 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 961 / 847
Referenced in: [show references]