Ignore:
Timestamp:
Sep 29, 2014, 2:11:34 PM (5 years ago)
Author:
nmedfort
Message:

More refactoring of the RE system; moved the original re/RE_Compiler to compiler.cpp and the PBIX_Compiler to the re/RE_Compiler.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • icGREP/icgrep-devel/icgrep/re/re_nullable.cpp

    r4194 r4197  
    1919RE * RE_Nullable::removeNullablePrefix(RE * re) {
    2020    if (Seq * seq = dyn_cast<Seq>(re)) {
    21         re = removeNullableSeqPrefix(seq);
     21        re = removeNullablePrefix(seq);
    2222    }
    2323    else if (Alt * alt = dyn_cast<Alt>(re)) {
     
    3333        else if (hasNullablePrefix(rep->getRE())) {
    3434            Seq * seq = makeSeq();
    35             seq->push_back(removeNullablePrefix(rep->getRE()));
     35            seq->push_back(removeNullablePrefix(rep->getRE()->clone()));
    3636            seq->push_back(makeRep(rep->getRE(), rep->getLB() - 1, rep->getLB() - 1));
     37            rep->setRE(nullptr);
     38            delete rep;
    3739            re = RE_Simplifier::simplify(seq);
    3840        }
     
    4446}
    4547
    46 inline Seq * RE_Nullable::removeNullableSeqPrefix(const Seq * seq) {
    47     Seq * new_seq = makeSeq(seq->getType());
     48inline Seq * RE_Nullable::removeNullablePrefix(Seq * seq) {
    4849    if (!seq->empty()) {
     50        std::vector<RE *> list;
    4951        auto i = seq->begin();
    5052        // find the first non-nullable prefix
    5153        while (i != seq->end() && isNullable(*i)) {
     54            delete *i;
    5255            ++i;
    5356        }
    54         if (i == seq->end()) {
    55             return new_seq;
     57        if (i != seq->end()) {
     58            // push the first non-nullable seq item to the front of the new_seq
     59            list.push_back(removeNullablePrefix(*i));
     60            std::copy(++i, seq->end(), std::back_inserter(list));
    5661        }
    57         // push the first non-nullable seq item to the front of the new_seq
    58         new_seq->push_back(removeNullablePrefix(*i));
    59         std::copy(++i, seq->end(), std::back_inserter(*new_seq));
     62        seq->swap(list);
    6063    }
    61     return new_seq;
     64    return seq;
    6265}
    6366
    6467RE * RE_Nullable::removeNullableSuffix(RE * re) {
    6568    if (Seq * seq = dyn_cast<Seq>(re)) {
    66         re = removeNullableSeqSuffix(seq);
     69        re = removeNullableSuffix(seq);
    6770    }
    6871    else if (Alt* alt = dyn_cast<Alt>(re)) {
     
    8083            seq->push_back(RE_Simplifier::simplify(makeRep(rep->getRE()->clone(), rep->getLB() - 1, rep->getLB() - 1)));
    8184            seq->push_back(removeNullableSuffix(rep->getRE()));
     85            rep->setRE(nullptr);
    8286            delete rep;
    8387            re = RE_Simplifier::simplify(seq);
     
    9094}
    9195
    92 inline Seq * RE_Nullable::removeNullableSeqSuffix(const Seq * seq) {
    93     Seq * new_seq = makeSeq(seq->getType());
     96inline Seq * RE_Nullable::removeNullableSuffix(Seq * seq) {
    9497    if (!seq->empty()) {
     98        std::vector<RE *> list;
    9599        auto i = seq->end();
    96100        // find the last non-nullable suffix
    97         while (i != seq->begin() && isNullable(*--i));
    98 
     101        while (i != seq->begin() && isNullable(*--i)) {
     102            delete *i;
     103        }
    99104        if (i != seq->begin()) {
    100             std::copy(seq->begin(), i, std::back_inserter(*new_seq));
    101             new_seq->push_back(removeNullableSuffix(*i));
     105            std::copy(seq->begin(), i, std::back_inserter(list));
     106            list.push_back(removeNullableSuffix(*i));
    102107        }
     108        seq->swap(list);
    103109    }
    104     return new_seq;
     110    return seq;
    105111}
    106112
    107113bool RE_Nullable::isNullable(const RE * re) {
    108114    if (const Seq * re_seq = dyn_cast<const Seq>(re)) {
    109         return isNullableVector(re_seq);
     115        return isNullable(re_seq);
    110116    }
    111117    else if (const Alt* re_alt = dyn_cast<const Alt>(re)) {
    112         return isNullableVector(re_alt);
     118        return isNullable(re_alt);
    113119    }
    114120    else if (const Rep* re_rep = dyn_cast<const Rep>(re)) {
     
    118124}
    119125
    120 inline bool RE_Nullable::isNullableVector(const Vector * vec) {
     126inline bool RE_Nullable::isNullable(const Vector * vec) {
    121127    for (const RE * re : *vec) {
    122128        if (!isNullable(re)) {
Note: See TracChangeset for help on using the changeset viewer.