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

60
LINES

< > BotCompany Repo | #652 // Find main class (from CNC Tokenization)

JavaX source code - run with: x30.jar

1  
!636
2  
!629 // standard functions
3  
4  
main {
5  
  psvm {
6  
    String src = takeInput(args, "#2000397");
7  
    List<String> cnc = parseCNC(src);
8  
    for (int i = 1; i+2 < cnc.size(); i += 2) {
9  
      if (cnc.get(i).equals("class")
10  
        && cnc.get(i+2).equals("main")) {
11  
        int j = i;
12  
        while (j < cnc.size() && !cnc.get(j).equals("{"))
13  
          j += 2;
14  
        int k = findEndOfBlock(cnc, j);
15  
        printExcerpt(cnc, i, k);
16  
        break;
17  
      }
18  
    }
19  
  }
20  
  
21  
  static int findEndOfBlock(List<String> cnc, int i) {
22  
    int j = i+2, level = 1;
23  
    while (j < cnc.size()) {
24  
      if (cnc.get(j).equals("{")) ++level;
25  
      else if (cnc.get(j).equals("}")) --level;
26  
      if (level == 0)
27  
        return j+1;
28  
      ++j;
29  
    }
30  
    return cnc.size();
31  
  }
32  
  
33  
  static void printExcerpt(List<String> cnc, int i, int j) {
34  
    StringBuilder out = new StringBuilder();
35  
    for (; i < j; i++)
36  
      out.append(cnc.get(i));
37  
    System.out.println(out.toString());
38  
  }
39  
  
40  
  public static String unquote(String s) {
41  
    if (s.startsWith("\"") && s.endsWith("\"") && s.length() > 1)
42  
      return s.substring(1, s.length()-1).replace("\\\"", "\"").replace("\\n", "\n").replace("\\r", "\r").replace("\\\\", "\\"); // SHOULD work...
43  
    return s;
44  
  }
45  
  
46  
  static String takeInput(String[] args, String defSnippet) tex {
47  
    if (args.length != 0)
48  
      return loadSnippet(args[0]);
49  
    String s = loadTextFile("input/input.txt", null);
50  
    if (s != null) return s;
51  
    return loadSnippet(defSnippet);
52  
  }
53  
  
54  
  static List<String> parseCNC(String src) {
55  
    List<String> list = new ArrayList<String>();
56  
    for (String s : toLines(src))
57  
      list.add(unquote(s));
58  
    return list;
59  
  }
60  
}

Author comment

Began life as a copy of #2000396

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #652
Snippet name: Find main class (from CNC Tokenization)
Eternal ID of this version: #652/1
Text MD5: 3f1ba34ae30ca5111cd0281c2640fefa
Author: stefan
Category: javax
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-06-27 21:01:38
Source code size: 1729 bytes / 60 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 638 / 527
Referenced in: [show references]