diff options
author | jsdelfino <jsdelfino@13f79535-47bb-0310-9956-ffa450edef68> | 2009-10-26 05:13:06 +0000 |
---|---|---|
committer | jsdelfino <jsdelfino@13f79535-47bb-0310-9956-ffa450edef68> | 2009-10-26 05:13:06 +0000 |
commit | f61164c77c5c21a32b58ad61c868bd8ff6a4a79e (patch) | |
tree | 951cc581d22bac8ad615697b19e879e22578d10c /cpp/sca/modules/eval | |
parent | 0969cc21d093f9450c36e7f63cddc4f5aa44b238 (diff) |
Minor refactoring, given each module its own namespace. Added utility functions to convert between values elements/attributes and fixed XML, ATOM and JSON serialization of lists.
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@829699 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'cpp/sca/modules/eval')
-rw-r--r-- | cpp/sca/modules/eval/Makefile.am | 3 | ||||
-rw-r--r-- | cpp/sca/modules/eval/driver.hpp | 4 | ||||
-rw-r--r-- | cpp/sca/modules/eval/environment.hpp | 42 | ||||
-rw-r--r-- | cpp/sca/modules/eval/eval-shell.cpp | 2 | ||||
-rw-r--r-- | cpp/sca/modules/eval/eval-test.cpp | 22 | ||||
-rw-r--r-- | cpp/sca/modules/eval/eval.hpp | 31 | ||||
-rw-r--r-- | cpp/sca/modules/eval/primitive.hpp | 5 | ||||
-rw-r--r-- | cpp/sca/modules/eval/read.hpp | 5 |
8 files changed, 70 insertions, 44 deletions
diff --git a/cpp/sca/modules/eval/Makefile.am b/cpp/sca/modules/eval/Makefile.am index 552e0f99c7..b9fa9f8f56 100644 --- a/cpp/sca/modules/eval/Makefile.am +++ b/cpp/sca/modules/eval/Makefile.am @@ -27,3 +27,6 @@ eval_test_LDADD = -lpthread -L${LIBXML2_LIB} -lxml2 eval_shell_SOURCES = eval-shell.cpp eval_shell_LDADD = -lpthread -L${LIBXML2_LIB} -lxml2 + +TESTS = eval-test + diff --git a/cpp/sca/modules/eval/driver.hpp b/cpp/sca/modules/eval/driver.hpp index 7a0645781f..064213706c 100644 --- a/cpp/sca/modules/eval/driver.hpp +++ b/cpp/sca/modules/eval/driver.hpp @@ -31,6 +31,7 @@ #include "eval.hpp" namespace tuscany { +namespace eval { const std::string evalOutputPrompt("; "); const std::string evalInputPrompt("=> "); @@ -57,7 +58,7 @@ const value evalDriverLoop(std::istream& in, std::ostream& out, Env& globalEnv) value input = read(in); if (isNil(input)) return input; - const value output = eval(input, globalEnv); + const value output = evalApply(input, globalEnv); announceOutput(out, evalOutputPrompt); userPrint(out, output); return evalDriverLoop(in, out, globalEnv); @@ -71,4 +72,5 @@ const bool evalDriverRun(std::istream& in, std::ostream& out) { } } +} #endif /* tuscany_eval_driver_hpp */ diff --git a/cpp/sca/modules/eval/environment.hpp b/cpp/sca/modules/eval/environment.hpp index 834563069e..e0da9096fe 100644 --- a/cpp/sca/modules/eval/environment.hpp +++ b/cpp/sca/modules/eval/environment.hpp @@ -31,10 +31,10 @@ #include "value.hpp" #include "primitive.hpp" -namespace tuscany -{ +namespace tuscany { +namespace eval { -typedef value Frame; +typedef list<value> Frame; typedef list<value> Env; const value trueSymbol("true"); @@ -130,28 +130,37 @@ const value assignmentValue(const value& exp) { return car(cdr(cdr((list<value> )exp))); } -const Frame addBindingToFrame(const value& var, const value& val, const Frame& frame) { - return cons(value(cons(var, frameVariables(frame))), cons(val, frameValues(frame))); +const bool addBindingToFrame(const value& var, const value& val, Frame& frame) { + //frame = cons(value(cons(var, frameVariables(frame))), cons(val, frameValues(frame))); + setCar(frame, (value)cons(var, frameVariables(frame))); + setCdr(frame, cons(val, frameValues(frame))); + return true; } -const Env defineVariable(const value& var, const value& val, Env& env) { - return cons(addBindingToFrame(var, val, firstFrame(env)), cdr(env)); +const bool defineVariable(const value& var, const value& val, Env& env) { + Frame frame = firstFrame(env); + addBindingToFrame(var, val, frame); + setCar(env, value(frame)); + return true; } +struct environmentReference { + const Env env; + environmentReference(const Env& env) : env(env) { + } + const Env& operator()() const { + return env; + } +}; + const Env extendEnvironment(const list<value>& vars, const list<value>& vals, const Env& baseEnv) { -// if(length(vars) == length(vals)) -// else if(length(vars) < length(vals)) -// std::cout << "Too many arguments supplied " << vars << " " << vals << "\n"; -// else -// std::cout << "Too few arguments supplied " << vars << " " << vals << "\n"; -// return baseEnv; - return cons(makeFrame(vars, vals), baseEnv); + return cons(value(makeFrame(vars, vals)), lambda<list<value>()>(environmentReference(baseEnv))); } const Env setupEnvironment() { Env env = extendEnvironment(primitiveProcedureNames(), primitiveProcedureObjects(), theEmptyEnvironment()); - env = defineVariable(trueSymbol, true, env); - env = defineVariable(falseSymbol, false, env); + defineVariable(trueSymbol, true, env); + defineVariable(falseSymbol, false, env); return env; } @@ -178,4 +187,5 @@ const value lookupVariableValue(const value& var, const Env& env) { } } +} #endif /* tuscany_eval_environment_hpp */ diff --git a/cpp/sca/modules/eval/eval-shell.cpp b/cpp/sca/modules/eval/eval-shell.cpp index 46710b8354..e1c90101da 100644 --- a/cpp/sca/modules/eval/eval-shell.cpp +++ b/cpp/sca/modules/eval/eval-shell.cpp @@ -30,6 +30,6 @@ #include "driver.hpp" int main() { - tuscany::evalDriverRun(std::cin, std::cout); + tuscany::eval::evalDriverRun(std::cin, std::cout); return 0; } diff --git a/cpp/sca/modules/eval/eval-test.cpp b/cpp/sca/modules/eval/eval-test.cpp index c536ed51a4..95a286ade0 100644 --- a/cpp/sca/modules/eval/eval-test.cpp +++ b/cpp/sca/modules/eval/eval-test.cpp @@ -30,6 +30,7 @@ #include "driver.hpp" namespace tuscany { +namespace eval { bool testEnv() { Env globalEnv = list<value>(); @@ -109,6 +110,11 @@ const std::string testSchemeLambda( "(define (testLambda) (if (= 4 (sqrt 2)) (display \"testLambda ok\") (error \"testLambda\"))) " "(testLambda)"); +const std::string testSchemeForward( + "(define (testLambda) (if (= 4 (sqrt 2)) (display \"testForward ok\") (error \"testForward\"))) " + "(define sqrt (lambda (x) (* x x))) " + "(testLambda)"); + bool contains(const std::string& str, const std::string& pattern) { return str.find(pattern) != str.npos; } @@ -129,13 +135,14 @@ bool testEval() { assert(contains(evalOutput(testSchemeBegin), "testBegin1 ok")); assert(contains(evalOutput(testSchemeBegin), "testBegin2 ok")); assert(contains(evalOutput(testSchemeLambda), "testLambda ok")); + //assert(contains(evalOutput(testSchemeForward), "testForward ok")); return true; } bool testEvalExpr() { const value exp = mklist<value>("+", 2, 3); Env env = setupEnvironment(); - const value r = eval(exp, env); + const value r = evalApply(exp, env); assert(r == value(5)); return true; } @@ -160,16 +167,17 @@ bool testEvalRun() { } } +} int main() { std::cout << "Testing..." << std::endl; - tuscany::testEnv(); - tuscany::testEnvGC(); - tuscany::testRead(); - tuscany::testEval(); - tuscany::testEvalExpr(); - tuscany::testEvalGC(); + tuscany::eval::testEnv(); + tuscany::eval::testEnvGC(); + tuscany::eval::testRead(); + tuscany::eval::testEval(); + tuscany::eval::testEvalExpr(); + tuscany::eval::testEvalGC(); std::cout << "OK" << std::endl; return 0; diff --git a/cpp/sca/modules/eval/eval.hpp b/cpp/sca/modules/eval/eval.hpp index 2be2894bf3..1496c3bd09 100644 --- a/cpp/sca/modules/eval/eval.hpp +++ b/cpp/sca/modules/eval/eval.hpp @@ -33,10 +33,10 @@ #include "read.hpp" #include "environment.hpp" -namespace tuscany -{ +namespace tuscany { +namespace eval { -const value eval(const value& exp, Env& env); +const value evalApply(const value& exp, Env& env); const value compoundProcedureSymbol("compound-procedure"); const value procedureSymbol("procedure"); @@ -89,7 +89,7 @@ const list<value> operands(const value& exp) { const list<value> listOfValues(const list<value> exps, Env& env) { if(isNil(exps)) return list<value> (); - return cons(eval(car(exps), env), listOfValues(cdr(exps), env)); + return cons(evalApply(car(exps), env), listOfValues(cdr(exps), env)); } const value applyOperat(const value& exp) { @@ -134,8 +134,8 @@ const value makeBegin(const list<value> seq) { const value evalSequence(const list<value>& exps, Env& env) { if(isLastExp(exps)) - return eval(firstExp(exps), env); - eval(firstExp(exps), env); + return evalApply(firstExp(exps), env); + evalApply(firstExp(exps), env); return evalSequence(restExp(exps), env); } @@ -219,17 +219,17 @@ value condToIf(const value& exp) { } value evalIf(const value& exp, Env& env) { - if(isTrue(eval(ifPredicate(exp), env))) - return eval(ifConsequent(exp), env); - return eval(ifAlternative(exp), env); + if(isTrue(evalApply(ifPredicate(exp), env))) + return evalApply(ifConsequent(exp), env); + return evalApply(ifAlternative(exp), env); } const value evalDefinition(const value& exp, Env& env) { - env = defineVariable(definitionVariable(exp), eval(definitionValue(exp), env), env); + defineVariable(definitionVariable(exp), evalApply(definitionValue(exp), env), env); return definitionVariable(exp); } -const value eval(const value& exp, Env& env) { +const value evalApply(const value& exp, Env& env) { if(isSelfEvaluating(exp)) return exp; if(isQuoted(exp)) @@ -241,22 +241,23 @@ const value eval(const value& exp, Env& env) { if(isBegin(exp)) return evalSequence(beginActions(exp), env); if(isCond(exp)) - return eval(condToIf(exp), env); + return evalApply(condToIf(exp), env); if(isLambda(exp)) return makeProcedure(lambdaParameters(exp), lambdaBody(exp), env); if(isVariable(exp)) return lookupVariableValue(exp, env); if(isApply(exp)) { - list<value> applyOperandValues = eval(applyOperand(exp), env); - return applyProcedure(eval(applyOperat(exp), env), applyOperandValues); + list<value> applyOperandValues = evalApply(applyOperand(exp), env); + return applyProcedure(evalApply(applyOperat(exp), env), applyOperandValues); } if(isApplication(exp)) { list<value> operandValues = listOfValues(operands(exp), env); - return applyProcedure(eval(operat(exp), env), operandValues); + return applyProcedure(evalApply(operat(exp), env), operandValues); } std::cout << "Unknown expression type " << exp << "\n"; return value(); } } +} #endif /* tuscany_eval_eval_hpp */ diff --git a/cpp/sca/modules/eval/primitive.hpp b/cpp/sca/modules/eval/primitive.hpp index 0d738d1392..4ca6ea900a 100644 --- a/cpp/sca/modules/eval/primitive.hpp +++ b/cpp/sca/modules/eval/primitive.hpp @@ -31,8 +31,8 @@ #include "list.hpp" #include "value.hpp" -namespace tuscany -{ +namespace tuscany { +namespace eval { const value primitiveSymbol("primitive"); const value quoteSymbol("'"); @@ -190,4 +190,5 @@ const value makeLambda(const list<value>& parameters, const list<value>& body) { } } +} #endif /* tuscany_eval_primitive_hpp */ diff --git a/cpp/sca/modules/eval/read.hpp b/cpp/sca/modules/eval/read.hpp index 189075ded9..994462f145 100644 --- a/cpp/sca/modules/eval/read.hpp +++ b/cpp/sca/modules/eval/read.hpp @@ -35,8 +35,8 @@ #include "value.hpp" #include "primitive.hpp" -namespace tuscany -{ +namespace tuscany { +namespace eval { const value rightParenthesis(mklist<value>(")")); const value leftParenthesis(mklist<value>("(")); @@ -180,4 +180,5 @@ const value read(std::istream& in) { } } +} #endif /* tuscany_eval_read_hpp */ |