Remove `CommandSequence` command (#8904)
authorAndres Noetzli <andres.noetzli@gmail.com>
Thu, 23 Jun 2022 20:51:05 +0000 (13:51 -0700)
committerGitHub <noreply@github.com>
Thu, 23 Jun 2022 20:51:05 +0000 (20:51 +0000)
The CommandSequence command was not widely used and was essentially
just replicating the functionality of a vector of commands. This is work
towards a streamlined parser API.

15 files changed:
src/main/command_executor.cpp
src/main/driver_unified.cpp
src/main/interactive_shell.cpp
src/main/interactive_shell.h
src/parser/smt2/Smt2.g
src/parser/tptp/Tptp.g
src/printer/ast/ast_printer.cpp
src/printer/ast/ast_printer.h
src/printer/printer.cpp
src/printer/printer.h
src/printer/smt2/smt2_printer.cpp
src/printer/smt2/smt2_printer.h
src/smt/command.cpp
src/smt/command.h
test/unit/main/interactive_shell_black.cpp

index 96b771ac2e13c55ee947c36b679083d825c1a26c..6e21040538d5a64a723ebceb90b269771d48e7bd 100644 (file)
@@ -84,27 +84,12 @@ void CommandExecutor::printStatisticsSafe(int fd) const
 
 bool CommandExecutor::doCommand(Command* cmd)
 {
-  CommandSequence *seq = dynamic_cast<CommandSequence*>(cmd);
-  if(seq != nullptr) {
-    // assume no error
-    bool status = true;
-
-    for (CommandSequence::iterator subcmd = seq->begin();
-         status && subcmd != seq->end();
-         ++subcmd)
-    {
-      status = doCommand(*subcmd);
-    }
-
-    return status;
-  } else {
-    if (d_solver->getOptionInfo("verbosity").intValue() > 2)
-    {
-      d_solver->getDriverOptions().out() << "Invoking: " << *cmd << std::endl;
-    }
-
-    return doCommandSingleton(cmd);
+  if (d_solver->getOptionInfo("verbosity").intValue() > 2)
+  {
+    d_solver->getDriverOptions().out() << "Invoking: " << *cmd << std::endl;
   }
+
+  return doCommandSingleton(cmd);
 }
 
 void CommandExecutor::reset()
index 859941cc50d8e5d75942e6ccb3bf55e189b6cf21..7887ed08ac18edf796c790c26486cc5a7eecc78b 100644 (file)
@@ -22,6 +22,7 @@
 #include <iostream>
 #include <memory>
 #include <new>
+#include <optional>
 
 #include "api/cpp/cvc5.h"
 #include "base/configuration.h"
@@ -162,7 +163,6 @@ int runCvc5(int argc, char* argv[], std::unique_ptr<cvc5::Solver>& solver)
     solver->setInfo("filename", filenameStr);
 
     // Parse and execute commands until we are done
-    std::unique_ptr<cvc5::Command> cmd;
     bool status = true;
     if (solver->getOptionInfo("interactive").boolValue() && inputFromStdin)
     {
@@ -187,14 +187,23 @@ int runCvc5(int argc, char* argv[], std::unique_ptr<cvc5::Solver>& solver)
           << std::endl
           << Configuration::copyright() << std::endl;
 
-      while(true) {
-        cmd.reset(shell.readCommand());
-        if (cmd == nullptr)
-          break;
-        status = pExecutor->doCommand(cmd) && status;
-        if (cmd->interrupted()) {
+      bool quit = false;
+      while (!quit)
+      {
+        std::optional<InteractiveShell::CmdSeq> cmds = shell.readCommand();
+        if (!cmds)
+        {
           break;
         }
+        for (std::unique_ptr<cvc5::Command>& cmd : *cmds)
+        {
+          status = pExecutor->doCommand(cmd) && status;
+          if (cmd->interrupted())
+          {
+            quit = true;
+            break;
+          }
+        }
       }
     }
     else
index d9f6ece6d4d94173d3097961e418461e11da4ebb..cd1f118816a7df94327736589f6231eaea42ca34 100644 (file)
  */
 #include "main/interactive_shell.h"
 
-#include <cstring>
 #include <unistd.h>
 
 #include <algorithm>
 #include <cstdlib>
+#include <cstring>
 #include <iostream>
+#include <optional>
 #include <set>
 #include <string>
 #include <utility>
@@ -170,7 +171,7 @@ InteractiveShell::~InteractiveShell() {
 #endif /* HAVE_LIBEDITLINE */
 }
 
-Command* InteractiveShell::readCommand()
+std::optional<InteractiveShell::CmdSeq> InteractiveShell::readCommand()
 {
   char* lineBuf = NULL;
   string line = "";
@@ -181,7 +182,7 @@ restart:
    * QuitCommand. */
   if(d_in.eof() || d_quit) {
     d_out << endl;
-    return NULL;
+    return {};
   }
 
   /* If something's wrong with the input, there's nothing we can do. */
@@ -248,7 +249,7 @@ restart:
       if(input.empty()) {
         /* Nothing left to parse. */
         d_out << endl;
-        return NULL;
+        return {};
       }
 
       /* Some input left to parse, but nothing left to read.
@@ -306,14 +307,14 @@ restart:
 
   /* There may be more than one command in the input. Build up a
      sequence. */
-  CommandSequence *cmd_seq = new CommandSequence();
+  std::vector<std::unique_ptr<cvc5::Command>> cmdSeq;
   Command *cmd;
 
   try
   {
     while ((cmd = d_parser->nextCommand()))
     {
-      cmd_seq->addCommand(cmd);
+      cmdSeq.emplace_back(cmd);
       if (dynamic_cast<QuitCommand*>(cmd) != NULL)
       {
         d_quit = true;
@@ -376,7 +377,7 @@ restart:
     // cmd_seq = new CommandSequence();
   }
 
-  return cmd_seq;
+  return std::optional<CmdSeq>(std::move(cmdSeq));
 }/* InteractiveShell::readCommand() */
 
 #if HAVE_LIBEDITLINE
index 32b283d3801fe56cb757039ee2f391693d2575ef..1943d7d1692b8b021f9ac769550b920ae972cf24 100644 (file)
@@ -18,7 +18,9 @@
 
 #include <iosfwd>
 #include <memory>
+#include <optional>
 #include <string>
+#include <vector>
 
 namespace cvc5 {
 
@@ -37,6 +39,8 @@ namespace parser {
   class InteractiveShell
   {
    public:
+    using CmdSeq = std::vector<std::unique_ptr<cvc5::Command>>;
+
     InteractiveShell(Solver* solver,
                      SymbolManager* sm,
                      std::istream& in,
@@ -48,10 +52,10 @@ namespace parser {
     ~InteractiveShell();
 
     /**
-     * Read a command from the interactive shell. This will read as
-     * many lines as necessary to parse a well-formed command.
+     * Read a list of commands from the interactive shell. This will read as
+     * many lines as necessary to parse at least one well-formed command.
      */
-    Command* readCommand();
+    std::optional<CmdSeq> readCommand();
 
     /**
      * Return the internal parser being used.
index ee0738b114233832ad9da3a1a7925f5668f7ab9a..bc5ee3147a14d423ef6a74e097c1e6cc95fdd69d 100644 (file)
@@ -725,7 +725,6 @@ smt25Command[std::unique_ptr<cvc5::Command>* cmd]
   std::vector<cvc5::Term> funcs;
   std::vector<cvc5::Term> func_defs;
   cvc5::Term aexpr;
-  std::unique_ptr<cvc5::CommandSequence> seq;
   std::vector<cvc5::Sort> sorts;
   std::vector<cvc5::Term> flattenVars;
 }
@@ -863,7 +862,6 @@ extendedCommand[std::unique_ptr<cvc5::Command>* cmd]
   std::vector<cvc5::Term> terms;
   std::vector<cvc5::Sort> sorts;
   std::vector<std::pair<std::string, cvc5::Sort> > sortedVarNames;
-  std::unique_ptr<cvc5::CommandSequence> seq;
   cvc5::Grammar* g = nullptr;
 }
     /* Extended SMT-LIB set of commands syntax, not permitted in
index 0db135c835a165ae22d20329954ca072e05cadf5..c238f324cd328f84dfa6d49442966cbe69813c4d 100644 (file)
@@ -238,12 +238,11 @@ parseCommand returns [cvc5::Command* cmd = NULL]
     }
   | EOF
     {
-      CommandSequence* seq = new CommandSequence();
       // assert that all distinct constants are distinct
       cvc5::Term aexpr = PARSER_STATE->getAssertionDistinctConstants();
       if( !aexpr.isNull() )
       {
-        seq->addCommand(new AssertCommand(aexpr));
+        PARSER_STATE->preemptCommand(new AssertCommand(aexpr));
       }
 
       std::string filename = PARSER_STATE->getInput()->getInputStreamName();
@@ -254,15 +253,14 @@ parseCommand returns [cvc5::Command* cmd = NULL]
       if(filename.substr(filename.length() - 2) == ".p") {
         filename = filename.substr(0, filename.length() - 2);
       }
-      seq->addCommand(new SetInfoCommand("filename", filename));
+      PARSER_STATE->preemptCommand(new SetInfoCommand("filename", filename));
       if(PARSER_STATE->hasConjecture()) {
         // note this does not impact how the TPTP status is reported currently
-        seq->addCommand(new CheckSatAssumingCommand(SOLVER->mkTrue()));
+        PARSER_STATE->preemptCommand(new CheckSatAssumingCommand(SOLVER->mkTrue()));
       } else {
-        seq->addCommand(new CheckSatCommand());
+        PARSER_STATE->preemptCommand(new CheckSatCommand());
       }
-      PARSER_STATE->preemptCommand(seq);
-      cmd = NULL;
+      cmd = nullptr;
     }
   ;
 
index f91bf5d0dc2dd21ccaccba37958b9fa91632078e..c785be5813641b0e3eec40882c4323a29d648bde 100644 (file)
@@ -230,19 +230,6 @@ void AstPrinter::toStreamCmdQuit(std::ostream& out) const
   out << "Quit()" << std::endl;
 }
 
-void AstPrinter::toStreamCmdCommandSequence(
-    std::ostream& out, const std::vector<cvc5::Command*>& sequence) const
-{
-  out << "cvc5::CommandSequence[" << endl;
-  for (cvc5::CommandSequence::const_iterator i = sequence.cbegin();
-       i != sequence.cend();
-       ++i)
-  {
-    out << *i << endl;
-  }
-  out << "]" << std::endl;
-}
-
 void AstPrinter::toStreamCmdDeclareFunction(std::ostream& out,
                                             const std::string& id,
                                             TypeNode type) const
index 577b38587aef1f49003e64c6e21ae0ebc2d603b6..8cf8bec47669a4cbed82bf7c4ca80f3f2f797264 100644 (file)
@@ -143,11 +143,6 @@ class AstPrinter : public cvc5::internal::Printer
   /** Print quit command */
   void toStreamCmdQuit(std::ostream& out) const override;
 
-  /** Print command sequence command */
-  void toStreamCmdCommandSequence(
-      std::ostream& out,
-      const std::vector<cvc5::Command*>& sequence) const override;
-
  private:
   void toStream(std::ostream& out,
                 TNode n,
index 70811aea9fd31e71cad5f2dbbcb1b412c24f9c41..7f7ad5c744eb3e69a713e506a7aebba6def6d705 100644 (file)
@@ -508,10 +508,4 @@ void Printer::toStreamCmdDeclareHeap(std::ostream& out,
   printUnknownCommand(out, "declare-heap");
 }
 
-void Printer::toStreamCmdCommandSequence(
-    std::ostream& out, const std::vector<cvc5::Command*>& sequence) const
-{
-  printUnknownCommand(out, "sequence");
-}
-
 }  // namespace cvc5::internal
index 7262c2a39d781aa6096b39134d30f889acef63da..ac71cb04b2558d7ba7318bf7109f67af170b88d0 100644 (file)
@@ -283,10 +283,6 @@ class Printer
                                       TypeNode locType,
                                       TypeNode dataType) const;
 
-  /** Print command sequence command */
-  virtual void toStreamCmdCommandSequence(
-      std::ostream& out, const std::vector<cvc5::Command*>& sequence) const;
-
  protected:
   /** Derived classes can construct, but no one else. */
   Printer() {}
index c495f56187b436fd0ed9c495605b792fe531e879..58cdd77392fb8e57d3104b9fd8cdae7ae034cda3 100644 (file)
@@ -1555,15 +1555,6 @@ void Smt2Printer::toStreamCmdQuit(std::ostream& out) const
   out << "(exit)" << std::endl;
 }
 
-void Smt2Printer::toStreamCmdCommandSequence(
-    std::ostream& out, const std::vector<cvc5::Command*>& sequence) const
-{
-  for (cvc5::Command* i : sequence)
-  {
-    out << *i;
-  }
-}
-
 void Smt2Printer::toStreamCmdDeclareFunction(std::ostream& out,
                                              const std::string& id,
                                              TypeNode type) const
index bfaa64b6172764c31a1fbcb1f5638caacbe8beac..1e19edf744e00b9684e568e3d8e13238ecf0bef5 100644 (file)
@@ -251,11 +251,6 @@ class Smt2Printer : public cvc5::internal::Printer
                               TypeNode locType,
                               TypeNode dataType) const override;
 
-  /** Print command sequence command */
-  void toStreamCmdCommandSequence(
-      std::ostream& out,
-      const std::vector<cvc5::Command*>& sequence) const override;
-
   /**
    * Get the string for a kind k, which returns how the kind k is printed in
    * the SMT-LIB format (with variant v).
index b2d7ccb8f22056a68c238b2266dad5a1aa247a80..5599eab399b24735aa5a666eee4f5d818c2df2dd 100644 (file)
@@ -799,90 +799,6 @@ void QuitCommand::toStream(std::ostream& out) const
   Printer::getPrinter(out)->toStreamCmdQuit(out);
 }
 
-/* -------------------------------------------------------------------------- */
-/* class CommandSequence                                                      */
-/* -------------------------------------------------------------------------- */
-
-CommandSequence::CommandSequence() : d_index(0) {}
-CommandSequence::~CommandSequence()
-{
-  for (unsigned i = d_index; i < d_commandSequence.size(); ++i)
-  {
-    delete d_commandSequence[i];
-  }
-}
-
-void CommandSequence::addCommand(Command* cmd)
-{
-  d_commandSequence.push_back(cmd);
-}
-
-void CommandSequence::clear() { d_commandSequence.clear(); }
-void CommandSequence::invoke(cvc5::Solver* solver, SymbolManager* sm)
-{
-  for (; d_index < d_commandSequence.size(); ++d_index)
-  {
-    d_commandSequence[d_index]->invoke(solver, sm);
-    if (!d_commandSequence[d_index]->ok())
-    {
-      // abort execution
-      d_commandStatus = d_commandSequence[d_index]->getCommandStatus();
-      return;
-    }
-    delete d_commandSequence[d_index];
-  }
-
-  AlwaysAssert(d_commandStatus == NULL);
-  d_commandStatus = CommandSuccess::instance();
-}
-
-void CommandSequence::invoke(cvc5::Solver* solver,
-                             SymbolManager* sm,
-                             std::ostream& out)
-{
-  for (; d_index < d_commandSequence.size(); ++d_index)
-  {
-    d_commandSequence[d_index]->invoke(solver, sm, out);
-    if (!d_commandSequence[d_index]->ok())
-    {
-      // abort execution
-      d_commandStatus = d_commandSequence[d_index]->getCommandStatus();
-      return;
-    }
-    delete d_commandSequence[d_index];
-  }
-
-  AlwaysAssert(d_commandStatus == NULL);
-  d_commandStatus = CommandSuccess::instance();
-}
-
-CommandSequence::const_iterator CommandSequence::begin() const
-{
-  return d_commandSequence.begin();
-}
-
-CommandSequence::const_iterator CommandSequence::end() const
-{
-  return d_commandSequence.end();
-}
-
-CommandSequence::iterator CommandSequence::begin()
-{
-  return d_commandSequence.begin();
-}
-
-CommandSequence::iterator CommandSequence::end()
-{
-  return d_commandSequence.end();
-}
-
-std::string CommandSequence::getCommandName() const { return "sequence"; }
-
-void CommandSequence::toStream(std::ostream& out) const
-{
-  Printer::getPrinter(out)->toStreamCmdCommandSequence(out, d_commandSequence);
-}
-
 /* -------------------------------------------------------------------------- */
 /* class DeclarationDefinitionCommand                                         */
 /* -------------------------------------------------------------------------- */
index ba6e30f1b1436c614888fe4d29a785204965dc98..534208205d4f45bbb730af396b085271e1ae8165 100644 (file)
@@ -1216,39 +1216,6 @@ class CVC5_EXPORT QuitCommand : public Command
   void toStream(std::ostream& out) const override;
 }; /* class QuitCommand */
 
-class CVC5_EXPORT CommandSequence : public Command
-{
- protected:
-  /** All the commands to be executed (in sequence) */
-  std::vector<Command*> d_commandSequence;
-  /** Next command to be executed */
-  unsigned int d_index;
-
- public:
-  CommandSequence();
-  ~CommandSequence();
-
-  void addCommand(Command* cmd);
-  void clear();
-
-  void invoke(cvc5::Solver* solver, SymbolManager* sm) override;
-  void invoke(cvc5::Solver* solver,
-              SymbolManager* sm,
-              std::ostream& out) override;
-
-  typedef std::vector<Command*>::iterator iterator;
-  typedef std::vector<Command*>::const_iterator const_iterator;
-
-  const_iterator begin() const;
-  const_iterator end() const;
-
-  iterator begin();
-  iterator end();
-
-  std::string getCommandName() const override;
-  void toStream(std::ostream& out) const override;
-}; /* class CommandSequence */
-
 }  // namespace cvc5
 
 #endif /* CVC5__COMMAND_H */
index 134c5fcdfe5ee5a9e4248aaf792562f3a5bbfef8..d96a02a77de2a344231d3096d81f8d4baa4080f9 100644 (file)
@@ -13,6 +13,7 @@
  * Black box testing of cvc5::InteractiveShell.
  */
 
+#include <memory>
 #include <sstream>
 #include <vector>
 
@@ -65,12 +66,15 @@ class TestMainBlackInteractiveShell : public TestInternal
                      uint32_t minCommands,
                      uint32_t maxCommands)
   {
-    Command* cmd;
     uint32_t n = 0;
-    while (n <= maxCommands && (cmd = shell.readCommand()) != NULL)
+    while (n <= maxCommands)
     {
-      ++n;
-      delete cmd;
+      std::optional<InteractiveShell::CmdSeq> cmds = shell.readCommand();
+      if (!cmds)
+      {
+        break;
+      }
+      n += cmds->size();
     }
     ASSERT_LE(n, maxCommands);
     ASSERT_GE(n, minCommands);
@@ -100,9 +104,7 @@ TEST_F(TestMainBlackInteractiveShell, def_use1)
 {
   InteractiveShell shell(d_solver.get(), d_symman.get(), *d_sin, *d_sout);
   *d_sin << "(declare-const x Real) (assert (> x 0))\n" << std::flush;
-  /* readCommand may return a sequence, so we can't say for sure
-     whether it will return 1 or 2... */
-  countCommands(shell, 1, 2);
+  countCommands(shell, 2, 2);
 }
 
 TEST_F(TestMainBlackInteractiveShell, def_use2)
@@ -110,10 +112,9 @@ TEST_F(TestMainBlackInteractiveShell, def_use2)
   InteractiveShell shell(d_solver.get(), d_symman.get(), *d_sin, *d_sout);
   /* readCommand may return a sequence, see above. */
   *d_sin << "(declare-const x Real)\n" << std::flush;
-  Command* tmp = shell.readCommand();
+  shell.readCommand();
   *d_sin << "(assert (> x 0))\n" << std::flush;
   countCommands(shell, 1, 1);
-  delete tmp;
 }
 
 TEST_F(TestMainBlackInteractiveShell, empty_line)