-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLox.cpp
More file actions
82 lines (72 loc) · 1.8 KB
/
Lox.cpp
File metadata and controls
82 lines (72 loc) · 1.8 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
#include "Lox.h"
#include "iostream"
#include "fstream"
#include "sstream"
#include "vector"
#include "string"
#include "filesystem"
#include "Token/Token.h"
#include "Scanner/Scanner.h"
#include "Parser/Parser.h"
#include <gtest/gtest.h>
#include <any>
#include <utility>
#include "Interpreter/Interpreter.h"
int main(int argc, char* argv[])
{
testing::InitGoogleTest(&argc,argv);
std::cout << "Current working directory" << std::filesystem::current_path() << std::endl;
std::string curdir(std::filesystem::current_path().string());
//LoxCpp::RunFile(curdir + "\\test.txt");
LoxCpp::RunPrompt();
return RUN_ALL_TESTS();
}
namespace LoxCpp {
bool hadError = false;
Interpreter interpreter;
void RunFile(const std::string& path) {
std::ifstream stream(path);
std::ostringstream stringStream;
stringStream << stream.rdbuf();
stream.close();
Run(stringStream.str());
}
void RunPrompt() {
for (;;) {
std::cout << std::endl;
std::cout << "-> ";
std::string line;
std::getline(std::cin, line);
if (line.empty())
break;
Run(line);
}
}
bool Run(std::string source) {
LoxException loxException{};
try {
Scanner scanner(std::move(source), loxException);
const std::vector<Token> tokens = scanner.ScanTokens();
Parser parser = Parser(tokens, loxException);
auto statements = parser.Parse();
interpreter.Interpret(std::move(statements));
}
catch(LoxException& err)
{
for(auto error : err.Errors)
{
Error(error.Line, error.ErrorMessage);
}
}
if (hadError)
return hadError;
return true;
}
void Error(int line, std::string message) {
Report(line, "", message);
}
void Report(const int line, const std::string whereAt, const std::string message) {
std::cout << "[line " << line << "] Error " << whereAt << ": " << message;
hadError = true;
}
}