01: package kawa.lang;
02:
03: import gnu.lists.*;
04: import java.io.*;
05: import gnu.text.Printable;
06:
07: public class PairPat extends Pattern implements Printable,
08: Externalizable {
09: Pattern car;
10: Pattern cdr;
11: private int car_count, cdr_count;
12:
13: public PairPat() {
14: }
15:
16: public PairPat(Pattern car, Pattern cdr) {
17: this .car = car;
18: this .cdr = cdr;
19: car_count = car.varCount();
20: cdr_count = cdr.varCount();
21: }
22:
23: public static PairPat make(Pattern car, Pattern cdr) {
24: return new PairPat(car, cdr);
25: }
26:
27: public boolean match(Object obj, Object[] vars, int start_vars) {
28: if (!(obj instanceof Pair))
29: return false;
30: Pair pair = (Pair) obj;
31: return (car.match(pair.car, vars, start_vars) && cdr.match(
32: pair.cdr, vars, start_vars + car_count));
33: }
34:
35: public void print(Consumer out) {
36: out.write("#<pair-pattern car: ");
37: car.print(out);
38: out.write(" cdr: ");
39: cdr.print(out);
40: out.write('>');
41: }
42:
43: public int varCount() {
44: return car_count + cdr_count;
45: }
46:
47: /**
48: * @serialData Write the car and then the cdr patterns (using writeObject).
49: */
50: public void writeExternal(ObjectOutput out) throws IOException {
51: out.writeObject(car);
52: out.writeObject(cdr);
53: }
54:
55: public void readExternal(ObjectInput in) throws IOException,
56: ClassNotFoundException {
57: car = (Pattern) in.readObject();
58: cdr = (Pattern) in.readObject();
59: }
60: }
|