-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepl.cpp
46 lines (40 loc) · 1.19 KB
/
repl.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
//
// Created by 23766 on 2024/10/6.
//
#include "repl.h"
#include <iostream>
#include <memory>
Repl::Repl() = default;
void Repl::start() {
std::cout << "Welcome to the simple Interpreter in C++. \nEnter expressions or commands.\n";
while (true) {
std::cout << "> ";
std::string input;
std::getline(std::cin, input);
if (input == "exit")
break;
auto lexer = std::make_unique<Lexer>(input);
// Token tok;
Parser parser(std::move(lexer));
auto p = parser.ParseProgram();
// do {
// tok = lexer.NextToken();
//if (tok.type!= TokenType::EOF_) {
// std::cout << "Token: " << tokenTypeToString(tok.type) << " Literal: " << tok.literal << std::endl;
//}
// } while (tok.type != TokenType::EOF_);
if (!parser.Errors().empty()){
std::cout << printParseErrors(parser.Errors()) ;
continue;
}
std::cout << p->String();
std::cout << "\n";
}
}
std::string printParseErrors(std::vector<std::string> errors){
std::ostringstream oss;
for (auto &s:errors){
oss << s << "\n";
}
return oss.str();
}