GSC Interpreter
A Turing-complete interpreter developed for a compiler course
Loading...
Searching...
No Matches
Environment Class Reference

Represents an environment for variable storage in the GSC interpreter. More...

#include <environment.hpp>

Inheritance diagram for Environment:
Collaboration diagram for Environment:

Public Member Functions

 Environment ()
 Constructs a new Environment.
 
 Environment (std::shared_ptr< Environment > enclosing)
 Constructs a new Environment with an enclosing environment.
 
std::any get (const Token &name) const
 Retrieves the value of a variable by its name.
 
void assign (const Token &name, std::any value)
 Assigns a value to a variable by its name.
 
void define (const std::string &name, std::any value)
 Defines a new variable in the current environment.
 

Private Attributes

std::shared_ptr< Environmentenclosing
 
std::map< std::string, std::any > values
 

Detailed Description

Represents an environment for variable storage in the GSC interpreter.

Note
This class manages variable scopes, allowing for nested environments to handle variable declarations and lookups.

Definition at line 15 of file environment.hpp.

Constructor & Destructor Documentation

◆ Environment() [1/2]

Environment::Environment ( )

Constructs a new Environment.

Note
This constructor creates a new environment without an enclosing environment.

Definition at line 4 of file environment.cpp.

4: enclosing(nullptr) {}
std::shared_ptr< Environment > enclosing

◆ Environment() [2/2]

Environment::Environment ( std::shared_ptr< Environment enclosing)

Constructs a new Environment with an enclosing environment.

Parameters
enclosingA shared pointer to the enclosing environment.
Note
This constructor allows for nested environments, enabling variable shadowing and scope management.

Definition at line 6 of file environment.cpp.

7 : enclosing(std::move(enclosing)) {}

Member Function Documentation

◆ assign()

void Environment::assign ( const Token name,
std::any  value 
)

Assigns a value to a variable by its name.

Parameters
nameThe Token representing the variable name.
valueThe value to assign to the variable.
Exceptions
RuntimeErrorif the variable is not defined in this or any enclosing environment.

Definition at line 21 of file environment.cpp.

21 {
22 auto it = values.find(name.getLexeme());
23 if (it != values.end()) {
24 it->second = std::move(value);
25 return;
26 }
27 if (enclosing) {
28 enclosing->assign(name, std::move(value));
29 return;
30 }
31 throw RuntimeError(std::make_shared<Token>(name),
32 "Undefined variable '" + name.getLexeme() + "'.");
33}
std::map< std::string, std::any > values
Represents a runtime error in the GSC interpreter.
std::string getLexeme() const
Definition token.cpp:10

◆ define()

void Environment::define ( const std::string &  name,
std::any  value 
)

Defines a new variable in the current environment.

Parameters
nameThe name of the variable to define.
valueThe value to assign to the variable.
Note
This method does not check for existing variables with the same name, allowing for redefinition within the same environment.

Definition at line 35 of file environment.cpp.

35 {
36 values[name] = std::move(value);
37}

◆ get()

std::any Environment::get ( const Token name) const

Retrieves the value of a variable by its name.

Parameters
nameThe Token representing the variable name.
Returns
The value associated with the variable.
Exceptions
RuntimeErrorif the variable is not defined in this or any enclosing environment.

Definition at line 9 of file environment.cpp.

9 {
10 auto it = values.find(name.getLexeme());
11 if (it != values.end()) {
12 return it->second;
13 }
14 if (enclosing) {
15 return enclosing->get(name);
16 }
17 throw RuntimeError(std::make_shared<Token>(name),
18 "Undefined variable '" + name.getLexeme() + "'.");
19}

Member Data Documentation

◆ enclosing

std::shared_ptr<Environment> Environment::enclosing
private

Definition at line 17 of file environment.hpp.

◆ values

std::map<std::string, std::any> Environment::values
private

Definition at line 18 of file environment.hpp.


The documentation for this class was generated from the following files: