-
Notifications
You must be signed in to change notification settings - Fork 0
/
lox.h
95 lines (80 loc) · 2.08 KB
/
lox.h
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
#include <iostream>
#include <vector>
#include <fstream>
#include <string>
#include <sstream>
#include <stdexcept>
#include <variant>
#include <unordered_map>
#include <Tokentype.h>
#include <Token.h>
class Lox
{
public:
static bool hadError;
static void report(int line, const std::string &where, const std::string &message);
static void error(int line, std::string &message);
static void runFile(const std::string &path);
static void runPrompt();
static void run(const std::string &source);
static void main(const std::vector<std::string> &args);
void Lox::error(int line, std::string &message)
{
report(line, " ", message);
}
void Lox::main(const std::vector<std::string> &args)
{
if (args.size() > 1)
{
std::cout << "Usage: jlox [script]";
exit(64);
}
else if (args.size() == 1)
{
runFile(args[0]);
if (hadError)
exit(65);
}
else
{
runPrompt;
hadError = false;
}
}
private:
static void report(int line, const std::string &where, const std::string &message)
{
std::cerr << "[line " << line << "] Error" << where << ": " << message << std::endl;
hadError = true;
}
void Lox::runFile(const std::string &path)
{
std::ifstream file(path, std::ios::binary);
if (!file)
{
std::cerr << "Unable to open file" << path << std::endl;
exit(1);
}
// std::streambuf* fileBuffer = file.rdbuf();
std::stringstream buffer;
buffer << file.rdbuf();
run(buffer.str());
}
void Lox::runPrompt()
{
std::string line;
while (true)
{
std::cout << "> ";
if (!std::getline(std::cin, line))
break;
run(line);
hadError = false;
}
}
void Lox::run(const std::string &source)
{
std::cout << "Running source: " << source << std::endl;
}
};
bool Lox::hadError = false;