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

88
LINES

< > BotCompany Repo | #1002707 // TREE - SNL to tree (dev., probably abandoned)

JavaX source code - run with: x30.jar

!752

answer {
  if (!attn()) ret null;
  
  if (jmatchStart("tree", s, m)) {
    S snl = m.rest().trim();
    L<S> tok = codeTokensOnly(nlTok(snl));
    //new L<S> out;
    //printTree(tok, i, out);
    //ret slackSnippet(out);
    ret structure(getTree(tok));
  ]
}

static Lisp tree(L<S> tok) {
  ret tree(tok, 0, l(tok));
}

static Lisp tree(L<S> tok, int i, int j) {
  ret lisp(join(" ", subList(tok, 0, i)));
}

static Lisp getTree(L<S> tok) {
  if (empty(tok)) ret lisp("_empty");
  int i = indexOfOperator(tok, 0);
  if (i >= l(tok))
    ret tree(tok);
    
  S op = tok.get(i);
  if (eq(op, "<")) {
    ret lisp("* < *",
      getTree(subList(tok, 0, i)),
      getTree(subList(tok, i+1));
  } else if (eq(op, ">")) {
    ret lisp("* > *",
      getTree(subList(tok, 0, i)),
      getTree(subList(tok, i+1));
  } else if (eq(op, "[")) {
    int j = snlEndOfBrackets(tok, i);
    
  }
}

// i must point at the opening bracket (any of the 2 types, not type parameters)
// index returned is index of closing bracket + 1
static int snlEndOfBrackets(List<String> tok, int i) {
  int j = i+1, level = 1;
  while (j < tok.size()) {
    if (litlist("[", "(").contains(tok.get(j))) ++level;
    else if (litlist("]", ")").contains(tok.get(j))) --level;
    if (level == 0)
      retj+1;
    ++j;
  }
  return l(tok);
}

/*
static void printTree(L<S> tok, int indent, L<S> out) {
  if (empty(tok)) ret;
  int i = indexOfOperator(tok, 0);
  if (i >= l(tok)) {
    out.add(indent(indent) + join(" ", tok));
    ret;
  }
  S op = tok.get(i);
  if (eq(op, "<")) {
    out.add(indent(indent) + join(" ", subList(tok, 0, i)));
    printTree(subList(tok, i+1));
  } else if (eq(op, ">")) {
    out.add(indent(indent) + join(" ", subList(tok, 0, i)));
    printTree(subList(tok, i+1));
  }
}*/

static 

static int indexOfOperator(L<S> tok, int i) {
  while (i < l(tok) && !isOperator(tok.get(i))) ++i;
  ret i;
}

static L<S> operators = litlist("<", ">", "[", "]", "(", ")");

static boolean isOperator(S t) {
  ret operators.contains(t);
}

Author comment

Began life as a copy of #1002700

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1002707
Snippet name: TREE - SNL to tree (dev., probably abandoned)
Eternal ID of this version: #1002707/1
Text MD5: b966e3877839519ace348140baa6fb1e
Author: stefan
Category: nl bots
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-02-16 01:19:24
Source code size: 2121 bytes / 88 lines
Pitched / IR pitched: No / No
Views / Downloads: 656 / 618
Referenced in: #3000382 - Answer for ferdie (>> t = 1, f = 0)
#3000383 - Answer for funkoverflow (>> t=1, f=0 okay)