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

void org::mathpiper::lisp::userfunctions::SingleArityBranchingUserFunction::evaluate ( Environment  aEnvironment,
ConsPointer  aResult,
ConsPointer  aArgumentsPointer 
) throws Exception [inline]

Evaluate the function with the given arguments. First, all arguments are evaluated by the evaluator associated with aEnvironment, unless the iHold flag of the corresponding parameter is true. Then a new LocalFrame is constructed, in which the actual arguments are assigned to the names of the formal arguments, as stored in iParameter. Then all rules in iRules are tried one by one. The body of the getFirstPointer rule that matches is evaluated, and the result is put in aResult. If no rule matches, aResult will recieve a new expression with evaluated arguments.

Parameters:
aResult (on output) the result of the evaluation
aEnvironment the underlying Lisp environment
aArguments the arguments to the function
Exceptions:
java.lang.Exception 

Definition at line 99 of file SingleArityBranchingUserFunction.java.

References arity(), org::mathpiper::lisp::cons::ConsPointer::cdr(), org::mathpiper::lisp::userfunctions::Branch::getBodyPointer(), org::mathpiper::lisp::cons::ConsPointer::getCons(), org::mathpiper::lisp::userfunctions::Branch::getPrecedence(), iBranchRules, iParameters, org::mathpiper::lisp::userfunctions::Branch::matches(), and org::mathpiper::lisp::cons::ConsPointer::setCons().

Referenced by org::mathpiper::lisp::LispExpressionEvaluator::evaluate().

                                                                                                                        {
        int arity = arity();
        ConsPointer[] argumentsResultPointerArray = evaluateArguments(aEnvironment, aArgumentsPointer);
        int parameterIndex;

        // Create a new local variables frame that has the same fenced state as this function.
        aEnvironment.pushLocalFrame(fenced(), this.functionName);



        try {
            // define the local variables.
            for (parameterIndex = 0; parameterIndex < arity; parameterIndex++) {
                String variableName = ((FunctionParameter) iParameters.get(parameterIndex)).iParameter;
                // set the variable to the new value
                aEnvironment.newLocalVariable(variableName, argumentsResultPointerArray[parameterIndex].getCons());
            }

            // walk the rules database, returning the evaluated result if the
            // predicate is true.
            int numberOfRules = iBranchRules.size();

            UserStackInformation userStackInformation = aEnvironment.iLispExpressionEvaluator.stackInformation();

            for (parameterIndex = 0; parameterIndex < numberOfRules; parameterIndex++) {
                Branch thisRule = ((Branch) iBranchRules.get(parameterIndex));
                LispError.lispAssert(thisRule != null);

                userStackInformation.iRulePrecedence = thisRule.getPrecedence();

                boolean matches = thisRule.matches(aEnvironment, argumentsResultPointerArray);

                if (matches) {

                    /* Rule dump trace code. */
                    if (isTraced() && showFlag) {
                        ConsPointer argumentsPointer = new ConsPointer();
                        argumentsPointer.setCons(SublistCons.getInstance(aEnvironment,aArgumentsPointer.getCons()));
                        String ruleDump = org.mathpiper.lisp.Utility.dumpRule(thisRule, aEnvironment, this);
                        Evaluator.traceShowRule(aEnvironment, argumentsPointer, ruleDump);
                    }

                    userStackInformation.iSide = 1;

                    aEnvironment.iLispExpressionEvaluator.evaluate(aEnvironment, aResult, thisRule.getBodyPointer());

                    /*Leave trace code */
                    if (isTraced() && showFlag) {
                        ConsPointer argumentsPointer2 = new ConsPointer();
                        argumentsPointer2.setCons(SublistCons.getInstance(aEnvironment,aArgumentsPointer.getCons()));
                        String localVariables = aEnvironment.getLocalVariables();
                        Evaluator.traceShowLeave(aEnvironment, aResult, argumentsPointer2, functionType, localVariables);
                        argumentsPointer2.setCons(null);
                    }//end if.

                    return;
                }//end if matches.

                // If rules got inserted, walk back
                while (thisRule != ((Branch) iBranchRules.get(parameterIndex)) && parameterIndex > 0) {
                    parameterIndex--;
                }
            }//end for.


            // No predicate was true: return a new expression with the evaluated
            // arguments.
            ConsPointer full = new ConsPointer();
            full.setCons(aArgumentsPointer.getCons().copy( aEnvironment, false));
            if (arity == 0) {
                full.cdr().setCons(null);
            } else {
                full.cdr().setCons(argumentsResultPointerArray[0].getCons());
                for (parameterIndex = 0; parameterIndex < arity - 1; parameterIndex++) {
                    argumentsResultPointerArray[parameterIndex].cdr().setCons(argumentsResultPointerArray[parameterIndex + 1].getCons());
                }
            }
            aResult.setCons(SublistCons.getInstance(aEnvironment,full.getCons()));


            /* Trace code */
            if (isTraced() && showFlag) {
                ConsPointer argumentsPointer3 = new ConsPointer();
                argumentsPointer3.setCons(SublistCons.getInstance(aEnvironment,aArgumentsPointer.getCons()));
                String localVariables = aEnvironment.getLocalVariables();
                Evaluator.traceShowLeave(aEnvironment, aResult, argumentsPointer3, functionType, localVariables);
                argumentsPointer3.setCons(null);
            }

        } catch (Exception e) {
            throw e;
        } finally {
            aEnvironment.popLocalFrame();
        }
    }


Generated by  Doxygen 1.6.0   Back to index