-
Notifications
You must be signed in to change notification settings - Fork 0
/
CSE.h
217 lines (160 loc) · 5.67 KB
/
CSE.h
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
//
// Created by nisal on 7/19/2023.
//
#ifndef RPAL_FINAL_CSE_H
#define RPAL_FINAL_CSE_H
#include <utility>
#include <vector>
#include <unordered_map>
#include <string>
#include <stdexcept>
#include <iostream>
#include "Tree.h"
// enum of node types for CSE machine
enum class ObjType : int {
LAMBDA,
IDENTIFIER,
INTEGER,
STRING,
GAMMA,
OPERATOR,
BETA,
EETA,
DELTA,
TAU,
ENV,
LIST,
BOOLEAN
};
/**
* Check if the given label is an operator.
*
* @param label The label to check.
* @return True if the label is an operator, false otherwise.
*/
bool isOperator(const std::string &label);
#pragma clang diagnostic push
#pragma ide diagnostic ignored "OCDFAInspection"
// NOLINTNEXTLINE
class CseNode {
private:
// General node properties
ObjType node_type;
std::string node_value;
// CseNode properties for lambda and eeta nodes
int env{};
int cs_index{}; // for delta, tau, eeta, lambda nodes
std::vector<std::string> bound_variables;
std::vector<CseNode> list_elements;
bool is_single_bound_var = true;
public:
CseNode() = default; // NOLINT(cppcoreguidelines-pro-type-member-init)
// Constructor for lambda (in stack) and eeta nodes
CseNode(ObjType node_type, std::string node_value, int cs_index, int env);
// Constructor for lambda (in control structure) nodes
CseNode(ObjType node_type, std::string node_value, int cs_index);
// Constructor for other nodes
CseNode(ObjType node_type, std::string node_value);
// Constructor for lambda (in cs) nodes with bound variables
CseNode(ObjType node_type, int cs_index, std::vector<std::string> bound_variables);
// Constructor for lambda (in stack) nodes with bound variables
CseNode(ObjType node_type, int cs_index, std::vector<std::string> bound_variables, int env);
CseNode(ObjType node_type, std::vector<CseNode> list_elements);
// Getters
[[nodiscard]] ObjType get_node_type() const;
[[nodiscard]] std::string get_node_value() const;
[[nodiscard]] int get_env() const;
[[nodiscard]] int get_cs_index() const;
[[nodiscard]] bool get_is_single_bound_var() const;
[[nodiscard]] std::vector<std::string> get_var_list() const;
std::vector<CseNode> get_list_elements();
CseNode set_env(int env_);
};
#pragma clang diagnostic pop
class ControlStructure {
private:
int cs_index;
std::vector<CseNode> nodes;
public:
// Constructor with empty nodes
explicit ControlStructure(int cs_index);
// add node to control structure
void add_node(const CseNode &node);
// Getters
[[maybe_unused]] [[nodiscard]] int get_cs_index() const;
// return the last node in the control structure
[[maybe_unused]] [[nodiscard]] CseNode get_last_node() const;
// pop the last node in the control structure
void pop_last_node();
// pop and return the last node in the control structure
CseNode pop_and_return_last_node();
// push another control structure to the current control structure
void push_control_structure(const ControlStructure &cs);
};
class Stack {
private:
std::vector<CseNode> nodes;
public:
// constructor with empty nodes
Stack() = default;
// add node to stack
void add_node(const CseNode &node);
// pop the last node in the stack
[[maybe_unused]] void pop_last_node();
// pop and return the last node in the stack
CseNode pop_and_return_last_node();
// length of the stack
[[maybe_unused]] [[nodiscard]] int length() const;
};
class Env {
private:
std::unordered_map<std::string, CseNode> variables;
std::unordered_map<std::string, CseNode> lambdas;
std::unordered_map<std::string, std::vector<CseNode>> lists;
[[maybe_unused]] bool is_lambda = false;
Env *parent_env;
public:
// constructor with empty variables and lambdas
Env();
// constructor with empty variables and lambdas
explicit Env(Env *parent_env);
// add variable to environment
void add_variable(const std::string &identifier, const CseNode &value);
[[maybe_unused]] void add_variables(const std::vector<std::string> &identifiers,
const std::vector<CseNode> &values);
void add_list(const std::string &identifier, const std::vector<CseNode>& list_elements);
// add lambda to environment
void add_lambda(const std::string &identifier, const CseNode &lambda);
// get variable from environment
CseNode get_variable(const std::string &identifier);
// get lambda from environment
CseNode get_lambda(const std::string &identifier);
// get list from environment
std::vector<CseNode> get_list(const std::string &identifier);
};
class CSE {
private:
int next_env = 0;
int next_cs = -1;
std::vector<ControlStructure *> control_structures;
ControlStructure main_control_structure = ControlStructure(-1);
Stack stack = Stack();
std::vector<int> env_stack = std::vector<int>();
std::unordered_map<int, Env *> envs = std::unordered_map<int, Env *>();
public:
CSE() = default;
/**
* Create control structures for the RPAL program represented by the given Abstract Syntax Tree (AST).
*
* @param root: The root node of the AST.
* @param current_cs: The current control structure being constructed.
* @param current_cs_index: The index of the current control structure in the control_structures vector.
*/
void create_cs(TreeNode *root, ControlStructure *current_cs = nullptr, int current_cs_index = -1);
/**
* Evaluate the main control structure.
* This function implements the RPAL evaluation algorithm for the main control structure.
*/
void evaluate();
};
#endif //RPAL_FINAL_CSE_H