Skip to content
Snippets Groups Projects
Transition.cpp 1.25 KiB
Newer Older
#include "Transition.hpp"
#include <regex>
Transition::Transition(const std::string & name)
{
  this->name = name;

  std::regex writeRegex("WRITE ([bs])\\.(.+) (.+) (.+)");

  auto doIfNameMatch = [name](auto & reg, auto init)
  {
    std::smatch sm;
    std::regex_match(name, sm, reg);
    if (sm.empty())
      return false;

    init(sm);

    return true;
  };
  if (doIfNameMatch(writeRegex, [this](auto sm){initWrite(sm[3], sm[1], sm[2], sm[4]);}))
  throw std::invalid_argument("no match");

  } catch (std::exception & e) {util::myThrow(fmt::format("Invalid name '{}' ({})", name, e.what()));}

}

void Transition::initWrite(std::string colName, std::string object, std::string index, std::string value)
{
  auto objectValue = Action::str2object(object);
  int indexValue = std::stoi(index);

  sequence.emplace_back(Action::addHypothesisRelative(colName, objectValue, indexValue, value));

  cost = [colName, objectValue, indexValue, value](const Config & config)
  {
    int lineIndex = 0;
    if (objectValue == Action::Object::Buffer)
      lineIndex = config.getWordIndex() + indexValue;
    else
      lineIndex = config.getStack(indexValue);
    if (config.getConst(colName, lineIndex, 0) == value)
      return 0;