1#include "gsc/environment.hpp"
2#include "gsc/runtimeError.hpp"
4Environment::Environment() : enclosing(
nullptr) {}
6Environment::Environment(std::shared_ptr<Environment> enclosing)
7 : enclosing(std::move(enclosing)) {}
9std::any Environment::get(
const Token &name)
const {
10 auto it = values.find(name.getLexeme());
11 if (it != values.end()) {
15 return enclosing->get(name);
17 throw RuntimeError(std::make_shared<Token>(name),
18 "Undefined variable '" + name.getLexeme() +
"'.");
21void Environment::assign(
const Token &name, std::any value) {
22 auto it = values.find(name.getLexeme());
23 if (it != values.end()) {
24 it->second = std::move(value);
28 enclosing->assign(name, std::move(value));
31 throw RuntimeError(std::make_shared<Token>(name),
32 "Undefined variable '" + name.getLexeme() +
"'.");
35void Environment::define(
const std::string &name, std::any value) {
36 values[name] = std::move(value);