Logo Search packages:      
Sourcecode: mathpiper version File versions  Download package

PatternParameter org::mathpiper::lisp::parametermatchers::Pattern::makeParamMatcher ( Environment  aEnvironment,
Cons  aPattern 
) throws Exception [inline, protected]

Construct a pattern matcher out of a Lisp expression. The result of this function depends on the value of aPattern:

  • If aPattern is a number, the corresponding Number is constructed and returned.
  • If aPattern is an atom, the corresponding AtomCons is constructed and returned.
  • If aPattern is a list of the form ( _ var ), where var is an atom, lookUp() is called on var. Then the correspoding Variable is constructed and returned.
  • If aPattern is a list of the form ( _ var expr ), where var is an atom, lookUp() is called on var. Then, expr is appended to iPredicates. Finally, the correspoding Variable is constructed and returned.
  • If aPattern is a list of another form, this function calls itself on any of the entries in this list. The resulting PatternParameter objects are collected in a SublistCons, which is returned.
  • Otherwise, this function returns null.

Definition at line 213 of file Pattern.java.

References org::mathpiper::lisp::cons::Cons::car(), org::mathpiper::lisp::cons::Cons::cdr(), org::mathpiper::lisp::cons::Cons::copy(), org::mathpiper::lisp::cons::ConsTraverser::getCons(), org::mathpiper::lisp::cons::ConsPointer::getCons(), org::mathpiper::lisp::cons::ConsTraverser::goNext(), iPredicates, lookUp(), and org::mathpiper::lisp::cons::ConsPointer::setCons().

Referenced by Pattern().

                                                                                                          {
        if (aPattern == null) {
            return null;
        }
        //LispError.check(aPattern.type().equals("Number"), LispError.INVALID_ARGUMENT);
        if (aPattern.getNumber(aEnvironment.getPrecision()) != null) {
            return new Number((BigNumber) aPattern.getNumber(aEnvironment.getPrecision()));
        }
        // Deal with atoms
        if (aPattern.car() instanceof String) {
            return new Atom( (String) aPattern.car());
        }

        // Else it must be a sublist
        if (aPattern.car() instanceof ConsPointer) {
            // See if it is a variable template:
            ConsPointer sublist = (ConsPointer) aPattern.car();
            //LispError.lispAssert(sublist != null);

            int num = Utility.listLength(sublist);

            // variable matcher here...
            if (num > 1) {
                Cons head = sublist.getCons();
                if (((String) head.car()) == aEnvironment.getTokenHash().lookUp("_")) {
                    Cons second = head.cdr().getCons();
                    if (second.car() instanceof String) {
                        int index = lookUp( (String) second.car());

                        // Make a predicate for the type, if needed
                        if (num > 2) {
                            ConsPointer third = new ConsPointer();

                            Cons predicate = second.cdr().getCons();
                            if ( (predicate.car() instanceof ConsPointer)) {
                                Utility.flatCopy(aEnvironment, third, (ConsPointer) predicate.car());
                            } else {
                                third.setCons(second.cdr().getCons().copy( aEnvironment, false));
                            }

                            String str = (String) second.car();
                            Cons last = third.getCons();
                            while (last.cdr().getCons() != null) {
                                last = last.cdr().getCons();
                            }

                            last.cdr().setCons(org.mathpiper.lisp.cons.AtomCons.getInstance(aEnvironment, str));

                            ConsPointer pred = new ConsPointer();
                            pred.setCons(org.mathpiper.lisp.cons.SublistCons.getInstance(aEnvironment,third.getCons()));

                            iPredicates.add(pred);
                        }
                        return new Variable(index);
                    }
                }
            }

            PatternParameter[] matchers = new PatternParameter[num];

            int i;
            ConsTraverser consTraverser = new ConsTraverser(sublist);
            for (i = 0; i < num; i++) {
                matchers[i] = makeParamMatcher(aEnvironment, consTraverser.getCons());
                LispError.lispAssert(matchers[i] != null);
                consTraverser.goNext();
            }
            return new Sublist(matchers, num);
        }

        return null;
    }


Generated by  Doxygen 1.6.0   Back to index