-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.cpp
More file actions
177 lines (141 loc) · 4.46 KB
/
Copy pathparser.cpp
File metadata and controls
177 lines (141 loc) · 4.46 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
#include "brainfxxck/parser.hpp"
#include <tao/pegtl.hpp>
#include <tao/pegtl/contrib/parse_tree.hpp>
#include <fstream>
#include <sstream>
#include <stack>
namespace brainfxxck {
namespace pegtl = tao::pegtl;
namespace grammar {
struct increment : pegtl::one<'+'> {};
struct decrement : pegtl::one<'-'> {};
struct move_right : pegtl::one<'>'> {};
struct move_left : pegtl::one<'<'> {};
struct output : pegtl::one<'.'> {};
struct input : pegtl::one<','> {};
struct loop_start : pegtl::one<'['> {};
struct loop_end : pegtl::one<']'> {};
struct instruction : pegtl::sor<
increment,
decrement,
move_right,
move_left,
output,
input,
loop_start,
loop_end
> {};
struct ignored : pegtl::not_one<'+', '-', '>', '<', '.', ',', '[', ']'> {};
struct grammar : pegtl::star<pegtl::sor<instruction, ignored>> {};
}
struct ParserState {
std::stack<InstructionList> instruction_stack;
ParserState() {
instruction_stack.push(InstructionList{});
}
void addInstruction(InstructionPtr instr) {
instruction_stack.top().push_back(std::move(instr));
}
void beginLoop() {
instruction_stack.push(InstructionList{});
}
void endLoop() {
if (instruction_stack.size() <= 1) {
throw ParseError("Unmatched ']' - loop end without loop start");
}
InstructionList loop_body = std::move(instruction_stack.top());
instruction_stack.pop();
auto loop = makeInstruction<Loop>(std::move(loop_body));
instruction_stack.top().push_back(std::move(loop));
}
InstructionList finish() {
if (instruction_stack.size() != 1) {
throw ParseError("Unmatched '[' - loop start without loop end");
}
return std::move(instruction_stack.top());
}
};
template<typename Rule>
struct action {};
template<>
struct action<grammar::increment> {
template<typename ActionInput>
static void apply(const ActionInput&, ParserState& state) {
state.addInstruction(makeInstruction<Add>(1));
}
};
template<>
struct action<grammar::decrement> {
template<typename ActionInput>
static void apply(const ActionInput&, ParserState& state) {
state.addInstruction(makeInstruction<Add>(-1));
}
};
template<>
struct action<grammar::move_right> {
template<typename ActionInput>
static void apply(const ActionInput&, ParserState& state) {
state.addInstruction(makeInstruction<Move>(1));
}
};
template<>
struct action<grammar::move_left> {
template<typename ActionInput>
static void apply(const ActionInput&, ParserState& state) {
state.addInstruction(makeInstruction<Move>(-1));
}
};
template<>
struct action<grammar::output> {
template<typename ActionInput>
static void apply(const ActionInput&, ParserState& state) {
state.addInstruction(makeInstruction<Output>());
}
};
template<>
struct action<grammar::input> {
template<typename ActionInput>
static void apply(const ActionInput&, ParserState& state) {
state.addInstruction(makeInstruction<Input>());
}
};
template<>
struct action<grammar::loop_start> {
template<typename ActionInput>
static void apply(const ActionInput&, ParserState& state) {
state.beginLoop();
}
};
template<>
struct action<grammar::loop_end> {
template<typename ActionInput>
static void apply(const ActionInput&, ParserState& state) {
state.endLoop();
}
};
Program Parser::parse(const std::string& source, bool optimize) {
ParserState state;
try {
pegtl::memory_input input(source, "brainfuck-source");
if (!pegtl::parse<grammar::grammar, action>(input, state)) {
throw ParseError("Failed to parse brainfuck source");
}
} catch (const pegtl::parse_error& e) {
throw ParseError(std::string("Parse error: ") + e.what());
}
InstructionList instructions = state.finish();
if (optimize) {
instructions = ASTOptimizer::optimize(std::move(instructions));
}
return Program(std::move(instructions));
}
Program Parser::parseFile(const std::string& filename, bool optimize) {
std::ifstream file(filename);
if (!file) {
throw ParseError("Cannot open file: " + filename);
}
std::stringstream buffer;
buffer << file.rdbuf();
return parse(buffer.str(), optimize);
}
}