mirror of https://github.com/vitalif/openscad
let()-expression: disable reassignments
parent
0a610baf5b
commit
dd4e229a46
|
@ -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);
|
||||
|
|
|
@ -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; }
|
||||
|
|
10
src/expr.cc
10
src/expr.cc
|
@ -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));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue