2022-05-17 02:35:51 +02:00
|
|
|
#include <musique.hh>
|
|
|
|
|
2022-05-17 16:10:56 +02:00
|
|
|
#include <iostream>
|
|
|
|
|
2022-05-21 23:54:21 +02:00
|
|
|
std::shared_ptr<Env> Env::global = nullptr;
|
2022-05-17 02:35:51 +02:00
|
|
|
|
2022-05-21 23:54:21 +02:00
|
|
|
std::shared_ptr<Env> Env::make()
|
2022-05-17 02:35:51 +02:00
|
|
|
{
|
2022-05-21 23:54:21 +02:00
|
|
|
auto new_env = new Env();
|
|
|
|
assert(new_env, "Cannot construct new env");
|
|
|
|
return std::shared_ptr<Env>(new_env);
|
2022-05-17 02:35:51 +02:00
|
|
|
}
|
|
|
|
|
2022-05-21 23:54:21 +02:00
|
|
|
Env& Env::force_define(std::string name, Value new_value)
|
2022-05-17 02:35:51 +02:00
|
|
|
{
|
2022-05-21 23:54:21 +02:00
|
|
|
variables.insert_or_assign(std::move(name), std::move(new_value));
|
|
|
|
return *this;
|
2022-05-17 02:35:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Value* Env::find(std::string const& name)
|
|
|
|
{
|
2022-05-21 23:54:21 +02:00
|
|
|
for (Env *env = this; env; env = env->parent.get()) {
|
2022-05-17 02:35:51 +02:00
|
|
|
if (auto it = env->variables.find(name); it != env->variables.end()) {
|
|
|
|
return &it->second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2022-05-21 23:54:21 +02:00
|
|
|
std::shared_ptr<Env> Env::enter()
|
2022-05-17 02:35:51 +02:00
|
|
|
{
|
2022-05-21 23:54:21 +02:00
|
|
|
auto next = make();
|
|
|
|
next->parent = shared_from_this();
|
|
|
|
return next;
|
2022-05-17 02:35:51 +02:00
|
|
|
}
|
|
|
|
|
2022-05-21 23:54:21 +02:00
|
|
|
std::shared_ptr<Env> Env::leave()
|
2022-05-17 02:35:51 +02:00
|
|
|
{
|
2022-05-21 23:54:21 +02:00
|
|
|
return parent;
|
2022-05-17 02:35:51 +02:00
|
|
|
}
|