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

100
LINES

< > BotCompany Repo | #1002001 // Port bot.py to JavaX

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Libraryless. Click here for Pure Java version (1523L/10K/32K).

!752

p {
  L<S> tok = pythonToJavaTok(loadSnippet("#1001999"));
  L<L<S>> lines = pythonToLines(tok);
  new L<S> out;
  out.add("!752\n");
  new L<S> statements;
  for (int i = 0; i < l(lines); i++) {
    L<S> l = lines.get(i);
    //print("Line: " + quote(join(l)));
    if (l(l) == 1) {
      out.add(join(l));
      continue;
    }
    new Matches m;
    if (mymatch("def *(*):", l, m)) {
      int j = endOfBlock(lines, i+1, " ");
      out.add("static S " + m.get(0) + "(S " + m.get(1) + ") {");
      for (int k = i+1; k < j; k++)
        out.add(convertStatement(lines.get(k)));
      out.add("}");
      i = j-1;
    } else {
      if (mymatchStart("* = ", l, m))
        statements.add("S " + m.get(0) + ";");
      statements.add(convertStatement(l));
      //out.add("// unknown line: " + join(l));
    }
  }
  S java = fromLines(concatLists(
    out,
    litlist("p {"),
    indent("  ", statements),
    litlist("}")
  ));
  print(java);
}

static S convertStatement(L<S> l) {
  l.set(l(l)-2, l.get(l(l)-2) + ";");
  ret join(l);
}

static boolean mymatch(S thepat, L<S> tok, Matches m) {
  L<S> pat = pythonToJavaTok(thepat);
  if (pat.size() != tok.size()) return false;
  ret mymatchStart(pat, tok, m);
}

static boolean mymatchStart(S thepat, L<S> tok, Matches m) {
  L<S> pat = pythonToJavaTok(thepat);
  ret mymatchStart(pat, tok, m);
}

static boolean mymatchStart(L<S> pat, L<S> tok, Matches m) {
  new L<S> result;
  if (tok.size() < pat.size()) return false;
  for (int i = 1; i < pat.size(); i += 2) {
    S p = pat.get(i), t = tok.get(i);
    if (eq(p, "*"))
      result.add(t);
    else if (neq(p, t))
      return false;
  }
  if (m != null) m.m = result.toArray(new S[result.size()]);
  ret true;
}

static int endOfBlock(L<L<S>> lines, int i, S indent) {
  while (i < l(lines) && lines.get(i).get(0).startsWith(indent))
    ++i;
  ret i;
}

static L<L<S>> pythonToLines(L<S> tok) {
  new L<L<S>> lines;
  for (S line : toLines(join(tok)))
    lines.add(javaTok(line));
  ret lines;
}

/* TODO - too complicated
static L<L<S>> tokToLines(L<S> tok) {
  new L<S> lines;
  int i = 0; // i and j are on whitespace tokens
  while (i < l(tok)-3) {
    int j = i;
    while (j < l(tok)-1 && !tok.get(j).contains("\n"))
      j += 2;
    if (i == j)
      lines.add(litlist(""));
    else
      lines.add(concatLists(litlist(""), tok.subList(i+1, j), litlist("")));
    i = j;
  }
  lines.add(
  ret lines;
}
*/

Author comment

Began life as a copy of #1002000

download  show line numbers  debug dex  old transpilations   

Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, teubizvjbppd, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1002001
Snippet name: Port bot.py to JavaX
Eternal ID of this version: #1002001/1
Text MD5: 077e43911f8c2acde5a2735f81a5fe91
Transpilation MD5: 8b0155477b9cc8f3c1d3c5ffb14be5f1
Author: stefan
Category: javax
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-12-11 21:19:56
Source code size: 2532 bytes / 100 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 815 / 799
Referenced in: #757 - Standing Challenge for relp.slack.com
#3000202 - Answer for stefanreich (>> T conversion bot)
#3000238 - Answer for stefanreich (>> t power bot)
#3000382 - Answer for ferdie (>> t = 1, f = 0)
#3000383 - Answer for funkoverflow (>> t=1, f=0 okay)