From 9f187b46ae761e8275362d6c1533e9fe79028c7b Mon Sep 17 00:00:00 2001 From: jsdelfino Date: Sun, 1 Nov 2009 05:24:54 +0000 Subject: Improved memory management using APR memory pools, changed frame allocation in eval library to support forward references and fixed memory leak in XML parsing code. Also simplified a bit the printing of lists to make them easier to read. git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@831639 13f79535-47bb-0310-9956-ffa450edef68 --- cpp/sca/modules/eval/eval.hpp | 56 ++++++++++++++++++++++++------------------- 1 file changed, 31 insertions(+), 25 deletions(-) (limited to 'cpp/sca/modules/eval/eval.hpp') diff --git a/cpp/sca/modules/eval/eval.hpp b/cpp/sca/modules/eval/eval.hpp index 1496c3bd09..78051c5a2b 100644 --- a/cpp/sca/modules/eval/eval.hpp +++ b/cpp/sca/modules/eval/eval.hpp @@ -36,7 +36,7 @@ namespace tuscany { namespace eval { -const value evalApply(const value& exp, Env& env); +const value evalExpr(const value& exp, Env& env, const gc_pool& pool); const value compoundProcedureSymbol("compound-procedure"); const value procedureSymbol("procedure"); @@ -86,10 +86,10 @@ const list operands(const value& exp) { return cdr((list )exp); } -const list listOfValues(const list exps, Env& env) { +const list listOfValues(const list exps, Env& env, const gc_pool& pool) { if(isNil(exps)) return list (); - return cons(evalApply(car(exps), env), listOfValues(cdr(exps), env)); + return cons(evalExpr(car(exps), env, pool), listOfValues(cdr(exps), env, pool)); } const value applyOperat(const value& exp) { @@ -132,19 +132,19 @@ const value makeBegin(const list seq) { return cons(beginSymbol, seq); } -const value evalSequence(const list& exps, Env& env) { +const value evalSequence(const list& exps, Env& env, const gc_pool& pool) { if(isLastExp(exps)) - return evalApply(firstExp(exps), env); - evalApply(firstExp(exps), env); - return evalSequence(restExp(exps), env); + return evalExpr(firstExp(exps), env, pool); + evalExpr(firstExp(exps), env, pool); + return evalSequence(restExp(exps), env, pool); } -const value applyProcedure(const value& procedure, list& arguments) { +const value applyProcedure(const value& procedure, list& arguments, const gc_pool& pool) { if(isPrimitiveProcedure(procedure)) return applyPrimitiveProcedure(procedure, arguments); if(isCompoundProcedure(procedure)) { - Env env = extendEnvironment(procedureParameters(procedure), arguments, procedureEnvironment(procedure)); - return evalSequence(procedureBody(procedure), env); + Env env = extendEnvironment(procedureParameters(procedure), arguments, procedureEnvironment(procedure), pool); + return evalSequence(procedureBody(procedure), env, pool); } std::cout << "Unknown procedure type " << procedure << "\n"; return value(); @@ -218,46 +218,52 @@ value condToIf(const value& exp) { return expandClauses(condClauses(exp)); } -value evalIf(const value& exp, Env& env) { - if(isTrue(evalApply(ifPredicate(exp), env))) - return evalApply(ifConsequent(exp), env); - return evalApply(ifAlternative(exp), env); +value evalIf(const value& exp, Env& env, const gc_pool& pool) { + if(isTrue(evalExpr(ifPredicate(exp), env, pool))) + return evalExpr(ifConsequent(exp), env, pool); + return evalExpr(ifAlternative(exp), env, pool); } -const value evalDefinition(const value& exp, Env& env) { - defineVariable(definitionVariable(exp), evalApply(definitionValue(exp), env), env); +const value evalDefinition(const value& exp, Env& env, const gc_pool& pool) { + defineVariable(definitionVariable(exp), evalExpr(definitionValue(exp), env, pool), env); return definitionVariable(exp); } -const value evalApply(const value& exp, Env& env) { +const value evalExpr(const value& exp, Env& env, const gc_pool& pool) { if(isSelfEvaluating(exp)) return exp; if(isQuoted(exp)) return textOfQuotation(exp); if(isDefinition(exp)) - return evalDefinition(exp, env); + return evalDefinition(exp, env, pool); if(isIf(exp)) - return evalIf(exp, env); + return evalIf(exp, env, pool); if(isBegin(exp)) - return evalSequence(beginActions(exp), env); + return evalSequence(beginActions(exp), env, pool); if(isCond(exp)) - return evalApply(condToIf(exp), env); + return evalExpr(condToIf(exp), env, pool); if(isLambda(exp)) return makeProcedure(lambdaParameters(exp), lambdaBody(exp), env); if(isVariable(exp)) return lookupVariableValue(exp, env); if(isApply(exp)) { - list applyOperandValues = evalApply(applyOperand(exp), env); - return applyProcedure(evalApply(applyOperat(exp), env), applyOperandValues); + list applyOperandValues = evalExpr(applyOperand(exp), env, pool); + return applyProcedure(evalExpr(applyOperat(exp), env, pool), applyOperandValues, pool); } if(isApplication(exp)) { - list operandValues = listOfValues(operands(exp), env); - return applyProcedure(evalApply(operat(exp), env), operandValues); + list operandValues = listOfValues(operands(exp), env, pool); + return applyProcedure(evalExpr(operat(exp), env, pool), operandValues, pool); } std::cout << "Unknown expression type " << exp << "\n"; return value(); } +const list quotedParameters(const list& p) { + if (isNil(p)) + return p; + return cons(mklist(quoteSymbol, car(p)), quotedParameters(cdr(p))); +} + } } #endif /* tuscany_eval_eval_hpp */ -- cgit v1.2.3