Skip to content
Snippets Groups Projects
Commit 5cc6a43b authored by Franck Dary's avatar Franck Dary
Browse files

Introduced optional state in transitions

parent eac4e36f
No related branches found
No related tags found
No related merge requests found
......@@ -10,6 +10,7 @@ class Transition
private :
std::string name;
std::string state;
std::vector<Action> sequence;
std::function<int(const Config & config)> cost;
......
......@@ -3,8 +3,7 @@
Transition::Transition(const std::string & name)
{
this->name = name;
std::regex nameRegex("(<(.+)> )?(.+)");
std::regex writeRegex("WRITE ([bs])\\.(.+) (.+) (.+)");
std::regex shiftRegex("SHIFT");
std::regex reduceRegex("REDUCE");
......@@ -14,23 +13,29 @@ Transition::Transition(const std::string & name)
try
{
if (util::doIfNameMatch(writeRegex, name, [this](auto sm){initWrite(sm[3], sm[1], sm[2], sm[4]);}))
if (!util::doIfNameMatch(nameRegex, name, [this, name](auto sm)
{
this->state = sm[2];
this->name = sm[3];
}))
util::myThrow("doesn't match nameRegex");
if (util::doIfNameMatch(writeRegex, this->name, [this](auto sm){initWrite(sm[3], sm[1], sm[2], sm[4]);}))
return;
if (util::doIfNameMatch(shiftRegex, name, [this](auto){initShift();}))
if (util::doIfNameMatch(shiftRegex, this->name, [this](auto){initShift();}))
return;
if (util::doIfNameMatch(reduceRegex, name, [this](auto){initReduce();}))
if (util::doIfNameMatch(reduceRegex, this->name, [this](auto){initReduce();}))
return;
if (util::doIfNameMatch(leftRegex, name, [this](auto sm){initLeft(sm[1]);}))
if (util::doIfNameMatch(leftRegex, this->name, [this](auto sm){initLeft(sm[1]);}))
return;
if (util::doIfNameMatch(rightRegex, name, [this](auto sm){initRight(sm[1]);}))
if (util::doIfNameMatch(rightRegex, this->name, [this](auto sm){initRight(sm[1]);}))
return;
if (util::doIfNameMatch(eosRegex, name, [this](auto){initEOS();}))
if (util::doIfNameMatch(eosRegex, this->name, [this](auto){initEOS();}))
return;
throw std::invalid_argument("no match");
} catch (std::exception & e) {util::myThrow(fmt::format("Invalid name '{}' ({})", name, e.what()));}
} catch (std::exception & e) {util::myThrow(fmt::format("Invalid name '{}' ({})", this->name, e.what()));}
}
......@@ -42,6 +47,9 @@ void Transition::apply(Config & config)
bool Transition::appliable(const Config & config) const
{
if (!state.empty() && state != config.getState())
return false;
for (const Action & action : sequence)
if (!action.appliable(config, action))
return false;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment