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