set! functionality fix

this will be probably changed in future
This commit is contained in:
VaclavT 2021-04-19 21:17:10 +02:00
parent a27bcda59b
commit 6638cdff55
1 changed files with 10 additions and 11 deletions

21
ml.cpp
View File

@ -110,13 +110,13 @@ MlValue::MlValue(const std::vector<MlValue> &params, MlValue ret, MlEnvironment
list.push_back(MlValue(params)); list.push_back(MlValue(params));
list.push_back(ret); list.push_back(ret);
// Lambdas capture only variables that they know they will use. // // Lambdas capture only variables that they know they will use.
std::vector<std::string> used_atoms = ret.get_used_atoms(); // std::vector<std::string> used_atoms = ret.get_used_atoms();
for (size_t i = 0; i < used_atoms.size(); i++) { // for (size_t i = 0; i < used_atoms.size(); i++) {
// If the environment has a symbol that this lambda uses, capture it. // // If the environment has a symbol that this lambda uses, capture it.
if (env.has(used_atoms[i])) // if (env.has(used_atoms[i]))
lambda_scope.set(used_atoms[i], env.get(used_atoms[i])); // lambda_scope.set(used_atoms[i], env.get(used_atoms[i]));
} // }
} }
// Construct a builtin function // Construct a builtin function
@ -653,16 +653,15 @@ void MlEnvironment::set(const std::string &name, MlValue value) {
void MlEnvironment::setX(const std::string &name, MlValue value) { void MlEnvironment::setX(const std::string &name, MlValue value) {
MlEnvironment* e = this; MlEnvironment* e = this->parent_scope; // we start above this scope for set!
do { while (e != nullptr) {
std::map<std::string, MlValue>::const_iterator itr = e->defs.find(name); std::map<std::string, MlValue>::const_iterator itr = e->defs.find(name);
if (itr != e->defs.end()) { if (itr != e->defs.end()) {
e->set(name, value); e->set(name, value);
return; return;
} }
e = e->parent_scope; e = e->parent_scope;
} while (e != nullptr); }
// not found so define // not found so define
this->set(name, value); this->set(name, value);
} }