Repository navigation
Expand file tree
/
Copy pathlox.cpp
More file actions
67 lines (57 loc) · 1.31 KB
/
Copy pathlox.cpp
File metadata and controls
67 lines (57 loc) · 1.31 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
#include <fstream>
#include <iostream>
#include <sstream>
#include "scanner.h"
#include "token.h"
bool g_hadError = false;
void report(int line, const std::string& where, const std::string& msg)
{
std::cerr << "[line " << line << "] Error" << where << ": " << msg << std::endl;
g_hadError = true;
}
void error(int line, const std::string& msg)
{
report(line, "", msg);
}
void error(const token& tok, const std::string& msg)
{
if (tok.type == END_OF_FILE) {
report(tok.line, " at end", msg);
} else {
report(tok.line, " at '" + tok.lexeme + "'", msg);
}
}
void run(const std::string& source)
{
scanner scanner(source);
std::vector<token> tokens = scanner.scan_tokens();
for (const token& token : tokens)
{
std::cout << token << '\n';
}
}
void run_file(const std::string& path)
{
std::ifstream file(path);
if (!file)
{
std::cerr << "Unable to open file: " << path << std::endl;
return;
}
std::ostringstream ss;
ss << file.rdbuf();
std::string source = ss.str();
run(source);
if (g_hadError) std::exit(65);
}
void run_prompt()
{
while (true)
{
std::cout << "> ";
std::string line;
if (!std::getline(std::cin, line)) break;
run(line);
g_hadError = false;
}
}