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