Skip to content
Snippets Groups Projects
Select Git revision
  • ce870e9bd834992bc665c8e6c19591792cbeece6
  • master default protected
2 results

Model.vue

Blame
  • ActionBank.cpp 18.10 KiB
    #include "ActionBank.hpp"
    #include "util.hpp"
    #include "ProgramParameters.hpp"
    
    std::vector<Action::BasicAction> ActionBank::str2sequence(const std::string & name)
    {
      auto invalidNameAndAbort = [&](const char * errInfo)
      {
        fprintf(stderr, "ERROR (%s) : unknown action name \'%s\' Aborting.\n", errInfo, name.c_str());
    
        exit(1);
      };
    
      std::vector<Action::BasicAction> sequence;
    
      char b1[1024];
      char b2[1024];
      char b3[1024];
      char b4[1024];
    
      if (sscanf(name.c_str(), "%s", b1) != 1)
        invalidNameAndAbort(ERRINFO);
    
      if(std::string(b1) == "WRITE")
      {
        if (sscanf(name.c_str(), "%s %s %s %s", b1, b4, b2, b3) != 4)
          invalidNameAndAbort(ERRINFO);
    
        std::string tapeName(b2);
        std::string value(b3);
        auto object = split(b4, '.');
    
        if (object.size() != 2)
          invalidNameAndAbort(ERRINFO);
    
        if (object[0] == "b")
        {
          int relativeIndex = std::stoi(object[1]);
    
          auto apply = [tapeName, value, relativeIndex](Config & c, Action::BasicAction &)
            {
              simpleBufferWrite(c, tapeName, value, relativeIndex);
            };
          auto undo = [tapeName, relativeIndex](Config & c, Action::BasicAction &)
            {
              simpleBufferWrite(c, tapeName, "", relativeIndex);
            };
          auto appliable = [tapeName, relativeIndex](Config & c, Action::BasicAction &)
            {
              return simpleBufferWriteAppliable(c, tapeName, relativeIndex);
            };
          Action::BasicAction basicAction =
            {Action::BasicAction::Type::Write, value, apply, undo, appliable};
    
          sequence.emplace_back(basicAction);
        }
        else if (object[0] == "s")
        {
           auto apply = [tapeName, value, object](Config & c, Action::BasicAction &)
            {
              int stackIndex = std::stoi(object[1]);
              int bufferIndex = c.stackGetElem(stackIndex);
              int relativeIndex = bufferIndex - c.head;
              simpleBufferWrite(c, tapeName, value, relativeIndex);
            };
          auto undo = [tapeName, object](Config & c, Action::BasicAction &)
            {
              int stackIndex = std::stoi(object[1]);
              int bufferIndex = c.stackGetElem(stackIndex);
              int relativeIndex = bufferIndex - c.head;