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

86
LINES

< > BotCompany Repo | #1025614 // parsePythonesqueProcedure (Include)

JavaX fragment (include)

1  
bool parsePythonesqueProcedure_debug;
2  
3  
L parsePythonesqueProcedure(S proc) {
4  
  //printStruct(proc);
5  
  proc = withoutLinesEmptyAfterTrim(proc);
6  
  //printStruct(proc);
7  
  //proc = autoUnindent(proc);
8  
  proc = autoUnindent_honoringBrackets(proc);
9  
  //printStruct(proc);
10  
  //print(indentx("> ", proc));
11  
  
12  
  LS l = groupPythonStyleIndents_honoringBrackets(proc);
13  
  //pnl("unpythonized ", l);
14  
15  
  new L out;
16  
  If lastIf = null;
17  
  for i over l: {
18  
    S s = l.get(i);
19  
    LS tok = javaTokWithBrackets(s);
20  
    if (eqic(firstCodeToken(tok), "if")) {
21  
      assertEquals(s, ":", getCodeToken(tok, 2));
22  
      out.add(lastIf = new If(deRoundBracket(getCodeToken(tok, 1)),
23  
        parsePythonesqueProcedure(joinSubList(tok, 3*2)), null));
24  
      continue;
25  
    }
26  
    if (eqic(firstCodeToken(tok), "unless")) {
27  
      assertEquals(s, ":", getCodeToken(tok, 2));
28  
      out.add(lastIf = new If(deRoundBracket(getCodeToken(tok, 1)),
29  
        null, parsePythonesqueProcedure(joinSubList(tok, 3*2))));
30  
      continue;
31  
    }
32  
    if (eqic(firstCodeToken(tok), "while")) {
33  
      assertEquals(s, ":", getCodeToken(tok, 2));
34  
      out.add(new While(deRoundBracket(getCodeToken(tok, 1)),
35  
        parsePythonesqueProcedure(joinSubList(tok, 3*2))));
36  
      continue;
37  
    }
38  
    if (eqic(firstCodeToken(tok), "else") && eqic(secondCodeToken(tok), "if")) {
39  
      O last = last(out);
40  
      if (lastIf == null) fail("Else if without if");
41  
      assertEquals(s, ":", getCodeToken(tok, 3));
42  
      lastIf.elseBlock = lastIf = new If(deRoundBracket(getCodeToken(tok, 2)),
43  
        parsePythonesqueProcedure(joinSubList(tok, 4*2)), null);
44  
      continue;
45  
    }
46  
    if (eqic(firstCodeToken(tok), "else")) {
47  
      O last = last(out);
48  
      if (lastIf == null) fail("Else without if");
49  
      assertEquals(s, ":", getCodeToken(tok, 1));
50  
      lastIf.elseBlock = joinSubList(tok, 2*2);
51  
      continue;
52  
    }
53  
    
54  
    // assignment
55  
    if (eqGet(tok, 3, ":") && eqGet(tok, 5, "=")) {
56  
      out.add(new Assignment(get(tok, 1), joinSubList(tok, 7)));
57  
      continue;
58  
    }
59  
    
60  
    lastIf = null;
61  
    if (eqic(firstCodeToken(tok), "for")) {
62  
      if (eq(getCodeToken(tok, 2), "in")) {
63  
        int idx = indexOf(tok, ":");
64  
        assertTrue(s, idx >= 0);
65  
        S var = getCodeToken(tok, 1);
66  
        S expr = joinSubList(tok, 3*2+1, idx-1);
67  
        O body = parsePythonesqueProcedure(joinSubList(tok, idx+1));
68  
        out.add(new ForIn(var, expr, body));
69  
      } else {
70  
        assertEquals(s, ":", getCodeToken(tok, 2));
71  
        S cond = getCodeToken(tok, 1);
72  
        // cond looks like: "(y | x has a y)"
73  
        cond = deRoundBracket(cond);
74  
        LS tok2 = javaTok(cond);
75  
        assertEquals(cond, "|", getCodeToken(tok2, 1));
76  
        S var = assertIdentifier(cond, getCodeToken(tok2, 0));
77  
        S actualCondition = trimJoinSubList(tok2, 2*2+1);
78  
        out.add(new For(var, actualCondition, parsePythonesqueProcedure(joinSubList(tok, 3*2))));
79  
      }
80  
    } else
81  
      out.add(s);
82  
  }
83  
  if (parsePythonesqueProcedure_debug)
84  
    pnl("Parsed procedure ", out);
85  
  ret out;
86  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 10 computer(s): bhatertpkbcr, ekrmjmnbrukm, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt, whxojlpjdney, xrpafgyirdlv

No comments. add comment

Snippet ID: #1025614
Snippet name: parsePythonesqueProcedure (Include)
Eternal ID of this version: #1025614/14
Text MD5: a42463a6a296b740b559be2b30df9f01
Author: stefan
Category: javax / a.i.
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-03-18 16:21:36
Source code size: 3113 bytes / 86 lines
Pitched / IR pitched: No / No
Views / Downloads: 145 / 1263
Version history: 13 change(s)
Referenced in: [show references]