let()-expression: disable reassignments

master
Oskar Linde 2014-05-22 22:24:26 +02:00
parent 0a610baf5b
commit dd4e229a46
3 changed files with 18 additions and 2 deletions

View File

@ -136,6 +136,15 @@ Value Context::lookup_variable(const std::string &name, bool silent) const
return Value();
}
bool Context::has_local_variable(const std::string &name) const
{
if (is_config_variable(name))
return config_variables.find(name) != config_variables.end();
if (!parent && constants.find(name) != constants.end())
return true;
return variables.find(name) != variables.end();
}
Value Context::evaluate_function(const std::string &name, const EvalContext *evalctx) const
{
if (this->parent) return this->parent->evaluate_function(name, evalctx);

View File

@ -24,6 +24,7 @@ public:
void set_constant(const std::string &name, const Value &value);
Value lookup_variable(const std::string &name, bool silent = false) const;
bool has_local_variable(const std::string &name) const;
void setDocumentPath(const std::string &path) { this->document_path = path; }
const std::string &documentPath() const { return this->document_path; }

View File

@ -157,9 +157,15 @@ namespace {
void evaluate_sequential_assignment(const AssignmentList & assignment_list, Context *context) {
EvalContext let_context(context, assignment_list);
const bool allow_reassignment = false;
for (int i = 0; i < let_context.numArgs(); i++) {
// NOTE: iteratively evaluated list of arguments
context->set_variable(let_context.getArgName(i), let_context.getArgValue(i, context));
if (!allow_reassignment && context->has_local_variable(let_context.getArgName(i))) {
PRINTB("WARNING: Ignoring duplicate variable assignment %s = %s", let_context.getArgName(i) % let_context.getArgValue(i, context).toString());
} else {
// NOTE: iteratively evaluated list of arguments
context->set_variable(let_context.getArgName(i), let_context.getArgValue(i, context));
}
}
}
}