TEST(EitherTest, ConstexprBasicTest) { constexpr Either<int, int> i = Right<int, int>(10); static_assert(i.isRight, "right is right"); static_assert(!i.isLeft, "right is not left."); static_assert(i.answer() == 10, "does not have value."); }
int main(int argc, char *argv[]) { boost::scoped_ptr<ProgramOptions> opts; try { opts.reset(new ProgramOptions(argc, const_cast<char const **>(argv), "a:m:q:v:")); } catch (std::exception &e) { std::cerr << e.what() << std::endl; return 1; } if (opts->arguments().size() < 2) { usage(opts->programName()); return 1; } boost::scoped_ptr<CorpusReader> reader; try { reader.reset(openCorpus(opts->arguments().at(0), true)); } catch (std::runtime_error &e) { std::cerr << "Could not open corpus: " << e.what() << std::endl; return 1; } alpinocorpus::Macros macros; if (opts->option('m')) { std::string macrosFn = opts->optionValue('m'); try { macros = alpinocorpus::loadMacros(macrosFn); } catch (std::runtime_error &e) { std::cerr << e.what() << std::endl; return 1; } } std::list<CorpusReader::MarkerQuery> markerQueries; if (opts->option('q')) { std::string query = alpinocorpus::expandMacros(macros, opts->optionValue('q')); Either<std::string, alpinocorpus::Empty> valid = reader->isValidQuery(CorpusReader::XPATH, false, query); if (valid.isLeft()) { std::cerr << "Invalid (or unwanted) query: " << query << std::endl << std::endl; std::cerr << valid.left() << std::endl; return 1; } std::string attribute = "active"; if (opts->option('a')) attribute = opts->optionValue('a'); std::string value = "1"; if (opts->option('v')) value = opts->optionValue('v'); markerQueries.push_back(CorpusReader::MarkerQuery(query, attribute, value)); } std::cout << reader->read(opts->arguments().at(1), markerQueries); }
TEST(EitherTets, IfRightTest) { { Either<int, int> i = Right<int, int>(10); int v = 0; ASSERT_EQ(i,i.ifRight([&](int i)->void{ v=1; })); ASSERT_EQ(1, v); } { Either<int, int> i = Left<int, int>(10); int v = 0; ASSERT_EQ(i, i.ifRight([&](int i)->void{ v=1; })); ASSERT_EQ(0, v); } }
MemberKey decode_member_key(PC& pc, Either<const Unit*, const UnitEmitter*> u) { auto const mcode = static_cast<MemberCode>(decode_byte(pc)); switch (mcode) { case MEC: case MEL: case MPC: case MPL: return MemberKey{mcode, decode_iva(pc)}; case MEI: return MemberKey{mcode, decode_raw<int64_t>(pc)}; case MET: case MPT: case MQT: { auto const id = decode_raw<Id>(pc); auto const str = u.match( [id](const Unit* u) { return u->lookupLitstrId(id); }, [id](const UnitEmitter* ue) { return ue->lookupLitstr(id); } ); return MemberKey{mcode, str}; } case MW: return MemberKey{}; } not_reached(); }
TEST(EitherTets, IfLeftTest) { { Either<int, int> i = Right<int, int>(10); int v = 0; i.ifLeft([&](int i)->void{ v=1; }); ASSERT_EQ(0, v); ASSERT_EQ(i, i.ifLeft([](int i) -> int{ return i+1; })); } { Either<int, int> i = Left<int, int>(10); int v = 0; i.ifLeft([&](int i)->void{ v=1; }); ASSERT_EQ(1, v); ASSERT_EQ(i, i.ifLeft([](int i) -> int{ return i+1; })); } }
std::string instrToString(PC it, Either<const Unit*, const UnitEmitter*> u) { std::stringstream out; PC iStart = it; Op op = decode_op(it); auto readRATA = [&] { if (auto unit = u.left()) { auto const rat = decodeRAT(unit, it); out << ' ' << show(rat); return; } auto const pc = it; it += encodedRATSize(pc); out << " <RepoAuthType>"; }; auto offsetOf = [u](PC pc) { return u.match( [pc](const Unit* u) { return u->offsetOf(pc); }, [pc](const UnitEmitter* ue) { return ue->offsetOf(pc); } ); }; auto lookupLitstrId = [u](Id id) { return u.match( [id](const Unit* u) { return u->lookupLitstrId(id); }, [id](const UnitEmitter* ue) { return ue->lookupLitstr(id); } ); }; auto lookupArrayId = [u](Id id) { return u.match( [id](const Unit* u) { return u->lookupArrayId(id); }, [id](const UnitEmitter* ue) { return ue->lookupArray(id); } ); }; switch (op) { #define READ(t) out << " " << *((t*)&*it); it += sizeof(t) #define READOFF() do { \ Offset _value = *(Offset*)it; \ out << " " << _value; \ if (u != nullptr) { \ out << " (" << offsetOf(iStart + _value) << ")"; \ } \ it += sizeof(Offset); \ } while (false) #define READV() out << " " << decodeVariableSizeImm(&it); #define READLA() out << " L:" << decodeVariableSizeImm(&it); #define READIVA() do { \ out << " "; \ auto imm = decodeVariableSizeImm((const uint8_t**)&it); \ if (op == OpIncStat && immIdx == 0) { \ out << Stats::g_counterNames[imm]; \ } else { \ out << imm; \ } \ immIdx++; \ } while (false) #define READOA(type) do { \ auto const immVal = static_cast<type>( \ *reinterpret_cast<const uint8_t*>(it) \ ); \ it += sizeof(unsigned char); \ out << " " << subopToName(immVal); \ } while (false) #define READLITSTR(sep) do { \ Id id = decode_raw<Id>(it); \ if (id < 0) { \ assert(op == OpSSwitch); \ out << sep << "-"; \ } else { \ auto const sd = lookupLitstrId(id); \ out << sep << "\"" << \ escapeStringForCPP(sd->data(), sd->size()) << "\""; \ } \ } while (false) #define READSVEC() do { \ int sz = decode_raw<int>(it); \ out << " <"; \ const char* sep = ""; \ for (int i = 0; i < sz; ++i) { \ out << sep; \ if (op == OpSSwitch) { \ READLITSTR(""); \ out << ":"; \ } \ Offset o = decode_raw<Offset>(it); \ out << offsetOf(iStart + o); \ sep = " "; \ } \ out << ">"; \ } while (false) #define READIVEC() do { \ int sz = decode_raw<int>(it); \ out << " <"; \ const char* sep = ""; \ for (int i = 0; i < sz; ++i) { \ out << sep; \ IterKind k = (IterKind)decode_raw<Id>(it); \ switch(k) { \ case KindOfIter: out << "(Iter) "; break; \ case KindOfMIter: out << "(MIter) "; break; \ case KindOfCIter: out << "(CIter) "; break; \ } \ out << decode_raw<Id>(it); \ sep = ", "; \ } \ out << ">"; \ } while (false) #define ONE(a) H_##a #define TWO(a, b) H_##a; H_##b #define THREE(a, b, c) H_##a; H_##b; H_##c; #define FOUR(a, b, c, d) H_##a; H_##b; H_##c; H_##d; #define NA #define H_BLA READSVEC() #define H_SLA READSVEC() #define H_ILA READIVEC() #define H_IVA READIVA() #define H_I64A READ(int64_t) #define H_LA READLA() #define H_IA READV() #define H_DA READ(double) #define H_BA READOFF() #define H_OA(type) READOA(type) #define H_SA READLITSTR(" ") #define H_RATA readRATA() #define H_AA do { \ out << ' '; \ staticArrayStreamer(lookupArrayId(decode_raw<Id>(it)), out); \ } while (false) #define H_VSA do { \ int sz = decode_raw<int32_t>(it); \ out << " <"; \ for (int i = 0; i < sz; ++i) { \ H_SA; \ } \ out << " >"; \ } while (false) #define H_KA out << ' ' << show(decode_member_key(it, u)) #define O(name, imm, push, pop, flags) \ case Op##name: { \ out << #name; \ UNUSED unsigned immIdx = 0; \ imm; \ break; \ } OPCODES #undef O #undef READ #undef READV #undef READLA #undef ONE #undef TWO #undef THREE #undef FOUR #undef NA #undef H_BLA #undef H_SLA #undef H_ILA #undef H_IVA #undef H_I64A #undef H_LA #undef H_IA #undef H_DA #undef H_BA #undef H_OA #undef H_SA #undef H_AA #undef H_VSA #undef H_KA default: assert(false); }; return out.str(); }
int main(int argc, char *argv[]) { boost::scoped_ptr<ProgramOptions> opts; try { opts.reset(new ProgramOptions(argc, const_cast<char const **>(argv), "a:m:q:s")); } catch (std::exception &e) { std::cerr << e.what() << std::endl; return 1; } if (opts->arguments().size() == 0) { usage(opts->programName()); return 1; } boost::shared_ptr<CorpusReader> reader; try { if (opts->arguments().size() == 1) reader = boost::shared_ptr<CorpusReader>( openCorpus(opts->arguments().at(0), true)); else reader = boost::shared_ptr<CorpusReader>( openCorpora(opts->arguments().begin(), opts->arguments().end(), true)); } catch (std::runtime_error &e) { std::cerr << "Could not open corpus: " << e.what() << std::endl; return 1; } CorpusInfo corpusInfo = alpinocorpus::predefinedCorpusOrFallback(reader->type()); std::string attr = corpusInfo.tokenAttribute(); if (opts->option('a')) { attr = opts->optionValue('a'); } alpinocorpus::Macros macros; if (opts->option('m')) { std::string macrosFn = opts->optionValue('m'); try { macros = alpinocorpus::loadMacros(macrosFn); } catch (std::runtime_error &e) { std::cerr << e.what() << std::endl; return 1; } } std::string query; if (opts->option('q')) { query = alpinocorpus::expandMacros(macros, opts->optionValue('q')); Either<std::string, alpinocorpus::Empty> valid = reader->isValidQuery(CorpusReader::XPATH, false, query); if (valid.isLeft()) { std::cerr << "Invalid (or unwanted) query: " << query << std::endl << std::endl; std::cerr << valid.left() << std::endl; return 1; } } try { listCorpus(reader, query, opts->option('s'), attr, corpusInfo); } catch (std::runtime_error const &e) { std::cerr << opts->programName() << ": error listing treebank: " << e.what() << std::endl; return 1; } return 0; }
std::string instrToString(PC it, Either<const Unit*, const UnitEmitter*> u) { std::string out; PC iStart = it; Op op = decode_op(it); auto readRATA = [&] { if (auto unit = u.left()) { auto const rat = decodeRAT(unit, it); folly::format(&out, " {}", show(rat)); return; } auto const pc = it; it += encodedRATSize(pc); out += " <RepoAuthType>"; }; auto offsetOf = [u](PC pc) { return u.match( [pc](const Unit* u) { return u->offsetOf(pc); }, [pc](const UnitEmitter* ue) { return ue->offsetOf(pc); } ); }; auto lookupLitstrId = [u](Id id) { return u.match( [id](const Unit* u) { return u->lookupLitstrId(id); }, [id](const UnitEmitter* ue) { return ue->lookupLitstr(id); } ); }; auto lookupArrayId = [u](Id id) { return u.match( [id](const Unit* u) { return u->lookupArrayId(id); }, [id](const UnitEmitter* ue) { return ue->lookupArray(id); } ); }; switch (op) { #define READ(t) folly::format(&out, " {}", *((t*)&*it)); it += sizeof(t) #define READOFF() do { \ Offset _value = *(Offset*)it; \ folly::format(&out, " {}", _value); \ if (u != nullptr) { \ folly::format(&out, " ({})", offsetOf(iStart + _value)); \ } \ it += sizeof(Offset); \ } while (false) #define READV() folly::format(&out, " {}", decode_iva(it)); #define READLA() folly::format(&out, " L:{}", decode_iva(it)); #define READIVA() do { \ auto imm = decode_iva(it); \ folly::format(&out, " {}", imm); \ immIdx++; \ } while (false) #define READOA(type) do { \ auto const immVal = static_cast<type>( \ *reinterpret_cast<const uint8_t*>(it) \ ); \ it += sizeof(unsigned char); \ folly::format(&out, " {}", subopToName(immVal)); \ } while (false) #define READLITSTR(sep) do { \ Id id = decode_raw<Id>(it); \ if (id < 0) { \ assertx(op == OpSSwitch); \ folly::format(&out, "{}-", sep); \ } else { \ auto const sd = lookupLitstrId(id); \ folly::format(&out, "{}\"{}\"", sep, \ escapeStringForCPP(sd->data(), sd->size())); \ } \ } while (false) #define READSVEC() do { \ int sz = decode_iva(it); \ out += " <"; \ const char* sep = ""; \ for (int i = 0; i < sz; ++i) { \ out += sep; \ if (op == OpSSwitch) { \ READLITSTR(""); \ out += ":"; \ } \ Offset o = decode_raw<Offset>(it); \ folly::format(&out, "{}", offsetOf(iStart + o)); \ sep = " "; \ } \ out += ">"; \ } while (false) #define READI32VEC() do { \ int sz = decode_iva(it); \ out += " <"; \ const char* sep = ""; \ for (int i = 0; i < sz; ++i) { \ folly::format(&out, "{}{}", sep, decode_raw<uint32_t>(it));\ sep = ", "; \ } \ out += ">"; \ } while (false) #define READBOOLVEC() do { \ int sz = decode_iva(it); \ uint8_t tmp = 0; \ out += " \""; \ for (int i = 0; i < sz; ++i) { \ if (i % 8 == 0) tmp = decode_raw<uint8_t>(it); \ out += ((tmp >> (i % 8)) & 1) ? "1" : "0"; \ } \ out += "\""; \ } while (false) #define READITERTAB() do { \ auto const sz = decode_iva(it); \ out += " <"; \ const char* sep = ""; \ for (int i = 0; i < sz; ++i) { \ out += sep; \ auto const k = (IterKind)decode_iva(it); \ switch (k) { \ case KindOfIter: out += "(Iter) "; break; \ case KindOfMIter: out += "(MIter) "; break; \ case KindOfCIter: out += "(CIter) "; break; \ case KindOfLIter: out += "(LIter) "; break; \ } \ folly::format(&out, "{}", decode_iva(it)); \ if (k == KindOfLIter) { \ folly::format(&out, " L:{}", decode_iva(it)); \ } \ sep = ", "; \ } \ out += ">"; \ } while (false) #define ONE(a) H_##a #define TWO(a, b) H_##a; H_##b #define THREE(a, b, c) H_##a; H_##b; H_##c; #define FOUR(a, b, c, d) H_##a; H_##b; H_##c; H_##d; #define FIVE(a, b, c, d, e) H_##a; H_##b; H_##c; H_##d; H_##e; #define NA #define H_BLA READSVEC() #define H_SLA READSVEC() #define H_ILA READITERTAB() #define H_I32LA READI32VEC() #define H_BLLA READBOOLVEC() #define H_IVA READIVA() #define H_I64A READ(int64_t) #define H_LA READLA() #define H_IA READV() #define H_CAR READV() #define H_CAW READV() #define H_DA READ(double) #define H_BA READOFF() #define H_OA(type) READOA(type) #define H_SA READLITSTR(" ") #define H_RATA readRATA() #define H_AA do { \ out += ' '; \ staticArrayStreamer(lookupArrayId(decode_raw<Id>(it)), out); \ } while (false) #define H_VSA do { \ int sz = decode_iva(it); \ out += " <"; \ for (int i = 0; i < sz; ++i) { \ H_SA; \ } \ out += " >"; \ } while (false) #define H_KA (out += ' ', out += show(decode_member_key(it, u))) #define H_LAR (out += ' ', out += show(decodeLocalRange(it))) #define H_FCA (out += ' ', out += show(decodeFCallArgs(it))) #define O(name, imm, push, pop, flags) \ case Op##name: { \ out += #name; \ UNUSED unsigned immIdx = 0; \ imm; \ break; \ } OPCODES #undef O #undef READ #undef READV #undef READLA #undef ONE #undef TWO #undef THREE #undef FOUR #undef FIVE #undef NA #undef H_BLA #undef H_SLA #undef H_ILA #undef H_I32LA #undef H_BLLA #undef H_IVA #undef H_I64A #undef H_LA #undef H_IA #undef H_CAR #undef H_CAW #undef H_DA #undef H_BA #undef H_OA #undef H_SA #undef H_AA #undef H_VSA #undef H_KA #undef H_LAR #undef H_FCA default: assertx(false); }; return out; }