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

81
LINES

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

JavaX fragment (include)

  // a Lisp-like form
  static class Lisp implements Iterable<Lisp> {
    S head;
    new L<Lisp> args;
    
    *() {}
    *(S *head) {}
    
    public S toString() {
      if (args.isEmpty())
        return quoteIfNotIdentifier(head);
      new L<S> bla;
      for (Lisp a : args)
        bla.add(a.toString());
      S inner = join(", ", bla);
      if (head.equals(""))
        return "{" + inner + "}"; // list
      else
        return quoteIfNotIdentifier(head) + "(" + inner + ")";
    }
    
    void add(Lisp l) {
      args.add(l);
    }
    
    void add(S s) {
      args.add(new Lisp(s));
    }
    
    int size() {
      return args.size();
    }
    
    boolean isEmpty() {
      return args.isEmpty();
    }
    
    Lisp get(int i) {
      return args.get(i);
    }
    
    S getString(int i) {
      return get(i).head;
    }
    
    S s(int i) {
      return getString(i);
    }
    
    boolean is(S... heads) {
      return asList(heads).contains(head);
    }
    
    public Iterator<Lisp> iterator() {
      return args.iterator();
    }
    
    Lisp subList(int fromIndex, int toIndex) {
      Lisp l = new Lisp(head);
      l.args.addAll(args.subList(fromIndex, toIndex)); // better to copy here I guess - safe
      return l;
    }
  }
  
  // make a lisp form
  static Lisp lisp(S head, Object... args) {
    Lisp l = new Lisp(head);
    for (O o : args)
      if (o instanceof String)
        l.args.add(new Lisp((String) o));
      else if (o instanceof Lisp)
        l.args.add((Lisp) o);
      else
        fail("Bad argument type: " + o);
    return l;
  }
  
  static Lisp lisplist(O... x) { return lisp("", x); }
  static Lisp enter(O... x) { return lisp("enter", x); }
  static Lisp answer(O... x) { return lisp("answer", x); }
  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: 681 / 1362
Referenced in: [show references]