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

1  
!636
2  
//!* constructors
3  
//!class JavaTok
4  
!L<S>
5  
!quicknew
6  
!standard functions
7  
//!multi-line strings
8  
!699 // improved multi-line strings
9  
10  
!include #1000353 // class JavaTok
11  
12  
main {
13  
  static Object in;
14  
  static new (Hash)Map<String, Function> functions;
15  
  
16  
  interface Function {
17  
    public Object process(Object[] args);
18  
  }
19  
  
20  
  static class Call {
21  
    String function;
22  
    String[] args;
23  
    
24  
    Object eval() {
25  
      return functions.get(function).process(args);
26  
    }
27  
    
28  
    public String toString() {
29  
      new StringBuilder buf;
30  
      for (int i = 0; i < args.length; i++) {
31  
        if (i != 0) buf.append(", ");
32  
        buf.append(quote(args[i]));
33  
      }
34  
      return function + "(" + buf + ")";
35  
    }
36  
  }
37  
  
38  
  psvm {
39  
    in = [[contains("<all iois>", "<latest ioi>")]];
40  
    parseAsJavaExpression();
41  
    
42  
    replaceStringConstant("<all iois>", "#681");
43  
    replaceStringConstant("<latest ioi>", "#1000384");
44  
    loadSnippetID1();
45  
    
46  
    // replace contains(x, y) with contains(toLines(x), y)
47  
    // use standard function "contains(List, Object)"
48  
    
49  
    functions.put("contains", new Function() {
50  
      public Object process(Object[] args) {
51  
        return toLinesTrim((String) args[0]).contains(args[1]);
52  
      }
53  
    });
54  
    
55  
    System.out.println("Result: " + ((Call) in).eval());
56  
  }
57  
  
58  
  static void parseAsJavaExpression() {
59  
    L<S> tok = JavaTok.split((String) in);
60  
    new Call c;
61  
    c.function = tok.get(1);
62  
    c.args = new String[] { unquote(tok.get(5)), unquote(tok.get(9)) };
63  
    in = c;
64  
  }
65  
  
66  
  static void replaceStringConstant(String before, String now) {
67  
    Call c = (Call) in;
68  
    new Call c2;
69  
    c2.function = c.function;
70  
    c2.args = new String[c.args.length];
71  
    for (int i = 0; i < c.args.length; i++)
72  
      c2.args[i] = c.args[i].equals(before) ? now : c.args[i];
73  
    in = c2;
74  
  }
75  
  
76  
  static void loadSnippetID1() tex {
77  
    Call c = (Call) in;
78  
    new Call c2;
79  
    c2.function = c.function;
80  
    c2.args = new String[c.args.length];
81  
    System.arraycopy(c.args, 0, c2.args, 0, c.args.length);
82  
    for (int i = 0; i < c.args.length; i++)
83  
      if (c.args[i].startsWith("#") && isSnippetID(c.args[i])) {
84  
        c2.args[i] = loadSnippet(c.args[i]);
85  
        break;
86  
      }
87  
    in = c2;
88  
  }
89  
}

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: 599 / 464
Referenced in: [show references]