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

void org::mathpiper::lisp::LispExpressionEvaluator::evaluate ( Environment  aEnvironment,
ConsPointer  aResult,
ConsPointer  aExpression 
) throws Exception [inline]

First, the evaluation depth is checked. An error is raised if the maximum evaluation depth is exceeded. The next step is the actual evaluation. aExpression is a Cons, so we can distinguish three cases:

  1. If aExpression is a string starting with " , it is simply copied in aResult. If it starts with another character (this includes the case where it represents a number), the environment is checked to see whether a variable with this name exists. If it does, its value is copied in aResult, otherwise aExpression is copied.

  2. If aExpression is a list, the head of the list is examined. If the head is not a string. InternalApplyPure() is called. If the head is a string, it is checked against the core commands (if there is a check, the corresponding evaluator is called). Then it is checked agaist the list of user function with getUserFunction(). Again, the corresponding evaluator is called if there is a check. If all fails, ReturnUnEvaluated() is called.

  3. Otherwise (ie. if aExpression is a getJavaObject object), it is copied in aResult.

Note: The result of this operation must be a unique (copied) element! Eg. its Next might be set...

Parameters:
aEnvironment the Lisp environment, in which the evaluation should take place
aResult the result of the evaluation
aExpression the expression to evaluate
Exceptions:
java.lang.Exception 

Definition at line 76 of file LispExpressionEvaluator.java.

References org::mathpiper::lisp::cons::Cons::car(), org::mathpiper::lisp::cons::ConsPointer::cdr(), org::mathpiper::lisp::cons::Cons::copy(), org::mathpiper::lisp::userfunctions::SingleArityBranchingUserFunction::evaluate(), org::mathpiper::lisp::cons::ConsPointer::getCons(), and org::mathpiper::lisp::cons::ConsPointer::setCons().

                                                                                                                  {
        LispError.lispAssert(aExpression.getCons() != null);
        synchronized (aEnvironment) {
            aEnvironment.iEvalDepth++;
            if (aEnvironment.iEvalDepth >= aEnvironment.iMaxEvalDepth) {
                if (aEnvironment.iEvalDepth > aEnvironment.iMaxEvalDepth + 20) {
                    LispError.check(aEnvironment.iEvalDepth < aEnvironment.iMaxEvalDepth, LispError.USER_INTERRUPT);
                } else {
                    LispError.check(aEnvironment.iEvalDepth < aEnvironment.iMaxEvalDepth, LispError.MAXIMUM_RECURSE_DEPTH_REACHED);
                }
            }
        }



        // evaluate an atom: find the bound value (treat it as a variable)
        if ( aExpression.car() instanceof String) {
            String str = (String) aExpression.car();
            if (str.charAt(0) == '\"') {
                aResult.setCons(aExpression.getCons().copy( aEnvironment, false));
                aEnvironment.iEvalDepth--;
                return;
            }

            ConsPointer val = new ConsPointer();
            aEnvironment.getGlobalVariable(str, val);
            if (val.getCons() != null) {
                aResult.setCons(val.getCons().copy( aEnvironment, false));
                aEnvironment.iEvalDepth--;
                return;
            }
            aResult.setCons(aExpression.getCons().copy( aEnvironment, false));
            aEnvironment.iEvalDepth--;
            return;
        }
        {


            if ( aExpression.car() instanceof ConsPointer) {
                ConsPointer subList = (ConsPointer) aExpression.car();
                Cons head = subList.getCons();
                if (head != null) {
                    if (head.car() instanceof String) {
                        {
                            BuiltinFunctionEvaluator evaluator = (BuiltinFunctionEvaluator) aEnvironment.getBuiltinFunctions().lookUp( (String) head.car());
                            // Try to find a built-in command
                            if (evaluator != null) {
                                evaluator.evaluate(aEnvironment, aResult, subList);
                                aEnvironment.iEvalDepth--;
                                return;
                            }
                        }
                        {
                            SingleArityBranchingUserFunction userFunc;
                            userFunc = getUserFunction(aEnvironment, subList);
                            if (userFunc != null) {
                                userFunc.evaluate(aEnvironment, aResult, subList);
                                aEnvironment.iEvalDepth--;
                                return;
                            }
                        }
                    } else {
                        //printf("ApplyPure!\n");
                        ConsPointer oper = new ConsPointer();
                        ConsPointer args2 = new ConsPointer();
                        oper.setCons(subList.getCons());
                        args2.setCons(subList.cdr().getCons());
                        Utility.applyPure(oper, args2, aResult, aEnvironment);
                        aEnvironment.iEvalDepth--;
                        return;
                    }
                    //printf("**** Undef: %s\n",head.String().String());
                    Utility.returnUnEvaluated(aResult, subList, aEnvironment);
                    aEnvironment.iEvalDepth--;
                    return;
                }
            }
            aResult.setCons(aExpression.getCons().copy( aEnvironment, false));
        }
        aEnvironment.iEvalDepth--;
    }


Generated by  Doxygen 1.6.0   Back to index