Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/memoire/silk/SilkPair.java


1   
2   
3   package com.memoire.silk;
4   import com.memoire.silk.*;
5   
6   
7   
8   /** A SilkPair has two fields, first and rest (or car and cdr). 
9    * The empty list is represented by null. The methods that you might
10   * expect here, like first, second, list, etc. are instead static methods
11   * in class SilkSchemeUtils. 
12   * @author Peter Norvig, peter@norvig.com http://www.norvig.com 
13   * Copyright 1998 Peter Norvig, see http://www.norvig.com/license.html */
14  
15  public class SilkPair extends SilkSchemeUtils {
16  
17      /** The first element of the pair. **/
18      public Object first;
19  
20      /** The other element of the pair. **/
21      public Object rest;
22  
23      /** Build a pair from two components. **/
24      public SilkPair(Object first, Object rest) { 
25    this.first = first; this.rest = rest; 
26      }
27  
28      /** Two pairs are equal if their first and rest fields are equal. **/
29      public boolean equals(Object x) {
30    if (x == this) return true;
31    else if (!(x instanceof SilkPair)) return false;
32    else {
33      SilkPair that = (SilkPair)x;
34      return equal(this.first, that.first)
35        && equal(this.rest, that.rest);
36    }
37      }
38  
39    /** Return a String representation of the pair. **/
40    public String toString() { return stringify(this, true); }
41  
42    /** Build up a String representation of the SilkPair in a StringBuffer. **/
43    void stringifySilkPair(boolean quoted, StringBuffer buf) {
44      String special = null;
45      if ((rest instanceof SilkPair) && rest(rest) == null) 
46        special = (first == "quote") ? "'" : (first == "quasiquote") ? "`"
47    : (first == "unquote") ? "," : (first == "unquote-splicing") ? ",@"
48    : null;
49    
50      if (special != null) {
51        buf.append(special); stringify(second(this), quoted, buf);
52      } else {
53        buf.append('(');
54        stringify(first, quoted, buf);
55        Object tail = rest;
56        while (tail instanceof SilkPair) {
57    buf.append(' ');
58    stringify(((SilkPair)tail).first, quoted, buf);
59    tail = ((SilkPair)tail).rest;
60        }
61        if (tail != null) {
62    buf.append(" . ");
63    stringify(tail, quoted, buf);
64        }
65        buf.append(')');
66      }
67    }
68  
69  }