-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenvironment.h
More file actions
51 lines (43 loc) · 1.38 KB
/
environment.h
File metadata and controls
51 lines (43 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#pragma once
#include "Expression.h"
#include <string>
#include <map>
#include <memory>
#include <iostream>
// Environment to store named expressions
class Environment {
private:
// Map of names to expressions
std::map<std::string, std::shared_ptr<Expression>> definitions;
public:
// Add a definition to the environment
void define(const std::string& name, const std::shared_ptr<Expression>& expr) {
definitions[name] = expr;
}
// Look up a definition
std::shared_ptr<Expression> lookup(const std::string& name) const {
auto it = definitions.find(name);
if (it != definitions.end()) {
return it->second;
}
return nullptr;
}
// Check if a name is defined
bool isDefined(const std::string& name) const {
return definitions.find(name) != definitions.end();
}
// Print all definitions
void printDefinitions() const {
if (definitions.empty()) {
std::cout << "No definitions yet." << std::endl;
return;
}
for (const auto& [name, expr] : definitions) {
std::cout << name << " = " << expr->toString() << std::endl;
}
}
// Access the definitions map directly (for iteration)
const std::map<std::string, std::shared_ptr<Expression>>& getDefinitions() const {
return definitions;
}
};