musique/src/environment.cc
Robert Bendun b9d87e1456 New environment implementation
It's a failure of locating precise source of the bug that would cause `var x =
[i|i] 0` to segfault. New implementation DOES NOT have this bug.
2022-05-22 03:44:10 +02:00

41 lines
771 B
C++

#include <musique.hh>
#include <iostream>
std::shared_ptr<Env> Env::global = nullptr;
std::shared_ptr<Env> Env::make()
{
auto new_env = new Env();
assert(new_env, "Cannot construct new env");
return std::shared_ptr<Env>(new_env);
}
Env& Env::force_define(std::string name, Value new_value)
{
variables.insert_or_assign(std::move(name), std::move(new_value));
return *this;
}
Value* Env::find(std::string const& name)
{
for (Env *env = this; env; env = env->parent.get()) {
if (auto it = env->variables.find(name); it != env->variables.end()) {
return &it->second;
}
}
return nullptr;
}
std::shared_ptr<Env> Env::enter()
{
auto next = make();
next->parent = shared_from_this();
return next;
}
std::shared_ptr<Env> Env::leave()
{
return parent;
}