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

44
LINES

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

JavaX fragment (include)

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

  new L out;
  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(new If(deRoundBracket(getCodeToken(tok, 1)),
        parsePythonesqueProcedure(joinSubList(tok, 3*2)), null));
    } else if (eqic(firstCodeToken(tok), "while")) {
      assertEquals(s, ":", getCodeToken(tok, 2));
      out.add(new While(deRoundBracket(getCodeToken(tok, 1)),
        parsePythonesqueProcedure(joinSubList(tok, 3*2))));
    } else if (eqic(firstCodeToken(tok), "else")) {
      O last = last(out);
      if (!last instanceof If) fail("Else without if");
      assertEquals(s, ":", getCodeToken(tok, 1));
      ((If) last).elseBlock = joinSubList(tok, 2*2);
    } else if (eqic(firstCodeToken(tok), "for")) {
      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);
  }
  //pnl("Parsed procedure ", out);
  ret out;
}

download  show line numbers  debug dex   

Travelled to 2 computer(s): mqqgnosmbjvj, tvejysmllsmz

No comments. add comment

Snippet ID: #1025614
Snippet name: parsePythonesqueProcedure (Include)
Eternal ID of this version: #1025614/2
Text MD5: 2f040ec7962e18a6312a41c0339b5bea
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-08 20:52:46
Source code size: 1709 bytes / 44 lines
Pitched / IR pitched: No / No
Views / Downloads: 3 / 94
Version history: 1 change(s)
Referenced in: [show references]