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

1  
!752
2  
3  
answer {
4  
  if (!attn()) ret null;
5  
  
6  
  if (jmatchStart("tree", s, m)) {
7  
    S snl = m.rest().trim();
8  
    L<S> tok = codeTokensOnly(nlTok(snl));
9  
    //new L<S> out;
10  
    //printTree(tok, i, out);
11  
    //ret slackSnippet(out);
12  
    ret structure(getTree(tok));
13  
  ]
14  
}
15  
16  
static Lisp tree(L<S> tok) {
17  
  ret tree(tok, 0, l(tok));
18  
}
19  
20  
static Lisp tree(L<S> tok, int i, int j) {
21  
  ret lisp(join(" ", subList(tok, 0, i)));
22  
}
23  
24  
static Lisp getTree(L<S> tok) {
25  
  if (empty(tok)) ret lisp("_empty");
26  
  int i = indexOfOperator(tok, 0);
27  
  if (i >= l(tok))
28  
    ret tree(tok);
29  
    
30  
  S op = tok.get(i);
31  
  if (eq(op, "<")) {
32  
    ret lisp("* < *",
33  
      getTree(subList(tok, 0, i)),
34  
      getTree(subList(tok, i+1));
35  
  } else if (eq(op, ">")) {
36  
    ret lisp("* > *",
37  
      getTree(subList(tok, 0, i)),
38  
      getTree(subList(tok, i+1));
39  
  } else if (eq(op, "[")) {
40  
    int j = snlEndOfBrackets(tok, i);
41  
    
42  
  }
43  
}
44  
45  
// i must point at the opening bracket (any of the 2 types, not type parameters)
46  
// index returned is index of closing bracket + 1
47  
static int snlEndOfBrackets(List<String> tok, int i) {
48  
  int j = i+1, level = 1;
49  
  while (j < tok.size()) {
50  
    if (litlist("[", "(").contains(tok.get(j))) ++level;
51  
    else if (litlist("]", ")").contains(tok.get(j))) --level;
52  
    if (level == 0)
53  
      retj+1;
54  
    ++j;
55  
  }
56  
  return l(tok);
57  
}
58  
59  
/*
60  
static void printTree(L<S> tok, int indent, L<S> out) {
61  
  if (empty(tok)) ret;
62  
  int i = indexOfOperator(tok, 0);
63  
  if (i >= l(tok)) {
64  
    out.add(indent(indent) + join(" ", tok));
65  
    ret;
66  
  }
67  
  S op = tok.get(i);
68  
  if (eq(op, "<")) {
69  
    out.add(indent(indent) + join(" ", subList(tok, 0, i)));
70  
    printTree(subList(tok, i+1));
71  
  } else if (eq(op, ">")) {
72  
    out.add(indent(indent) + join(" ", subList(tok, 0, i)));
73  
    printTree(subList(tok, i+1));
74  
  }
75  
}*/
76  
77  
static 
78  
79  
static int indexOfOperator(L<S> tok, int i) {
80  
  while (i < l(tok) && !isOperator(tok.get(i))) ++i;
81  
  ret i;
82  
}
83  
84  
static L<S> operators = litlist("<", ">", "[", "]", "(", ")");
85  
86  
static boolean isOperator(S t) {
87  
  ret operators.contains(t);
88  
}

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: 586 / 544
Referenced in: [show references]