-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLox.cpp
64 lines (49 loc) · 1.03 KB
/
Lox.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
#include "Lox.hpp"
#include "Scanner.hpp"
#include "Token.hpp"
#include <iostream>
#include <fstream>
Lox::Lox() : m_hadError(false)
{
}
void Lox::error(int line, std::string message)
{
report(line, "", message);
}
void Lox::report(int line, std::string where, std::string message)
{
std::cout << "[line " << line << "] Error" \
<< where << ": " << message << std::endl;
m_hadError = true;
}
void Lox::runFile(std::string path)
{
std::ifstream file(path);
std::string input;
std::string tmp;
while (getline(file, tmp)) {
if (!input.empty()) input += '\n';
input += tmp;
}
run(input);
if (m_hadError)
exit(65);
}
void Lox::run(std::string input)
{
Scanner scanner(*this, input);
std::vector<Token> tokens = scanner.scanTokens();
for (auto& token : tokens)
std::cout << token.toString() << std::endl;
}
void Lox::runPrompt()
{
std::cout << "Running prompt..." << std::endl;
std::string input;
std::cout << "> ";
while (std::getline(std::cin, input)) {
run(input);
m_hadError = false;
std::cout << "> ";
}
}