VarListPat.java 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package kawa.lang;
  2. import gnu.lists.*;
  3. /** A Pattern that will match a list of a given minimum length. */
  4. public class VarListPat extends Pattern {
  5. /** Minimun length of list that will match. */
  6. int min_length;
  7. public VarListPat (int min) { min_length = min; }
  8. /** Succeeds of obj is a list with at least min_length elements.
  9. * @param obj the object to match against
  10. * @return true if the match succeeded.
  11. * The elements vars[start_vars .. start_vars + min_length] contain
  12. * the first min_length elements of obj followed by the
  13. * min_length'th cdr of obj. */
  14. public boolean match (Object obj, Object[] vars, int start_vars)
  15. {
  16. int i;
  17. for (i = 0; i < min_length; i++)
  18. {
  19. if (obj instanceof Pair)
  20. {
  21. Pair p = (Pair)obj;
  22. vars[start_vars + i] = p.getCar();
  23. obj = p.getCdr();
  24. }
  25. else
  26. return false;
  27. }
  28. vars [start_vars + i] = obj;
  29. return true;
  30. }
  31. public int varCount () { return min_length + 1; }
  32. public void print (Consumer out)
  33. {
  34. out.write("#<varlist-pattern min:");
  35. out.writeInt(min_length);
  36. out.write('>');
  37. }
  38. }