1 #include "proto_to_ruby.h"
3 using namespace ruby_fuzzer;
5 std::string protoConverter::removeSpecial(const std::string &x)
9 tmp.erase(std::remove_if(tmp.begin(), tmp.end(),
10 [](char c) { return !(std::isalpha(c) || std::isdigit(c)); } ), tmp.end());
14 void protoConverter::visit(ArrType const& x)
16 if (x.elements_size() > 0) {
17 int i = x.elements_size();
19 for (auto &e : x.elements()) {
34 void protoConverter::visit(Array const& x)
36 switch (x.arr_func()) {
39 m_output << ".flatten";
43 m_output << ".compact";
55 m_output << ".rotate";
59 m_output << ".rotate!";
63 m_output << ".delete_if";
67 m_output << ".insert";
71 m_output << ".bsearch";
75 m_output << ".keep_if";
79 m_output << ".select";
81 case Array::VALUES_AT:
83 m_output << ".values_at";
99 m_output << ".permutation";
103 m_output << ".combination";
107 m_output << ".assoc";
111 m_output << ".rassoc";
119 void protoConverter::visit(AssignmentStatement const& x)
121 m_output << "var_" << m_numLiveVars << " = ";
123 m_numVarsPerScope.top()++;
128 void protoConverter::visit(BinaryOp const& x)
133 case BinaryOp::ADD: m_output << " + "; break;
134 case BinaryOp::SUB: m_output << " - "; break;
135 case BinaryOp::MUL: m_output << " * "; break;
136 case BinaryOp::DIV: m_output << " / "; break;
137 case BinaryOp::MOD: m_output << " % "; break;
138 case BinaryOp::XOR: m_output << " ^ "; break;
139 case BinaryOp::AND: m_output << " and "; break;
140 case BinaryOp::OR: m_output << " or "; break;
141 case BinaryOp::EQ: m_output << " == "; break;
142 case BinaryOp::NE: m_output << " != "; break;
143 case BinaryOp::LE: m_output << " <= "; break;
144 case BinaryOp::GE: m_output << " >= "; break;
145 case BinaryOp::LT: m_output << " < "; break;
146 case BinaryOp::GT: m_output << " > "; break;
147 case BinaryOp::RS: m_output << " >> "; break;
153 void protoConverter::visit(BuiltinFuncs const& x)
155 switch (x.bifunc_oneof_case()) {
156 case BuiltinFuncs::kOs:
159 case BuiltinFuncs::kTime:
162 case BuiltinFuncs::kArr:
165 case BuiltinFuncs::kMops:
168 case BuiltinFuncs::BIFUNC_ONEOF_NOT_SET:
175 void protoConverter::visit(Const const& x)
177 switch (x.const_oneof_case()) {
179 m_output << "(" << (x.int_lit() % 13) << ")";
181 case Const::kBoolVal:
182 m_output << "(" << x.bool_val() << ")";
184 case Const::CONST_ONEOF_NOT_SET:
190 void protoConverter::visit(Function const& x)
192 m_output << "def foo()\nvar_0 = 1\n";
193 visit(x.statements());
198 void protoConverter::visit(HashType const& x)
200 if (x.keyval_size() > 0) {
201 int i = x.keyval_size();
203 for (auto &e : x.keyval()) {
217 void protoConverter::visit(IfElse const& x)
223 m_output << "\nelse\n";
224 visit(x.else_body());
225 m_output << "\nend\n";
228 void protoConverter::visit(KVPair const& x)
230 m_output << "\"" << removeSpecial(x.key()) << "\"";
232 m_output << "\"" << removeSpecial(x.val()) << "\"";
235 void protoConverter::visit(MathConst const& x)
237 switch (x.math_const()) {
239 m_output << "Math::PI";
242 m_output << "Math::E";
247 void protoConverter::visit(MathOps const& x)
249 switch (x.math_op()) {
251 m_output << "Math.cbrt(";
256 m_output << "Math.cos(";
261 m_output << "Math.erf(";
266 m_output << "Math.erfc(";
271 m_output << "Math.log(";
276 m_output << "Math.log10(";
281 m_output << "Math.log2(";
286 m_output << "Math.sin(";
291 m_output << "Math.sqrt(";
296 m_output << "Math.tan(";
303 void protoConverter::visit(MathType const& x)
305 switch (x.math_arg_oneof_case()) {
306 case MathType::kMathRval:
307 visit(x.math_rval());
309 case MathType::kMathConst:
310 visit(x.math_const());
312 case MathType::MATH_ARG_ONEOF_NOT_SET:
318 void protoConverter::visit(ObjectSpace const& x)
320 switch (x.os_func()) {
321 case ObjectSpace::COUNT:
322 m_output << "ObjectSpace.count_objects";
327 m_output << ")" << "\n";
330 void protoConverter::visit(Rvalue const& x)
332 switch (x.rvalue_oneof_case()) {
333 case Rvalue::kVarref:
342 case Rvalue::RVALUE_ONEOF_NOT_SET:
348 void protoConverter::visit(Statement const& x)
350 switch (x.stmt_oneof_case()) {
351 case Statement::kAssignment:
352 visit(x.assignment());
354 case Statement::kIfelse:
357 case Statement::kTernaryStmt:
358 visit(x.ternary_stmt());
360 case Statement::kBuiltins:
363 case Statement::kBlockstmt:
364 visit(x.blockstmt());
366 case Statement::STMT_ONEOF_NOT_SET:
372 void protoConverter::visit(StatementSeq const& x)
374 if (x.statements_size() > 0) {
375 m_numVarsPerScope.push(0);
376 m_output << "@scope ||= begin\n";
377 for (auto &st : x.statements())
380 m_numLiveVars -= m_numVarsPerScope.top();
381 m_numVarsPerScope.pop();
385 void protoConverter::visit(StringExtNoArg const& x)
387 m_output << "\"" << removeSpecial(x.str_arg()) << "\"";
388 switch (x.str_op()) {
389 case StringExtNoArg::DUMP:
392 case StringExtNoArg::STRIP:
393 m_output << ".strip";
395 case StringExtNoArg::LSTRIP:
396 m_output << ".lstrip";
398 case StringExtNoArg::RSTRIP:
399 m_output << ".rstrip";
401 case StringExtNoArg::STRIPE:
402 m_output << ".strip!";
404 case StringExtNoArg::LSTRIPE:
405 m_output << ".lstrip!";
407 case StringExtNoArg::RSTRIPE:
408 m_output << ".rstrip!";
410 case StringExtNoArg::SWAPCASE:
411 m_output << ".swapcase";
413 case StringExtNoArg::SWAPCASEE:
414 m_output << ".swapcase!";
416 case StringExtNoArg::SQUEEZE:
417 m_output << ".squeeze";
422 void protoConverter::visit(Ternary const& x)
425 visit(x.tern_cond());
433 void protoConverter::visit(Time const& x)
435 switch (x.t_func()) {
437 m_output << "Time.at";
440 m_output << "Time.gm";
443 m_output << "(" << (x.t_arg()% 13) << ")" << "\n";
446 void protoConverter::visit(VarRef const& x)
448 m_output << "var_" << (static_cast<uint32_t>(x.varnum()) % m_numLiveVars);
451 std::string protoConverter::FunctionToString(Function const& input)
454 return m_output.str();