-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
225 lines (188 loc) · 4.19 KB
/
main.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
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#include <cstdlib>
#include <fstream>
#include <ios>
#include <iostream>
#include <istream>
#include <vector>
enum TokenType {
LEFT_PAREN,
RIGHT_PAREN,
LEFT_BRACE,
RIGHT_BRACE,
COMMA,
DOUBLE_QUOTE,
COLON,
STRING,
NUMBER,
BOOL,
NIL,
END_OF_FILE,
};
class Token {
TokenType type;
int line;
std::string lexeme;
std::string literal;
public:
Token(TokenType type, std::string lexeme, std::string literal, int line) {
this->type = type;
this->line = line;
this->lexeme = lexeme;
this->literal = literal;
};
std::string to_string() {
return "Token<" + std::to_string(this->type) + ", " + this->lexeme + ", " +
this->literal + ", " + std::to_string(this->line) + ">";
}
};
void report(int line, std::string where, std::string message) {
std::cerr << "[line " << line << "] Error" << where << ": " << message
<< std::endl;
}
void error(int line, std::string message) { report(line, "", message); }
class Scanner {
private:
std::vector<Token> tokens;
std::istream &source;
int start = 0;
int current = 0;
int line = 1;
void add_token(TokenType type) { add_token(type, ""); }
void add_token(TokenType type, std::string literal) {
std::string lexeme = "";
Token token(type, lexeme, literal, line);
tokens.push_back(token);
}
char advance() {
char c = peek();
this->current++;
return c;
}
char peek() {
if (is_at_end()) {
return '\0';
}
char c;
this->source.seekg(current);
this->source.get(c);
return c;
}
bool is_at_end() {
this->source.seekg(current);
// char c;
// this->source.get(c);
return source.peek() == EOF;
// return c == EOF;
// bool is_eof = this->source.eof();
//
// return is_eof;
}
void string() {
while (peek() != '"' && !is_at_end()) {
if (peek() == '\n') {
line++;
}
advance();
}
if (is_at_end()) {
error(line, "Unterminated string.");
return;
}
advance();
std::string str_content;
source.seekg(start + 1);
char crr_char;
while (source.get(crr_char) && source.tellg() < current) {
str_content.push_back(crr_char);
}
add_token(STRING, str_content);
}
public:
explicit Scanner(std::istream &source) : source(source) {}
std::vector<Token> scan_tokens() {
while (!is_at_end()) {
start = current;
scan_token();
}
add_token(END_OF_FILE);
return this->tokens;
}
void scan_token() {
// while we have things coming in keep looping
char c = advance();
switch (c) {
case '{':
add_token(LEFT_BRACE);
break;
case '}':
add_token(RIGHT_BRACE);
break;
case '[':
add_token(LEFT_PAREN);
break;
case ']':
add_token(RIGHT_PAREN);
break;
case ',':
add_token(COMMA);
break;
case '"':
string();
break;
case ':':
add_token(COLON);
break;
case ' ':
case '\r':
case '\t':
break;
case '\n':
line++;
break;
default:
error(this->line, "Unexpected character.");
break;
}
}
};
void run(std::istream &stream) {
// stream.seekg(18);
//
// char c;
// stream.get(c);
//
// bool is_eof = c == std::istream::eofbit;
// bool is_eof_2 = stream.eof();
//
// std::cout << "Read: " << c << "\t" << is_eof << "\t" << is_eof_2 <<
// std::endl;
Scanner s(stream);
std::vector<Token> tokens = s.scan_tokens();
for (Token token : tokens) {
std::cout << token.to_string() << std::endl;
}
}
int main(int argc, char **argv) {
if (argc != 2) {
std::cerr << "Usage: " << argv[0] << " <file_path>" << std::endl;
exit(64);
}
std::string file_path = argv[1];
std::ifstream input_stream(file_path);
if (!input_stream || !input_stream.is_open()) {
std::cerr << "Could not open " << file_path << " for reading!" << std::endl;
exit(65);
}
// char c;
// input_stream.seekg(19);
// input_stream.get(c);
//
// bool is_eof = input_stream.eof();
//
// if (is_eof) {
// std::cout << "IM EOF" << std::endl;
// }
//
// std::cout << "INPUT STREAM EOF: " << c << "\t is it eof?: " << is_eof << std::endl;
run(input_stream);
}