Bob/source/Environment.cpp
2023-05-31 02:44:39 -04:00

46 lines
988 B
C++

//
// Created by Bobby Lucero on 5/30/23.
//
#include "../headers/Environment.h"
#include "../headers/Lexer.h"
sptr(Object) Environment::get(Token name)
{
if(variables.count(name.lexeme))
{
return variables[name.lexeme];
}
if(enclosing != nullptr)
{
return enclosing->get(name);
}
throw std::runtime_error("Undefined variable '" + name.lexeme + "'.");
}
void Environment::define(std::string name, sptr(Object) value) {
if(variables.count(name) > 0){
throw std::runtime_error("'" + name + "' already defined.");
}
variables.insert(std::make_pair(name, value));
}
void Environment::assign(Token name, std::shared_ptr<Object> value) {
if(variables.count(name.lexeme) > 0)
{
variables[name.lexeme] = value;
return;
}
if(enclosing != nullptr)
{
enclosing->assign(name, value);
return;
}
throw std::runtime_error("Undefined variable '" + name.lexeme + "'.");
}