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

!636

!629 // standard functions

main {
  psvm {
    String src = takeInput(args, "#2000397");
    List<String> cnc = parseCNC(src);
    for (int i = 1; i+2 < cnc.size(); i += 2) {
      if (cnc.get(i).equals("class")
        && cnc.get(i+2).equals("main")) {
        int j = i;
        while (j < cnc.size() && !cnc.get(j).equals("{"))
          j += 2;
        int k = findEndOfBlock(cnc, j);
        printExcerpt(cnc, i, k);
        break;
      }
    }
  }
  
  static int findEndOfBlock(List<String> cnc, int i) {
    int j = i+2, level = 1;
    while (j < cnc.size()) {
      if (cnc.get(j).equals("{")) ++level;
      else if (cnc.get(j).equals("}")) --level;
      if (level == 0)
        return j+1;
      ++j;
    }
    return cnc.size();
  }
  
  static void printExcerpt(List<String> cnc, int i, int j) {
    StringBuilder out = new StringBuilder();
    for (; i < j; i++)
      out.append(cnc.get(i));
    System.out.println(out.toString());
  }
  
  public static String unquote(String s) {
    if (s.startsWith("\"") && s.endsWith("\"") && s.length() > 1)
      return s.substring(1, s.length()-1).replace("\\\"", "\"").replace("\\n", "\n").replace("\\r", "\r").replace("\\\\", "\\"); // SHOULD work...
    return s;
  }
  
  static String takeInput(String[] args, String defSnippet) tex {
    if (args.length != 0)
      return loadSnippet(args[0]);
    String s = loadTextFile("input/input.txt", null);
    if (s != null) return s;
    return loadSnippet(defSnippet);
  }
  
  static List<String> parseCNC(String src) {
    List<String> list = new ArrayList<String>();
    for (String s : toLines(src))
      list.add(unquote(s));
    return list;
  }
}

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