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

131
LINES

< > BotCompany Repo | #1002696 // class Lisp (CURRENT)

JavaX fragment (include)

1  
// a Lisp-like form
2  
static class Lisp implements Iterable<Lisp> {
3  
  S head;
4  
  L<Lisp> args;
5  
  
6  
  // O more; // additional info, user-defined
7  
  
8  
  *() {}
9  
  *(S head) { this.head = _compactString(head); }
10  
  *(S *head, Lisp... args) {
11  
    argsForEdit().addAll(asList(args));
12  
  }
13  
  *(S head, Collection args) {
14  
    this.head = _compactString(head);
15  
    for (O arg : args) add(arg);
16  
  }
17  
  
18  
  L<Lisp> argsForEdit() {
19  
    ret args == null ? (args = new L) : args;
20  
  }
21  
  
22  
  // INEFFICIENT
23  
  public S toString() {
24  
    if (empty())
25  
      return quoteIfNotIdentifierOrInteger(head);
26  
    new L<S> bla;
27  
    for (Lisp a : args)
28  
      bla.add(a.toString());
29  
    S inner = join(", ", bla);
30  
    if (head.equals(""))
31  
      return "{" + inner + "}"; // list
32  
    else
33  
      return quoteIfNotIdentifier(head) + "(" + inner + ")";
34  
  }
35  
36  
  S raw() {
37  
    if (!isEmpty ()) fail("not raw: " + this);
38  
    ret head;
39  
  }
40  
  
41  
  Lisp add(Lisp l) {
42  
    argsForEdit().add(l);
43  
    ret this;
44  
  }
45  
  
46  
  Lisp add(S s) {
47  
    argsForEdit().add(new Lisp(s));
48  
    ret this;
49  
  }
50  
  
51  
  Lisp add(O o) {
52  
    if (o instanceof Lisp) add((Lisp) o);
53  
    else if (o instanceof S) add((S) o);
54  
    else fail("Bad argument type: " + structure(o));
55  
    ret this;
56  
  }
57  
  
58  
  int size() {
59  
    return l(args);
60  
  }
61  
  
62  
  bool empty() { ret main.empty(args); }
63  
  bool isEmpty() { ret main.empty(args); }
64  
  bool isLeaf() { ret main.empty(args); }
65  
  
66  
  Lisp get(int i) {
67  
    return main.get(args, i);
68  
  }
69  
  
70  
  S getString(int i) { Lisp a = get(i); ret a == null ? null : a.head; }
71  
  
72  
  S s(int i) { ret getString(i); }
73  
  S rawOrNull(int i) {
74  
    Lisp a = get(i); ret a != null && a.isLeaf() ? a.head : null;
75  
  }
76  
  S raw(int i) { ret assertNotNull(rawOrNull(i)); }
77  
  bool isLeaf(int i) { ret rawOrNull(i) != null; }
78  
  
79  
  boolean isA(S head) {
80  
    return eq(head, this.head);
81  
  }
82  
83  
  boolean is(S head, int size) {
84  
    ret isA(head) && size() == size;
85  
  }
86  
  
87  
  bool is(S head) { ret isA(head); }
88  
  bool headIs(S head) { ret isA(head); }
89  
  
90  
  boolean is(S... heads) {
91  
    return asList(heads).contains(head);
92  
  }
93  
  
94  
  // check head for one of these (ignore case)
95  
  boolean isic(S... heads) {
96  
    return containsIgnoreCase(heads, head);
97  
  }
98  
  
99  
  public Iterator<Lisp> iterator() {
100  
    return main.iterator(args);
101  
  }
102  
  
103  
  Lisp subList(int fromIndex, int toIndex) {
104  
    Lisp l = new Lisp(head);
105  
    l.argsForEdit().addAll(args.subList(fromIndex, toIndex)); // better to copy here I guess - safe
106  
    return l;
107  
  }
108  
109  
  public bool equals(O o) {
110  
    if (o == null || o.getClass() != Lisp.class) ret false;
111  
    Lisp l = cast o;
112  
    ret eq(head, l.head) && (isLeaf() ? l.isLeaf()
113  
      : l.args != null && eq(args, l.args));
114  
  }
115  
  
116  
  public int hashCode() {
117  
    ret head.hashCode() + main.hashCode(args);
118  
  }
119  
  
120  
  Lisp addAll(L args) {
121  
    for (O arg : args) add(arg);
122  
    ret this;
123  
  }
124  
  
125  
  S unquoted() { ret unquote(raw()); }
126  
  S unq() { ret unquoted(); }
127  
  S unq(int i) { ret get(i).unq(); }
128  
  
129  
  // heads of arguments
130  
  L<S> heads() { ret collect(args, "head"); }
131  
}

Author comment

Began life as a copy of #1001196

download  show line numbers  debug dex  old transpilations   

Travelled to 18 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, jtubtzbbkimh, lpdgvwnxivlt, lulzaavyztxj, mqqgnosmbjvj, onxytkatvevr, ppjhyzlbdabe, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt, wtqryiryparv

No comments. add comment

Snippet ID: #1002696
Snippet name: class Lisp (CURRENT)
Eternal ID of this version: #1002696/19
Text MD5: f0d3b91cfee26e64295001716900fda0
Author: stefan
Category: logic code
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-10-26 04:01:56
Source code size: 3122 bytes / 131 lines
Pitched / IR pitched: No / No
Views / Downloads: 934 / 5725
Version history: 18 change(s)
Referenced in: [show references]