From bd0fdbf902f8ca8e7e352582efe938e1d6743dd1 Mon Sep 17 00:00:00 2001 From: jsdelfino Date: Mon, 16 Nov 2009 06:57:41 +0000 Subject: Cleaning up SVN structure, moving sca trunk to sca-cpp/trunk. git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@880633 13f79535-47bb-0310-9956-ffa450edef68 --- cpp/sca/modules/eval/eval.hpp | 290 ------------------------------------------ 1 file changed, 290 deletions(-) delete mode 100644 cpp/sca/modules/eval/eval.hpp (limited to 'cpp/sca/modules/eval/eval.hpp') diff --git a/cpp/sca/modules/eval/eval.hpp b/cpp/sca/modules/eval/eval.hpp deleted file mode 100644 index 8c9ecfdecc..0000000000 --- a/cpp/sca/modules/eval/eval.hpp +++ /dev/null @@ -1,290 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -/* $Rev$ $Date$ */ - -#ifndef tuscany_eval_eval_hpp -#define tuscany_eval_eval_hpp - -/** - * Core script evaluation logic. - */ - -#include -#include "list.hpp" -#include "value.hpp" -#include "primitive.hpp" -#include "io.hpp" -#include "environment.hpp" - -namespace tuscany { -namespace eval { - -const value evalExpr(const value& exp, Env& env, const gc_pool& pool); - -const value compoundProcedureSymbol("compound-procedure"); -const value procedureSymbol("procedure"); -const value applySymbol("apply"); -const value beginSymbol("begin"); -const value condSymbol("cond"); -const value elseSymbol("else"); -const value ifSymbol("if"); - -const bool isBegin(const value& exp) { - return isTaggedList(exp, beginSymbol); -} - -const list beginActions(const value& exp) { - return cdr((list )exp); -} - -const bool isLambdaExpr(const value& exp) { - return isTaggedList(exp, lambdaSymbol); -} - -const list lambdaParameters(const value& exp) { - return car(cdr((list )exp)); -} - -static list lambdaBody(const value& exp) { - return cdr(cdr((list )exp)); -} - -const value makeProcedure(const list& parameters, const value& body, const Env& env) { - return mklist(procedureSymbol, parameters, body, env); -} - -const bool isApply(const value& exp) { - return isTaggedList(exp, applySymbol); -} - -const bool isApplication(const value& exp) { - return isList(exp); -} - -const value operat(const value& exp) { - return car((list )exp); -} - -const list operands(const value& exp) { - return cdr((list )exp); -} - -const list listOfValues(const list exps, Env& env, const gc_pool& pool) { - if(isNil(exps)) - return list (); - return cons(evalExpr(car(exps), env, pool), listOfValues(cdr(exps), env, pool)); -} - -const value applyOperat(const value& exp) { - return cadr((list )exp); -} - -const value applyOperand(const value& exp) { - return caddr((list )exp); -} - -const bool isCompoundProcedure(const value& procedure) { - return isTaggedList(procedure, procedureSymbol); -} - -const list procedureParameters(const value& exp) { - return car(cdr((list )exp)); -} - -const value procedureBody(const value& exp) { - return car(cdr(cdr((list )exp))); -} - -const Env procedureEnvironment(const value& exp) { - return (Env)car(cdr(cdr(cdr((list )exp)))); -} - -const bool isLastExp(const list& seq) { - return isNil(cdr(seq)); -} - -const value firstExp(const list& seq) { - return car(seq); -} - -const list restExp(const list& seq) { - return cdr(seq); -} - -const value makeBegin(const list seq) { - return cons(beginSymbol, seq); -} - -const value evalSequence(const list& exps, Env& env, const gc_pool& pool) { - if(isLastExp(exps)) - 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 gc_pool& pool) { - if(isPrimitiveProcedure(procedure)) - return applyPrimitiveProcedure(procedure, arguments, pool); - if(isCompoundProcedure(procedure)) { - Env env = extendEnvironment(procedureParameters(procedure), arguments, procedureEnvironment(procedure), pool); - return evalSequence(procedureBody(procedure), env, pool); - } - std::cout << "Unknown procedure type " << procedure << "\n"; - return value(); -} - -const value sequenceToExp(const list exps) { - if(isNil(exps)) - return exps; - if(isLastExp(exps)) - return firstExp(exps); - return makeBegin(exps); -} - -const list condClauses(const value& exp) { - return cdr((list )exp); -} - -const value condPredicate(const value& clause) { - return car((list )clause); -} - -const list condActions(const value& clause) { - return cdr((list )clause); -} - -const value ifPredicate(const value& exp) { - return car(cdr((list )exp)); -} - -const value ifConsequent(const value& exp) { - return car(cdr(cdr((list )exp))); -} - -const value ifAlternative(const value& exp) { - if(!isNil(cdr(cdr(cdr((list )exp))))) - return car(cdr(cdr(cdr((list )exp)))); - return false; -} - -const bool isCond(const value& exp) { - return isTaggedList(exp, condSymbol); -} - -const bool isCondElseClause(const value& clause) { - return condPredicate(clause) == elseSymbol; -} - -const bool isIf(const value& exp) { - return isTaggedList(exp, ifSymbol); -} - -const value makeIf(value predicate, value consequent, value alternative) { - return mklist(ifSymbol, predicate, consequent, alternative); -} - -const value expandClauses(const list& clauses) { - if(isNil(clauses)) - return false; - const value first = car(clauses); - const list rest = cdr(clauses); - if(isCondElseClause(first)) { - if(isNil(rest)) - return sequenceToExp(condActions(first)); - std::cout << "else clause isn't last " << clauses << "\n"; - return value(); - } - return makeIf(condPredicate(first), sequenceToExp(condActions(first)), expandClauses(rest)); -} - -value condToIf(const value& exp) { - return expandClauses(condClauses(exp)); -} - -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, const gc_pool& pool) { - defineVariable(definitionVariable(exp), evalExpr(definitionValue(exp), env, pool), env); - return definitionVariable(exp); -} - -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, pool); - if(isIf(exp)) - return evalIf(exp, env, pool); - if(isBegin(exp)) - return evalSequence(beginActions(exp), env, pool); - if(isCond(exp)) - return evalExpr(condToIf(exp), env, pool); - if(isLambdaExpr(exp)) - return makeProcedure(lambdaParameters(exp), lambdaBody(exp), env); - if(isVariable(exp)) - return lookupVariableValue(exp, env); - if(isApply(exp)) { - 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, 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))); -} - -/** - * Evaluate an expression against a script provided as a list of values. - */ -const value evalScriptLoop(const value& expr, const list& script, eval::Env& globalEnv, const gc_pool& pool) { - if (isNil(script)) - return eval::evalExpr(expr, globalEnv, pool); - eval::evalExpr(car(script), globalEnv, pool); - return evalScriptLoop(expr, cdr(script), globalEnv, pool); -} - -const value evalScript(const value& expr, const value& script, Env& env, const gc_pool& pool) { - return evalScriptLoop(expr, script, env, pool); -} - -/** - * Evaluate an expression against a script provided as an input stream. - */ -const value evalScript(const value& expr, std::istream& is, Env& env, const gc_pool& pool) { - return evalScript(expr, readScript(is), env, pool); -} - -} -} -#endif /* tuscany_eval_eval_hpp */ -- cgit v1.2.3