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

81
LINES

< > BotCompany Repo | #1001196 // Lisp (include)

JavaX fragment (include)

1  
  // a Lisp-like form
2  
  static class Lisp implements Iterable<Lisp> {
3  
    S head;
4  
    new L<Lisp> args;
5  
    
6  
    *() {}
7  
    *(S *head) {}
8  
    
9  
    public S toString() {
10  
      if (args.isEmpty())
11  
        return quoteIfNotIdentifier(head);
12  
      new L<S> bla;
13  
      for (Lisp a : args)
14  
        bla.add(a.toString());
15  
      S inner = join(", ", bla);
16  
      if (head.equals(""))
17  
        return "{" + inner + "}"; // list
18  
      else
19  
        return quoteIfNotIdentifier(head) + "(" + inner + ")";
20  
    }
21  
    
22  
    void add(Lisp l) {
23  
      args.add(l);
24  
    }
25  
    
26  
    void add(S s) {
27  
      args.add(new Lisp(s));
28  
    }
29  
    
30  
    int size() {
31  
      return args.size();
32  
    }
33  
    
34  
    boolean isEmpty() {
35  
      return args.isEmpty();
36  
    }
37  
    
38  
    Lisp get(int i) {
39  
      return args.get(i);
40  
    }
41  
    
42  
    S getString(int i) {
43  
      return get(i).head;
44  
    }
45  
    
46  
    S s(int i) {
47  
      return getString(i);
48  
    }
49  
    
50  
    boolean is(S... heads) {
51  
      return asList(heads).contains(head);
52  
    }
53  
    
54  
    public Iterator<Lisp> iterator() {
55  
      return args.iterator();
56  
    }
57  
    
58  
    Lisp subList(int fromIndex, int toIndex) {
59  
      Lisp l = new Lisp(head);
60  
      l.args.addAll(args.subList(fromIndex, toIndex)); // better to copy here I guess - safe
61  
      return l;
62  
    }
63  
  }
64  
  
65  
  // make a lisp form
66  
  static Lisp lisp(S head, Object... args) {
67  
    Lisp l = new Lisp(head);
68  
    for (O o : args)
69  
      if (o instanceof String)
70  
        l.args.add(new Lisp((String) o));
71  
      else if (o instanceof Lisp)
72  
        l.args.add((Lisp) o);
73  
      else
74  
        fail("Bad argument type: " + o);
75  
    return l;
76  
  }
77  
  
78  
  static Lisp lisplist(O... x) { return lisp("", x); }
79  
  static Lisp enter(O... x) { return lisp("enter", x); }
80  
  static Lisp answer(O... x) { return lisp("answer", x); }
81  
  static Lisp or(O... x) { return lisp("or", x); }

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: #1001196
Snippet name: Lisp (include)
Eternal ID of this version: #1001196/1
Text MD5: 1c37b3017e1452046742265ebe420bc2
Author: stefan
Category: logic code
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-10-21 15:17:57
Source code size: 1902 bytes / 81 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 743 / 1430
Referenced in: [show references]