-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.cpp
294 lines (253 loc) · 6.43 KB
/
util.cpp
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#include "util.h"
#include "lexer.h"
#include <iostream>
#include <optional>
#include "parser.h"
#include "statement.h"
void eat_whitespace(Tokens& tokens) {
Token next_token = tokens.next();
while (next_token.get_type()==WS){
tokens.eat();
next_token = tokens.next();
}
}
void debug(Tokens tokens) {
std::cout << "DEBUG:\n";
tokens.print();
}
std::string enum_to_string(TokenType t) {
switch (t) {
case NUM: return "Number";
case VAR: return "Descriptor";
case PLUS: return "Plus";
case MINUS: return "Minus";
case TIMES: return "Multiplication";
case SLASH: return "Divide";
case LPAREN: return "Left Parenthesis";
case RPAREN: return "Right Parenthesis";
case WS: return "White Space";
case NL: return "New Line";
case EQUAL: return "Equal";
case KEYWORD: return "Keyword";
case LESS: return "Less";
case GREATER: return "Greater";
case COMMA: return "Comma";
case END: return "End of File";
default: lex_error("Token index not found: " + t); exit(1);
}
}
std::string enum_to_string(ReturnType t) {
switch (t) {
case NONE_TYPE: return "none";
case VAR_ASSIGNMENT_TYPE: return "var_assignment";
case FUNC_ASSIGNMENT_TYPE: return "func_assignment";
case BREAK_TYPE: return "break";
case RETURN_TYPE: return "return";
default: eval_error("Type not found: " + t, 2000); exit(1);
}
}
std::string enum_to_string(ValueType t) {
switch (t) {
case INT_TYPE: return "int";
case FLOAT_TYPE: return "float";
case BOOL_TYPE: return "bool";
case STRING_TYPE: return "string";
case OBJECT_VALUE_TYPE: return "object"; // need to be implemented
default: eval_error("Type not found: " + t, 2001); exit(1);
}
}
void lex_error(int pos, std::string lexing_string) {
std::string spaces_hat = "";
std::string spaces_pipe = "";
for (int i = 0; i < pos; i++) {
spaces_hat += " ";
spaces_pipe += " ";
}
spaces_hat += "^\n";
spaces_pipe += "|\n";
if (lexing_string[lexing_string.size()-1]!='\n') {
lexing_string += "\n";
}
std::cout << "Encountered lexing error at position " << pos << "\n" << lexing_string << spaces_hat << spaces_pipe << std::endl;
exit(1);
}
void lex_error(std::string error_message) {
std::cout << error_message << std::endl;
exit(1);
}
std::optional<Token> lex_keyword(std::string lexing_string, int& pos) {
std::vector<std::string> keywords = {
"if",
"for",
"while",
"true",
"false",
"return",
"and",
"or",
"not",
"def",
"return",
"break",
"debug",
"end"
};
int lexing_string_len = lexing_string.size();
std::string current_token_value = "";
int pos_before = pos;
if (isalpha(lexing_string[pos])){
current_token_value += lexing_string[pos];
pos++;
while (pos < lexing_string_len && (isalnum(lexing_string[pos]) || lexing_string[pos]=='_')){
current_token_value += lexing_string[pos];
pos++;
}
for(auto keyword: keywords){
if (current_token_value==keyword){
Token token(KEYWORD, current_token_value);
return token;
}
}
pos = pos_before;
return std::nullopt;
}
else {
pos = pos_before;
return std::nullopt;
}
}
void parse_error(std::string expected, std::string found, Tokens tokens){
std::cout << "Parse error in line " << tokens.getLineNumber() << ":" << std::endl;
std::cout << tokens.getCodeContext() << std::endl;
std::cout << "Expected: '" << expected << "', but found: '" << found << "'"<< std::endl;
exit(1);
}
void parse_error(std::string error_message, Tokens tokens) {
std::cout << "Parse error in line " << tokens.getLineNumber() << ":" << std::endl;
std::cout << tokens.getCodeContext() << std::endl;
std::cout << error_message << std::endl;
exit(1);
}
void eval_error(std::string expected, std::string found, int id) {
std::cout << "Eval error with id " << id << ":" << std::endl;
std::cout << "Expected: '" << expected << "', but found: '" << found << "'" << std::endl;
exit(1);
}
void eval_error(std::string error_msg, int id) {
std::cout << "Eval error with id " << id << ":" << std::endl;
std::cout << error_msg << std::endl;
exit(1);
}
std::string boolToString(bool b) {
return ( b ? "true" : "false");
}
bool stringToBool(std::string s) {
if (s=="true") {
return true;
}
return false;
}
int ipow(int base, int exp)
{
if (exp < 0 ){
eval_error("Can not use negative exponent in ipow", 1337);
}
int result = 1;
for (;;)
{
if (exp & 1)
result *= base;
exp >>= 1;
if (!exp)
break;
base *= base;
}
return result;
}
void pushOperatorToStack(std::stack<StatementHelper>& operatorStack, StatementHelper& op, std::vector<StatementHelper>& queue){
int operator_prec = getOperatorPrecedence(op);
if (op.type == "**") {
while (operatorStack.size()>0 && getOperatorPrecedence(operatorStack.top())>operator_prec){
StatementHelper operator_top = operatorStack.top();
operatorStack.pop();
queue.push_back(operator_top);
}
}
else {
while (operatorStack.size()>0 && getOperatorPrecedence(operatorStack.top())>=operator_prec){
StatementHelper operator_top = operatorStack.top();
operatorStack.pop();
queue.push_back(operator_top);
}
}
operatorStack.push(op);
}
Statement* convertReturnToExpression(ReturnValue ret) {
if (ret.getType() == PRIMITIVE_TYPE) {
PrimitiveValue* pval = (PrimitiveValue*) ret.getValueObj();
std::string value = pval->getValue();
switch (pval->getType())
{
case INT_TYPE:
return new Number(value);
case STRING_TYPE:
return new String(value);
case BOOL_TYPE:
return new Bool(value);
default:
eval_error("Can not convert type to object", 9876);
exit(1);
}
}
else {
eval_error("THIS IS A TEMPORARY ERROR FOR USE WITH OBJECT LATER THX GG", 381487);
exit(1);
}
}
int getOperatorPrecedence(StatementHelper& op){
// Operator Precedence
/*
Highest
7 .
6 not
5 **
4 /, *
3 + -
2 and
1 or
0 ==, >, <, >=, <=
Lowest
*/
std::string op_value = op.type;
if (op_value == ".") {
return 7;
}
else if (op_value == "not"){
return 6;
}
else if (op_value == "**"){
return 5;
}
else if (op_value == "/" || op_value == "*"){
return 4;
}
else if (op_value == "+" || op_value == "-"){
return 3;
}
else if (op_value == "and"){
return 1;
}
else if (op_value == "or"){
return 0;
}
else if (op_value == "==" || op_value == ">" || op_value == "<" || op_value == ">=" || op_value == "<="){
return 2;
}
else if (op_value == "(") {
return -1;
}
else {
std::cout << "Operator not found: " + op_value << std::endl;
exit(1);
}
}