set! added
This commit is contained in:
parent
c95d7ec886
commit
a50a3bceb3
28
ml.cpp
28
ml.cpp
|
|
@ -652,6 +652,22 @@ void MlEnvironment::set(const std::string &name, MlValue value) {
|
|||
}
|
||||
|
||||
|
||||
void MlEnvironment::setX(const std::string &name, MlValue value) {
|
||||
MlEnvironment* e = this;
|
||||
do {
|
||||
std::map<std::string, MlValue>::const_iterator itr = e->defs.find(name);
|
||||
if (itr != e->defs.end()) {
|
||||
e->set(name, value);
|
||||
return;
|
||||
}
|
||||
e = e->parent_scope;
|
||||
} while (e != nullptr);
|
||||
|
||||
// not found so define
|
||||
this->set(name, value);
|
||||
}
|
||||
|
||||
|
||||
MlValue MlValue::apply(std::vector<MlValue> args, MlEnvironment &env) {
|
||||
MlEnvironment e;
|
||||
std::vector<MlValue> params;
|
||||
|
|
@ -936,6 +952,16 @@ namespace builtin {
|
|||
return f;
|
||||
}
|
||||
|
||||
// Sets (if exists) or define a variable with a value (SPECIAL FORM)
|
||||
MlValue setx(std::vector<MlValue> args, MlEnvironment &env) {
|
||||
if (args.size() != 2)
|
||||
throw MlError(MlValue("set!", define), env, args.size() > 2 ? TOO_MANY_ARGS : TOO_FEW_ARGS);
|
||||
|
||||
MlValue result = args[1].eval(env);
|
||||
env.setX(args[0].display(), result);
|
||||
return result;
|
||||
}
|
||||
|
||||
// Loop over a list of expressions with a condition (SPECIAL FORM)
|
||||
MlValue while_loop(std::vector<MlValue> args, MlEnvironment &env) {
|
||||
MlValue acc;
|
||||
|
|
@ -1796,6 +1822,7 @@ MlValue MlEnvironment::get(const std::string &name) const {
|
|||
if (name == "benchmark") return MlValue("benchmark", builtin::benchmark);
|
||||
if (name == "and") return MlValue("and", builtin::do_and);
|
||||
if (name == "or") return MlValue("or", builtin::do_or);
|
||||
if (name == "set!") return MlValue("set!", builtin::setx);
|
||||
|
||||
// Comparison operations
|
||||
if (name == "=") return MlValue("=", builtin::eq);
|
||||
|
|
@ -1976,3 +2003,4 @@ int main(int argc, char *argv[]) {
|
|||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
|
|
|||
4
ml.h
4
ml.h
|
|
@ -42,6 +42,10 @@ public:
|
|||
// Set the value associated with this name in this scope
|
||||
void set(const std::string &name, MlValue value);
|
||||
|
||||
// Set the value associated with this name in this scope and parent scopes
|
||||
// and if not exists sets in this scope
|
||||
void setX(const std::string &name, MlValue value);
|
||||
|
||||
// Get vector of executables in this scope
|
||||
std::vector<std::string> get_lambdas_list() const;
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue