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

79
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;
    }
    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   

Travelled to 4 computer(s): mqqgnosmbjvj, onxytkatvevr, tvejysmllsmz, whxojlpjdney

No comments. add comment

Snippet ID: #1025614
Snippet name: parsePythonesqueProcedure (Include)
Eternal ID of this version: #1025614/13
Text MD5: b97c3efaacdd3797d36d8f2b2ace7ea1
Author: stefan
Category: javax / a.i.
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-10-19 20:20:32
Source code size: 2939 bytes / 79 lines
Pitched / IR pitched: No / No
Views / Downloads: 25 / 528
Version history: 12 change(s)
Referenced in: [show references]

Formerly at http://tinybrain.de/1025614 & http://1025614.tinybrain.de