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

206
LINES

< > BotCompany Repo | #1002711 // snlToTree

JavaX fragment (include)

1  
static boolean snlToTree_debug = false;
2  
3  
static Lisp snlToTree(S snl) {
4  
  ret snlToTree(snlParse(snl));
5  
}
6  
7  
static Lisp snlToTree(Explain e) {
8  
  class T {
9  
    L<Explain> scanJuxta(Explain e) {
10  
      boolean isJuxta = isA(e, "juxta");
11  
      if (snlToTree_debug)
12  
        print("scanJuxta " + e.string() + " " + isJuxta);
13  
      if (isJuxta) {
14  
        Explain juxta = descend(e, "juxta");
15  
        Explain a = juxta.sub(0), b = juxta.sub(1);
16  
        ret concatLists(scanJuxta(a), scanJuxta(b));
17  
      } else
18  
        ret litlist(e);
19  
    }
20  
    
21  
    boolean isSame(Explain a, Explain b) {
22  
      ret a.fromToken() == b.fromToken() && a.toToken() == b.toToken();
23  
    }
24  
    
25  
    L<S> getClasses(Explain e) {
26  
      new L<S> l;
27  
      while (e != null) {
28  
        l.add(e.className());
29  
        if (l(e.subs) == 1 && e.sub(0) != null && isSame(e, e.sub(0)))
30  
          e = e.sub(0);
31  
        else
32  
          break;
33  
      }
34  
      ret l;
35  
    }
36  
    
37  
    Explain castTo(Explain e, S className) {
38  
      ret descend(e, className);
39  
    }
40  
    
41  
    Explain descend(Explain e, S className) {
42  
      new L<S> l;
43  
      while (e != null) {
44  
        if (eq(e.className(), className))
45  
          ret e;
46  
        if (l(e.subs) == 1)
47  
          e = e.sub(0);
48  
        else
49  
          break;
50  
      }
51  
      ret null;
52  
    }
53  
    
54  
    boolean isA(Explain e, S className) {
55  
      ret getClasses(e).contains(className);
56  
    }
57  
    
58  
    boolean allAre(L<Explain> l, S className) {
59  
      for (Explain e: l)
60  
        if (!isA(e, className))
61  
          ret false;
62  
      ret true;
63  
    }
64  
    
65  
    boolean anyAre(L<Explain> l, S className) {
66  
      for (Explain e : l)
67  
        if (isA(e, className))
68  
          ret true;
69  
      ret false;
70  
    }
71  
    
72  
    L<S> getOperators(L<Explain> parts) {
73  
      new L<S> l;
74  
      for (Explain e : parts)
75  
        l.add(makeOperator(e));
76  
      ret l;
77  
    }
78  
    
79  
    // true = it's a sub part
80  
    boolean[] opMap(L<Explain> parts) {
81  
      boolean[] map = new boolean[l(parts)];
82  
      if (anyAre(parts, "symbol")) {
83  
        for (int i = 0; i < l(parts); i++)
84  
          if (!isA(parts.get(i), "symbol"))
85  
            map[i] = true;
86  
      } else
87  
        for (int i = 0; i < l(parts); i++)
88  
          //if (!isA(parts.get(i), "extidentifier"))
89  
          if (!startsWithLetter(parts.get(i).string()))
90  
            map[i] = true;
91  
      ret map;
92  
    }
93  
    
94  
    S makeOperatorFromJuxta(L<Explain> parts) {
95  
      new L<S> l;
96  
      boolean[] map = opMap(parts);
97  
      for (int i = 0; i < l(parts); i++) {
98  
        //print("Operator part " + (++i) + ": " + e.string() + " / " + structure(getClasses(e)));
99  
        
100  
        if (map[i])
101  
          l.add("*");
102  
        else
103  
          l.add(parts.get(i).string());
104  
      }
105  
      S op = join(" ", l);
106  
      if (snlToTree_debug)
107  
        print("makeOperatorFromJuxta " + structure(parts) + " => " + op);
108  
      ret op;
109  
    }
110  
    
111  
    L<Explain> getJuxtaArgs(L<Explain> parts) {
112  
      new L<Explain> l;
113  
      boolean[] map = opMap(parts);
114  
      for (int i = 0; i < l(parts); i++)
115  
        if (map[i])
116  
          l.add(parts.get(i));
117  
      ret l;
118  
    }
119  
    
120  
    S makeOperator(Explain e) {
121  
      if (isA(e, "realarrow"))
122  
        ret descend(e, "realarrow").sub(0).string() + " *";
123  
      else if (isA(e, "juxta")) {
124  
        L<Explain> j = scanJuxta(e);
125  
        //print("scanJuxta => " + structure(j));
126  
        ret makeOperatorFromJuxta(j);
127  
      } else
128  
        ret e.string();
129  
    }
130  
    
131  
    Lisp getTree(Explain e) {
132  
      //print("getTree " + structure(getClasses(e)));
133  
      
134  
      if (isA(e, "realarrow")) {
135  
        //print("realarrow");
136  
        Explain a = castTo(e, "realarrow");
137  
        ret snlToTree_lisp(e, "<", getTree(a.sub(0)), getTree(a.sub(1)));
138  
      }
139  
      
140  
      if (isA(e, "realarrowr")) {
141  
        //print("realarrowr");
142  
        Explain a = castTo(e, "realarrowr");
143  
        ret snlToTree_lisp(e, ">", getTree(a.sub(0)), getTree(a.sub(1)));
144  
      }
145  
      
146  
      if (isA(e, "square")) {
147  
        //print("square");
148  
        Explain a = castTo(e, "square");
149  
        ret snlToTree_lisp(e, "[]", getTree(a.sub(0)));
150  
      }
151  
      
152  
      if (isA(e, "round")) {
153  
        Explain a = castTo(e, "round");
154  
        ret snlToTree_lisp(e, "()", getTree(a.sub(0)));
155  
      }
156  
      
157  
      if (isA(e, "juxta")) {
158  
        Explain juxta = descend(e, "juxta");
159  
        L<Explain> parts = scanJuxta(juxta);
160  
        
161  
        if (snlToTree_debug) {
162  
          print("juxta " + e.string());
163  
          for (int i = 0; i < l(parts); i++)
164  
            print("  part " + parts.get(i).string() + " " + structure(getClasses(parts.get(i))));
165  
        }
166  
        
167  
        if (l(parts) == 2 && eq(parts.get(0).string(), "="))
168  
          ret snlToTree_lisp(e, "= *", removeBrackets(getTree(parts.get(1))));
169  
        else /*if (anyAre(parts, "subword") || anyAre(parts, "ucid")) */ {
170  
          if (snlToTree_debug)
171  
            print("subwords!");
172  
            
173  
          L<Explain> args = getJuxtaArgs(parts);
174  
          Lisp l = snlToTree_lisp(e, makeOperatorFromJuxta(parts));
175  
          for (Explain arg : args)
176  
            l.add(removeBrackets(getTree(arg)));
177  
          ret l; // snlSimplifyJuxta(l);
178  
        }
179  
    
180  
        // fall back to simple string
181  
      }
182  
      
183  
      ret snlToTree_lisp(e, e.string());
184  
    }
185  
186  
    Lisp removeBrackets(Lisp l) {
187  
      while (l != null && eq(l.head, "[]") && l.size() == 1)
188  
        l = l.get(0);
189  
      ret l;
190  
    }
191  
  }
192  
  
193  
  if (e == null) ret null;
194  
  ret simplifySNLTree(new T().getTree(e));
195  
}
196  
197  
static Lisp snlToTree_lisp(Explain e, S head, Lisp... args) {
198  
  Lisp l = lisp(head, args);
199  
  
200  
  // this is problematic with serialization...
201  
  //new SNLInfo info;
202  
  //info.originalExplain = e;
203  
  //l.more = info;
204  
  
205  
  ret l;
206  
}

Author comment

Began life as a copy of #1002689

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: #1002711
Snippet name: snlToTree
Eternal ID of this version: #1002711/1
Text MD5: 55f2def4b8584bb845ff079459660828
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-02-25 20:48:36
Source code size: 5828 bytes / 206 lines
Pitched / IR pitched: No / No
Views / Downloads: 637 / 1102
Referenced in: [show references]