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

86
LINES

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

JavaX fragment (include)

bool parsePythonesqueProcedure_debug;

L parsePythonesqueProcedure(S proc) {
  //printStruct(proc);
  proc = withoutLinesEmptyAfterTrim(proc);
  //printStruct(proc);
  //proc = autoUnindent(proc);
  proc = autoUnindent_honoringBrackets(proc);
  //printStruct(proc);
  //print(indentx("> ", proc));
  
  LS l = groupPythonStyleIndents_honoringBrackets(proc);
  //pnl("unpythonized ", l);

  new L out;
  If lastIf = null;
  for i over l: {
    S s = l.get(i);
    LS tok = javaTokWithBrackets(s);
    if (eqic(firstCodeToken(tok), "if")) {
      assertEquals(s, ":", getCodeToken(tok, 2));
      out.add(lastIf = new If(deRoundBracket(getCodeToken(tok, 1)),
        parsePythonesqueProcedure(joinSubList(tok, 3*2)), null));
      continue;
    }
    if (eqic(firstCodeToken(tok), "unless")) {
      assertEquals(s, ":", getCodeToken(tok, 2));
      out.add(lastIf = new If(deRoundBracket(getCodeToken(tok, 1)),
        null, parsePythonesqueProcedure(joinSubList(tok, 3*2))));
      continue;
    }
    if (eqic(firstCodeToken(tok), "while")) {
      assertEquals(s, ":", getCodeToken(tok, 2));
      out.add(new While(deRoundBracket(getCodeToken(tok, 1)),
        parsePythonesqueProcedure(joinSubList(tok, 3*2))));
      continue;
    }
    if (eqic(firstCodeToken(tok), "else") && eqic(secondCodeToken(tok), "if")) {
      O last = last(out);
      if (lastIf == null) fail("Else if without if");
      assertEquals(s, ":", getCodeToken(tok, 3));
      lastIf.elseBlock = lastIf = new If(deRoundBracket(getCodeToken(tok, 2)),
        parsePythonesqueProcedure(joinSubList(tok, 4*2)), null);
      continue;
    }
    if (eqic(firstCodeToken(tok), "else")) {
      O last = last(out);
      if (lastIf == null) fail("Else without if");
      assertEquals(s, ":", getCodeToken(tok, 1));
      lastIf.elseBlock = joinSubList(tok, 2*2);
      continue;
    }
    
    // assignment
    if (eqGet(tok, 3, ":") && eqGet(tok, 5, "=")) {
      out.add(new Assignment(get(tok, 1), joinSubList(tok, 7)));
      continue;
    }
    
    lastIf = null;
    if (eqic(firstCodeToken(tok), "for")) {
      if (eq(getCodeToken(tok, 2), "in")) {
        int idx = indexOf(tok, ":");
        assertTrue(s, idx >= 0);
        S var = getCodeToken(tok, 1);
        S expr = joinSubList(tok, 3*2+1, idx-1);
        O body = parsePythonesqueProcedure(joinSubList(tok, idx+1));
        out.add(new ForIn(var, expr, body));
      } else {
        assertEquals(s, ":", getCodeToken(tok, 2));
        S cond = getCodeToken(tok, 1);
        // cond looks like: "(y | x has a y)"
        cond = deRoundBracket(cond);
        LS tok2 = javaTok(cond);
        assertEquals(cond, "|", getCodeToken(tok2, 1));
        S var = assertIdentifier(cond, getCodeToken(tok2, 0));
        S actualCondition = trimJoinSubList(tok2, 2*2+1);
        out.add(new For(var, actualCondition, parsePythonesqueProcedure(joinSubList(tok, 3*2))));
      }
    } else
      out.add(s);
  }
  if (parsePythonesqueProcedure_debug)
    pnl("Parsed procedure ", out);
  ret out;
}

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: 270 / 1393
Version history: 13 change(s)
Referenced in: #1025597 - PhilosophyBot1 (LIVE)
#1027172 - PhilosophyBot1 (backup before trails)
#1027175 - PhilosophyBot1 (backup before trails 2)
#1027178 - PhilosophyBot1 with trails [dev.]