Exemplo n.º 1
0
int main(int argc, char *argv[])
{
   char *pcLine;
   DynArray_T oTokens;

   pcPgmName = argv[0];

   printf("%s \n", pcPgmName);
   printf("------------------------------------\n");

   while ((pcLine = readLine(stdin)) != NULL)
   {
      printf("Line: %s\n", pcLine);
      fflush(stdout);
      oTokens = lexLine(pcLine);
      if (oTokens != NULL)
      {
         writeTokens(oTokens);
         freeTokens(oTokens);
         DynArray_free(oTokens);
      }

      printf("------------------------------------\n");
      free(pcLine);
   }

   return 0;
}
Exemplo n.º 2
0
static void performCommand(char *acLine, DynArray_T oHistoryList, 
                           char *pcProgName)

/* Expand any !commandprefix in acLine. Insert acLine into 
   oHistoryList iff the expanding succeeds and acLine does not consist
   of entirely whitespace characters. Lexically and syntactically 
   analyze acLine. Execute acLine if no errors are found. It is a 
   checked runtime error for acLine, oHistory, or pcProgName to be
   NULL. */

{
   char *pcTemp;
   Command_T oCommand;
   DynArray_T oTokens;
   int iSuccessful;

   assert(acLine != NULL);
   assert(oHistoryList != NULL);
   assert(pcProgName != NULL);

   if(histHasCommandPrefix(acLine))
   {
      iSuccessful = histExpandLine(acLine, oHistoryList, pcProgName);
      if(iSuccessful)
         printf("%s\n", acLine);
      else
         return;
   }
   oTokens = DynArray_new(0);
   iSuccessful = lexLine(acLine, oTokens, pcProgName);
   if(DynArray_getLength(oTokens) > 0)
   {
      /* Allocate memory to store command in oHistoryList iff 
         command does not consist of entirely whitespace
         characters. */
      pcTemp = (char*)malloc(strlen(acLine) + 1);
      assert(pcTemp != NULL);
      strcpy(pcTemp, acLine);
      DynArray_add(oHistoryList, pcTemp);

      if(iSuccessful)
      {
         oCommand = Command_new();
         iSuccessful = parseToken(oTokens, oCommand, pcProgName);
         if(iSuccessful)
            execute(oCommand, oHistoryList, pcProgName);
         Command_free(oCommand, NULL);
      }
   }
   DynArray_map(oTokens, Token_free, NULL);
   DynArray_free(oTokens);
}
Exemplo n.º 3
0
void Reception::run()
{
  std::string line;

  std::cout << "La Piazza> ";
  while(std::getline(std::cin, line))
    {
      if (line.size() == 0)
        {
          std::cout << "La Piazza> ";
          continue;
        }
      replaceAll(line, ";", " ; ");
      std::istringstream iss(line);
      std::vector<std::string> tokens;

      std::copy(std::istream_iterator<std::string>(iss),
                std::istream_iterator<std::string>(),
                std::back_inserter< std::vector<std::string> >(tokens));

      lexLine(tokens);

      for (std::list<Reception::Lexem>::iterator it = _lex.begin(), end = _lex.end();
           it != end; ++it)
        {
          if (*it == UNKNOWN)
            {
              std::cerr << "Your command is invalid" << std::endl;
              break;
            }
        }

      parseLexem(tokens);
      _lex.clear();

      for (std::deque<Kitchen*>::iterator it = _kitchens.begin(), end = _kitchens.end();
           it != end; ++it)
        {
          (*it)->getStatus();
        }

      //check here terminated commands

      //add new pizza commands
      for (std::deque<Command*>::const_iterator it = _commands.begin(), end = _commands.end();
           it != end; ++it)
        {
          const std::list<APizza*>& pizzaList = (*it)->getPizzas();
          for (std::list<APizza*>::const_iterator jt = pizzaList.begin(), end = pizzaList.end();
               jt != end; ++jt)
            {
              for (std::deque<Kitchen*>::const_iterator kt = _kitchens.begin(), end = _kitchens.end();
                   kt != end; ++kt)
                {
                  (*kt)->newOrder((*jt));
                }
            }
        }

      for (std::deque<Kitchen*>::iterator it = _kitchens.begin(), end = _kitchens.end();
           it != end; ++it)
        {
          (*it)->getStatus();
        }

      std::cout << "La Piazza> ";
    }
}