diff options
author | jsdelfino <jsdelfino@13f79535-47bb-0310-9956-ffa450edef68> | 2009-10-03 21:50:33 +0000 |
---|---|---|
committer | jsdelfino <jsdelfino@13f79535-47bb-0310-9956-ffa450edef68> | 2009-10-03 21:50:33 +0000 |
commit | b2b06bd2780dc40f666201d095c388313364b37a (patch) | |
tree | 1186e75dd637a02e567f91a1c5d7014a2bca7a95 /cpp/sca/modules/eval/environment.hpp | |
parent | 95cb8fb517576b3357ecdf27a30c29b1365c5cf5 (diff) |
Refactored store tests and moved them under test/. Simplified the script evaluation logic a little, added an apply procedure and support for variable lists of args. Added a test case that assembles the store app using a script.
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@821425 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'cpp/sca/modules/eval/environment.hpp')
-rw-r--r-- | cpp/sca/modules/eval/environment.hpp | 45 |
1 files changed, 37 insertions, 8 deletions
diff --git a/cpp/sca/modules/eval/environment.hpp b/cpp/sca/modules/eval/environment.hpp index 4ee27df552..46c1716cd8 100644 --- a/cpp/sca/modules/eval/environment.hpp +++ b/cpp/sca/modules/eval/environment.hpp @@ -29,6 +29,7 @@ #include <string> #include "list.hpp" #include "value.hpp" +#include "primitive.hpp" namespace tuscany { @@ -40,6 +41,7 @@ const value trueSymbol = value("true"); const value falseSymbol = value("false"); const value defineSymbol = value("define"); const value setSymbol = value("set!"); +const value dotSymbol = value("."); const Env theEmptyEnvironment() { return list<value>(); @@ -73,8 +75,35 @@ list<value> frameValues(const Frame& frame) { return cdr((list<value> )frame); } +const bool isDotVariable(const value& var) { + return var == dotSymbol; +} + +const Frame makeBinding(const Frame& frameSoFar, const list<value>& variables, const list<value> values) { + if (variables == list<value>()) { + if (values != list<value>()) + std::cout << "Too many arguments supplied " << values << "\n"; + return frameSoFar; + } + if (isDotVariable(car(variables))) + return makeBinding(frameSoFar, cdr(variables), makeList(value(values))); + + if (values == list<value>()) { + if (variables != list<value>()) + std::cout << "Too few arguments supplied " << variables << "\n"; + return frameSoFar; + } + + const list<value> vars = cons(car(variables), frameVariables(frameSoFar)); + const list<value> vals = cons(car(values), frameValues(frameSoFar)); + const Frame newFrame = value(cons(value(vars), vals)); + + return makeBinding(newFrame, cdr(variables), cdr(values)); +} + const Frame makeFrame(const list<value>& variables, const list<value> values) { - return value(cons((value)variables, values)); + const Frame emptyFrame = value(cons((value)list<value>(), list<value>())); + return makeBinding(emptyFrame, variables, values); } const value definitionVariable(const value& exp) { @@ -110,13 +139,13 @@ const Env defineVariable(const value& var, const value& val, Env& env) { } const Env extendEnvironment(const list<value>& vars, const list<value>& vals, const Env& baseEnv) { - if(length(vars) == length(vals)) - return cons(makeFrame(vars, vals), baseEnv); - 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; +// 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); } const Env setupEnvironment() { |