-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
276 lines (224 loc) · 4.73 KB
/
main.cpp
File metadata and controls
276 lines (224 loc) · 4.73 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
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#include <cassert>
#include <fstream>
#include <iostream>
#include <stdlib.h>
#include <streambuf>
#include <string>
#include <vector>
#include <map>
#include <memory>
#include "token.h"
#include "scanner.h"
class ExprVisitor;
class Expr
{
public:
virtual ~Expr () {};
virtual void accept_visitor (const ExprVisitor & v) = 0;
};
using ExprPtr = std::shared_ptr<Expr>;
class ExprBinary;
class ExprGrouping;
class ExprUnary;
class ExprLiteral;
class ExprVisitor
{
public:
virtual void visitBinary (const ExprBinary & e) const = 0;
virtual void visitGrouping (const ExprGrouping & e) const = 0;
virtual void visitUnary (const ExprUnary & e) const = 0;
virtual void visitLiteral (const ExprLiteral & e) const = 0;
virtual ~ExprVisitor () {};
};
class ExprBinary : public Expr
{
public:
ExprBinary (ExprPtr left, Token token, ExprPtr right)
: m_left (left)
, m_token (token)
, m_right (right)
{
}
void accept_visitor (const ExprVisitor & v) override
{
v.visitBinary (*this);
}
virtual ~ExprBinary ()
{
}
ExprPtr m_left;
Token m_token;
ExprPtr m_right;
};
class ExprGrouping : public Expr
{
public:
ExprGrouping (ExprPtr expression)
: m_expression (expression)
{
}
void accept_visitor (const ExprVisitor & v) override
{
v.visitGrouping (*this);
}
virtual ~ExprGrouping ()
{
}
ExprPtr m_expression;
};
class ExprLiteral : public Expr
{
public:
ExprLiteral (std::any value)
: m_value (value)
{
}
void accept_visitor (const ExprVisitor & v) override
{
v.visitLiteral (*this);
}
virtual ~ExprLiteral ()
{
}
std::any m_value;
};
class ExprUnary : public Expr
{
public:
ExprUnary (Token token, ExprPtr right)
: m_token (token)
, m_right (right)
{
}
void accept_visitor (const ExprVisitor & v) override
{
v.visitUnary (*this);
}
virtual ~ExprUnary ()
{
}
Token m_token;
ExprPtr m_right;
};
void run (const std::string &code)
{
/*
* Create scanner from code
*/
Scanner scanner (code);
/*
* Scan tokens
*/
const auto tokens = scanner.scan_tokens ();
/*
* Dump tokens for debugging
*/
for (auto token : tokens)
{
std::cout << token << std::endl;
}
}
/*
* REPL prompt
*/
int run_prompt () {
for (std::string line; std::getline (std::cin, line);)
{
run (line);
}
return EXIT_SUCCESS;
}
/*
* Process file
*/
int run_file (const std::string &filename)
{
std::ifstream is (filename);
std::string text ((std::istreambuf_iterator<char>(is)),
std::istreambuf_iterator<char>());
run (text);
return EXIT_SUCCESS;
}
/*
* AST Printer
*/
class ASTPrinter : public ExprVisitor
{
public:
void visitBinary (const ExprBinary & e) const override
{
std::cout << "(" << e.m_token.m_lexeme << " ";
e.m_left->accept_visitor (*this);
std::cout << " ";
e.m_right->accept_visitor (*this);
std::cout << ")";
}
void visitGrouping (const ExprGrouping & e) const override
{
std::cout << "(group ";
e.m_expression->accept_visitor (*this);
std::cout << ")";
}
void visitUnary (const ExprUnary & e) const override
{
std::cout << "(" << e.m_token.m_lexeme << " ";
e.m_right->accept_visitor (*this);
std::cout << ")";
}
void visitLiteral (const ExprLiteral & e) const override
{
if (e.m_value.has_value ())
{
if (e.m_value.type () == typeid (double))
{
std::cout << std::any_cast<double> (e.m_value);
}
else if (e.m_value.type () == typeid (std::string))
{
std::cout << std::any_cast<std::string> (e.m_value);
}
else
{
std::cout << "UNKNOWN_LITERAL_TYPE";
}
}
else
{
std::cout << "NULL_LITERAL";
}
}
};
/*
* Main
*/
int main (int argc, char **argv)
{
// Manually make a tree for testing
// auto e = std::make_shared<ExprBinary>
// (std::make_shared <ExprUnary>
auto t1 = Token (TokenType::TOK_MINUS, "-", 0);
auto t2 = Token (TokenType::TOK_STAR, "*", 0);
auto e1 = std::make_shared<ExprLiteral> (45.67);
auto e2 = std::make_shared<ExprGrouping> (e1);
auto e3 = std::make_shared<ExprLiteral> (123.0);
auto e4 = std::make_shared<ExprUnary> (t1, e3);
auto e = std::make_shared<ExprBinary> (e4, t2, e2);
/*
* Now print e
*/
ASTPrinter printer;
e->accept_visitor (printer);
if (argc == 1)
{
return run_prompt ();
}
else if (argc == 2)
{
return run_file (argv[1]);
}
else
{
std::cerr << "Incorrect use" << std::endl;
return EXIT_FAILURE;
}
}