KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > scm > Cell


1                                 // Cons cell
2
package scm;
3
4 class Cell implements Obj
5 {
6   Obj car;
7   Cell cdr; // this should be a Obj, but its
8
// a mite quicker to bag the cast check,
9
// and be unable to do (a . b)
10
public Obj eval(Env e)
11     throws Exception JavaDoc
12   {
13     Procedure p;
14
15     if (car == null)
16       { throw new SchemeError("null car cell trying to eval " + this); }
17
18     if (car instanceof Procedure)
19       { p = (Procedure) car; }
20     else
21       { p = (Procedure) car.eval(e); }
22
23     return (p.apply(cdr, e)); // primitive ops override the default
24
// compound procedures by subclassing
25
}
26
27   Cell (Obj a, Cell b)
28   { car = a; cdr = b; }
29
30   public String JavaDoc toString()
31   {
32     return toString("");
33   }
34   public String JavaDoc toString(String JavaDoc s)
35   {
36     if (car == null) { s += "()"; }
37     else
38       { s += car.toString(); }
39
40     if (cdr == null)
41       { return ( "(" + s + ")"); }
42     else
43       { return (cdr.toString(s + " ")); }
44   }
45 }
46
47
Popular Tags