PairPat.java 1.4 KB

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