-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
292 lines (239 loc) · 8.37 KB
/
main.cpp
File metadata and controls
292 lines (239 loc) · 8.37 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
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
#include <iostream>
#include <string>
#include <vector>
#include <unordered_set>
#include <sstream>
#include "lexer.h"
#include "parser.h"
#include "interpreter.h"
// ===== ANSI COLORS =====
static const char* C_RESET = "\033[0m";
static const char* C_BOLD = "\033[1m";
static const char* C_GRAY = "\033[90m";
static const char* C_GREEN = "\033[32m";
static const char* C_CYAN = "\033[36m";
static const char* C_RED = "\033[31m";
// highlight colors
static const char* K_KEYWORD = "\033[34m"; // blue
static const char* K_BOOL = "\033[35m"; // purple
// ===== keyword sets (ONLY these get colored) =====
static const std::unordered_set<std::string> KEYWORDS = {
"int","float","bool","string","char",
"fun","done","return",
"while","for",
"if","else","do",
"print","input"
};
static const std::unordered_set<std::string> BOOLS = {"true","false"};
static std::string trim(const std::string& s) {
size_t a = 0;
while (a < s.size() && isspace((unsigned char)s[a])) a++;
size_t b = s.size();
while (b > a && isspace((unsigned char)s[b-1])) b--;
return s.substr(a, b - a);
}
static bool isAllWhitespace(const std::string& s) {
return trim(s).empty();
}
// ===== highlight ONLY keywords/bools, do NOT color strings/chars/numbers =====
static std::string highlightKeywordsOnly(const std::string& line) {
std::ostringstream out;
std::string tok;
bool inString = false;
bool inChar = false;
auto flushTok = [&]() {
if (tok.empty()) return;
if (KEYWORDS.count(tok)) out << K_KEYWORD << tok << C_RESET;
else if (BOOLS.count(tok)) out << K_BOOL << tok << C_RESET;
else out << tok;
tok.clear();
};
for (size_t i = 0; i < line.size(); ++i) {
char c = line[i];
// keep strings/chars untouched
if (c == '"' && !inChar) {
flushTok();
out << c;
inString = !inString;
continue;
}
if (c == '\'' && !inString) {
flushTok();
out << c;
inChar = !inChar;
continue;
}
if (inString || inChar) {
out << c;
continue;
}
if (isalnum((unsigned char)c) || c == '_') {
tok.push_back(c);
} else {
flushTok();
out << c;
}
}
flushTok();
return out.str();
}
// ===== completion detection for multi-line =====
struct CompletionState {
bool ok = true;
std::string errorMsg;
int paren = 0;
int blockDepth = 0; // blocks closed by done;
int funcDepth = 0; // fun blocks closed by done - return ...;
bool pendingBlock = false;
bool pendingFunc = false;
bool complete = false;
};
static CompletionState analyzeCompletion(const std::vector<Token>& tks) {
CompletionState st;
TokenType last = TokenType::INVALID;
for (size_t i = 0; i < tks.size(); ++i) {
const Token& tk = tks[i];
if (tk.type == TokenType::INVALID) {
st.ok = false;
st.errorMsg = tk.lexeme;
return st;
}
if (tk.type != TokenType::END) last = tk.type;
switch (tk.type) {
case TokenType::LPAREN: st.paren++; break;
case TokenType::RPAREN: st.paren--; break;
// start of a block: while, for, if
case TokenType::KW_WHILE:
case TokenType::KW_FOR:
case TokenType::KW_IF:
st.pendingBlock = true;
break;
// else closes the previous then-block and opens a new block
case TokenType::KW_ELSE: {
if (st.blockDepth > 0) st.blockDepth--;
st.pendingBlock = true;
break;
}
// function
case TokenType::KW_FUN:
st.funcDepth++;
st.pendingFunc = true;
break;
case TokenType::COLON:
if (st.pendingBlock) { st.blockDepth++; st.pendingBlock = false; }
if (st.pendingFunc) { st.pendingFunc = false; }
break;
case TokenType::KW_DONE: {
TokenType next = (i + 1 < tks.size()) ? tks[i + 1].type : TokenType::END;
if (next == TokenType::MINUS) {
if (st.funcDepth > 0) st.funcDepth--;
} else {
if (st.blockDepth > 0) st.blockDepth--;
}
break;
}
default:
break;
}
if (st.paren < 0) {
st.ok = false;
st.errorMsg = "Too many ')'";
return st;
}
}
bool endsWithSemi = (last == TokenType::SEMI);
st.complete =
st.ok &&
st.paren == 0 &&
st.blockDepth == 0 &&
st.funcDepth == 0 &&
!st.pendingBlock &&
!st.pendingFunc &&
endsWithSemi;
return st;
}
static void clearScreen() {
std::cout << "\033[2J\033[H" << std::flush;
}
static void printHelp() {
std::cout
<< C_BOLD << "Qarebaq REPL commands" << C_RESET << "\n"
<< " " << C_CYAN << ":help" << C_RESET << " show help\n"
<< " " << C_CYAN << ":reset" << C_RESET << " clear current multi-line buffer\n"
<< " " << C_CYAN << ":clear" << C_RESET << " clear screen\n"
<< " " << C_CYAN << ":exit" << C_RESET << " quit\n\n";
}
// rewrite the last typed line with colors (no duplicate printing)
static void rewriteLastInputLine(const std::string& prompt, const std::string& rawLine, bool enableColor) {
// Move cursor up one line, clear it, and print colored version
std::cout << "\033[A\r\033[2K";
if (!enableColor) {
std::cout << prompt << rawLine << "\n";
return;
}
std::cout << prompt << highlightKeywordsOnly(rawLine) << "\n";
}
int main() {
std::cout << C_BOLD << "Qarebaq REPL" << C_RESET
<< " (" << C_GRAY << "type :help" << C_RESET << ")\n";
Interpreter interp;
// keep programs alive (important for storing function pointers)
std::vector<std::vector<StmtPtr>> allPrograms;
std::string buffer;
// allow turning colors off if needed
bool colorOn = true;
while (true) {
bool multiline = !buffer.empty();
std::string prompt = multiline ? std::string(C_GREEN) +C_BOLD+ "....> " + C_RESET
: std::string(C_RED) +C_BOLD+ ">>> " + C_RESET;
// print prompt
std::cout << prompt << std::flush;
std::string line;
if (!std::getline(std::cin, line)) break;
std::string t = trim(line);
// Commands
if (!multiline) {
if (t == ":exit" || t == "exit") break;
if (t == ":help") { printHelp(); continue; }
if (t == ":clear") { clearScreen(); continue; }
if (t == ":reset") { buffer.clear(); continue; }
if (t == ":color on") { colorOn = true; continue; }
if (t == ":color off") { colorOn = false; continue; }
} else {
if (t == ":reset") { buffer.clear(); continue; }
if (t == ":clear") { clearScreen(); continue; }
if (t == ":color on") { colorOn = true; continue; }
if (t == ":color off") { colorOn = false; continue; }
}
// ignore empty lines only when not in a block
if (!multiline && isAllWhitespace(line)) {
// optionally remove the empty visual line by rewriting it to just prompt
continue;
}
// rewrite the line user just typed (no duplicate echo)
rewriteLastInputLine(prompt, line, colorOn);
buffer += line + "\n";
Lexer lx(buffer);
auto tokens = lx.tokenize();
CompletionState st = analyzeCompletion(tokens);
if (!st.ok) {
std::cout << C_RED << "Lexer error: " << st.errorMsg << C_RESET << "\n";
buffer.clear();
continue;
}
if (!st.complete) {
continue; // keep collecting lines
}
try {
Parser ps(tokens);
auto program = ps.parseProgram();
allPrograms.push_back(std::move(program));
interp.run(allPrograms.back());
} catch (const std::exception& e) {
std::cout << C_RED << "Error: " << e.what() << C_RESET << "\n";
}
buffer.clear();
}
std::cout << C_GRAY << "bye.\n" << C_RESET;
return 0;
}