-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfzbz.cc
246 lines (204 loc) · 6.61 KB
/
fzbz.cc
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
//
// FizzBuzzLang
// A Programming Language just for writing Fizz Buzz program. :)
//
// Copyright (c) 2021 Yuji Hirose. All rights reserved.
// MIT License
//
#include <fstream>
#include <variant>
#include "peglib.h"
using namespace std;
using namespace peg;
using namespace peg::udl;
//-----------------------------------------------------------------------------
// Parser
//-----------------------------------------------------------------------------
shared_ptr<Ast> parse(const string& source, ostream& out) {
parser parser(R"(
# Syntax Rules
EXPRESSION ← TERNARY
TERNARY ← CONDITION ('?' EXPRESSION ':' EXPRESSION)?
CONDITION ← MULTIPLICATIVE (ConditionOperator MULTIPLICATIVE)?
MULTIPLICATIVE ← CALL (MultiplicativeOperator CALL)*
CALL ← PRIMARY (EXPRESSION)?
PRIMARY ← FOR / Identifier / '(' EXPRESSION ')' / String / Number
FOR ← 'for' Identifier 'from' Number 'to' Number EXPRESSION
# Token Rules
ConditionOperator ← '=='
MultiplicativeOperator ← '%'
Identifier ← !Keyword < [a-zA-Z][a-zA-Z0-9_]* >
String ← "'" < ([^'] .)* > "'"
Number ← < [0-9]+ >
Keyword ← ('for' / 'from' / 'to') ![a-zA-Z]
%whitespace ← [ \t\r\n]*
)");
parser.enable_ast();
parser.set_logger([&](size_t ln, size_t col, const auto& msg) {
out << ln << ":" << col << ": " << msg << endl;
});
shared_ptr<Ast> ast;
if (parser.parse_n(source.data(), source.size(), ast)) {
return parser.optimize_ast(ast);
}
return nullptr;
}
//-----------------------------------------------------------------------------
// Value
//-----------------------------------------------------------------------------
struct Value;
using Function = function<Value(const Value&)>;
struct Value {
variant<nullptr_t, bool, long, string_view, Function> v;
Value() : v(nullptr) {}
explicit Value(bool b) : v(b) {}
explicit Value(long l) : v(l) {}
explicit Value(string_view s) : v(s) {}
explicit Value(Function f) : v(move(f)) {}
template <typename T>
T get() const {
try {
return std::get<T>(v);
} catch (bad_variant_access&) {
throw runtime_error("type error.");
}
}
bool operator==(const Value& rhs) const {
switch (v.index()) {
case 0:
return get<nullptr_t>() == rhs.get<nullptr_t>();
case 1:
return get<bool>() == rhs.get<bool>();
case 2:
return get<long>() == rhs.get<long>();
case 3:
return get<string_view>() == rhs.get<string_view>();
}
}
string str() const {
switch (v.index()) {
case 0:
return "nil";
case 1:
return get<bool>() ? "true" : "false";
case 2:
return to_string(get<long>());
case 3:
return string(get<string_view>());
}
}
};
//-----------------------------------------------------------------------------
// Environment
//-----------------------------------------------------------------------------
struct Environment {
shared_ptr<Environment> outer;
map<string_view, Value> values;
Environment(shared_ptr<Environment> outer = nullptr) : outer(outer) {}
const Value& get_value(string_view s) const {
if (auto it = values.find(s); it != values.end()) {
return it->second;
} else if (outer) {
return outer->get_value(s);
}
throw runtime_error("undefined variable '" + string(s) + "'...");
}
void set_value(string_view s, const Value& val) { values[s] = val; }
};
//-----------------------------------------------------------------------------
// Interpreter
//-----------------------------------------------------------------------------
Value eval(const Ast& ast, shared_ptr<Environment> env);
Value eval_ternary(const Ast& ast, shared_ptr<Environment> env) {
auto cond = eval(*ast.nodes[0], env).get<bool>();
auto val1 = eval(*ast.nodes[1], env);
auto val2 = eval(*ast.nodes[2], env);
return cond ? val1 : val2;
}
Value eval_condition(const Ast& ast, shared_ptr<Environment> env) {
auto lhs = eval(*ast.nodes[0], env);
auto rhs = eval(*ast.nodes[2], env);
return Value(lhs == rhs);
}
Value eval_multiplicative(const Ast& ast, shared_ptr<Environment> env) {
auto l = eval(*ast.nodes[0], env).get<long>();
for (size_t i = 1; i < ast.nodes.size(); i += 2) {
auto r = eval(*ast.nodes[i + 1], env).get<long>();
l = l % r;
}
return Value(l);
}
Value eval_call(const Ast& ast, shared_ptr<Environment> env) {
auto fn = env->get_value(ast.nodes[0]->token).get<Function>();
auto val = eval(*ast.nodes[1], env);
return fn(val);
}
Value eval_for(const Ast& ast, shared_ptr<Environment> env) {
auto ident = ast.nodes[0]->token;
auto from = eval(*ast.nodes[1], env).get<long>();
auto to = eval(*ast.nodes[2], env).get<long>();
auto& expr = *ast.nodes[3];
for (auto i = from; i <= to; i++) {
auto call_env = make_shared<Environment>(env);
call_env->set_value(ident, Value(i));
eval(expr, call_env);
}
return Value();
}
Value eval(const Ast& ast, shared_ptr<Environment> env) {
switch (ast.tag) {
case "TERNARY"_:
return eval_ternary(ast, env);
case "CONDITION"_:
return eval_condition(ast, env);
case "MULTIPLICATIVE"_:
return eval_multiplicative(ast, env);
case "CALL"_:
return eval_call(ast, env);
case "FOR"_:
return eval_for(ast, env);
case "Identifier"_:
return Value(env->get_value(ast.token));
case "String"_:
return Value(ast.token);
case "Number"_:
return Value(ast.token_to_number<long>());
default:
return Value();
}
}
Value interpret(const Ast& ast) {
auto env = make_shared<Environment>();
env->set_value("puts", Value(Function([](auto& val) {
cout << val.str() << endl;
return Value();
})));
return eval(ast, env);
}
//-----------------------------------------------------------------------------
// main
//-----------------------------------------------------------------------------
int main(int argc, const char** argv) {
if (argc < 2) {
cerr << "usage: fzbz [source file path]" << endl;
return 1;
}
ifstream file{argv[1]};
if (!file) {
cerr << "can't open the source file." << endl;
return 2;
}
auto source =
string(istreambuf_iterator<char>(file), istreambuf_iterator<char>());
auto ast = parse(source, cerr);
if (!ast) {
return 3;
}
try {
interpret(*ast);
} catch (const exception& e) {
cerr << e.what() << endl;
return 4;
}
return 0;
}