From b08a3979831ace0e1fe66941f2e378c3b2b5067c Mon Sep 17 00:00:00 2001
From: Houtan Bastani <houtan@dynare.org>
Date: Thu, 3 Nov 2016 12:56:54 +0100
Subject: [PATCH] var_forecast: move var declaration from options_ to M_,
 simplify structure in which it is stored

---
 ComputingTasks.cc | 27 ++++++++++++++-------------
 ComputingTasks.hh | 12 +++++++-----
 DynareBison.yy    |  6 +++---
 ModFile.cc        |  3 ---
 ParsingDriver.cc  |  6 +++++-
 Statement.hh      |  2 --
 6 files changed, 29 insertions(+), 27 deletions(-)

diff --git a/ComputingTasks.cc b/ComputingTasks.cc
index 81ce4cf9..c7a29788 100644
--- a/ComputingTasks.cc
+++ b/ComputingTasks.cc
@@ -171,19 +171,20 @@ PriorPosteriorFunctionStatement::writeOutput(ostream &output, const string &base
          << "'" << type << "');" << endl;
 }
 
-VARStatement::VARStatement(const SymbolList &symbol_list_arg,
-                           const OptionsList &options_list_arg,
-                           const SymbolTable &symbol_table_arg) :
+VarModelStatement::VarModelStatement(const SymbolList &symbol_list_arg,
+                                     const OptionsList &options_list_arg,
+                                     const string &name_arg,
+                                     const SymbolTable &symbol_table_arg) :
   symbol_list(symbol_list_arg),
   options_list(options_list_arg),
+  name(name_arg),
   symbol_table(symbol_table_arg)
 {
 }
 
 void
-VARStatement::checkPass(ModFileStructure &mod_file_struct, WarningConsolidation &warnings)
+VarModelStatement::checkPass(ModFileStructure &mod_file_struct, WarningConsolidation &warnings)
 {
-  mod_file_struct.var_model_present = true;
   vector<string> symbols = symbol_list.get_symbols();
   for (vector<string>::const_iterator it = symbols.begin(); it != symbols.end(); it++)
     if (symbol_table.getType(*it) != eEndogenous)
@@ -192,27 +193,27 @@ VARStatement::checkPass(ModFileStructure &mod_file_struct, WarningConsolidation
         exit(EXIT_FAILURE);
       }
 
-  OptionsList::num_options_t::const_iterator it = options_list.num_options.find("var(varidx).order");
+  OptionsList::num_options_t::const_iterator it = options_list.num_options.find("var.order");
   if (it == options_list.num_options.end())
     {
-      cerr << "ERROR: You must provide the order option to the var command." << endl;
+      cerr << "ERROR: You must provide the order option to the var_model statement." << endl;
       exit(EXIT_FAILURE);
     }
 
-  OptionsList::string_options_t::const_iterator it1 = options_list.string_options.find("var(varidx).name");
-  if (it1 == options_list.string_options.end())
+  if (name.empty())
     {
-      cerr << "ERROR: You must provide the model_name option to the var command." << endl;
+      cerr << "ERROR: You must provide the model_name option to the var_model statement." << endl;
       exit(EXIT_FAILURE);
     }
 }
 
 void
-VARStatement::writeOutput(ostream &output, const string &basename, bool minimal_workspace) const
+VarModelStatement::writeOutput(ostream &output, const string &basename, bool minimal_workspace) const
 {
   options_list.writeOutput(output);
-  symbol_list.writeOutput("options_.var(varidx).var_list_", output);
-  output << "varidx = varidx + 1;" << endl;
+  symbol_list.writeOutput("options_.var.var_list_", output);
+  output << "M_.var." << name << " = options_.var;" << endl
+         << "clear options_.var;" << endl;
 }
 
 StochSimulStatement::StochSimulStatement(const SymbolList &symbol_list_arg,
diff --git a/ComputingTasks.hh b/ComputingTasks.hh
index d9a0c536..bba0efbf 100644
--- a/ComputingTasks.hh
+++ b/ComputingTasks.hh
@@ -110,16 +110,18 @@ public:
   virtual void writeOutput(ostream &output, const string &basename, bool minimal_workspace) const;
 };
 
-class VARStatement : public Statement
+class VarModelStatement : public Statement
 {
 private:
   const SymbolList symbol_list;
   const OptionsList options_list;
-    const SymbolTable &symbol_table;
+  const string &name;
+  const SymbolTable &symbol_table;
 public:
-  VARStatement(const SymbolList &symbol_list_arg,
-               const OptionsList &options_list_arg,
-               const SymbolTable &symbol_table_arg);
+  VarModelStatement(const SymbolList &symbol_list_arg,
+                    const OptionsList &options_list_arg,
+                    const string &name_arg,
+                    const SymbolTable &symbol_table_arg);
   virtual void checkPass(ModFileStructure &mod_file_struct, WarningConsolidation &warnings);
   virtual void writeOutput(ostream &output, const string &basename, bool minimal_workspace) const;
 };
diff --git a/DynareBison.yy b/DynareBison.yy
index 3e23212b..224a1965 100644
--- a/DynareBison.yy
+++ b/DynareBison.yy
@@ -347,7 +347,7 @@ var : VAR var_list ';'
       { driver.end_nonstationary_var(true, $6); }
     ;
 
-var_model : VAR '(' var_model_options_list ')' symbol_list ';' { driver.var_model(); } ;
+var_model : VAR_MODEL '(' var_model_options_list ')' symbol_list ';' { driver.var_model(); } ;
 
 var_model_options_list : var_model_options_list COMMA var_model_options
                          | var_model_options
@@ -2827,8 +2827,8 @@ o_simul_seed : SIMUL_SEED EQUAL INT_NUMBER { driver.error("'simul_seed' option i
 o_qz_criterium : QZ_CRITERIUM EQUAL non_negative_number { driver.option_num("qz_criterium", $3); };
 o_qz_zero_threshold : QZ_ZERO_THRESHOLD EQUAL non_negative_number { driver.option_num("qz_zero_threshold", $3); };
 o_file : FILE EQUAL filename { driver.option_str("file", $3); };
-o_var_name : MODEL_NAME EQUAL symbol { driver.option_str("var(varidx).name", $3); };
-o_var_order : ORDER EQUAL INT_NUMBER { driver.option_num("var(varidx).order", $3); };
+o_var_name : MODEL_NAME EQUAL symbol { driver.option_str("var.model_name", $3); };
+o_var_order : ORDER EQUAL INT_NUMBER { driver.option_num("var.order", $3); };
 o_series : SERIES EQUAL symbol { driver.option_str("series", $3); };
 o_datafile : DATAFILE EQUAL filename { driver.option_str("datafile", $3); };
 o_dirname : DIRNAME EQUAL filename { driver.option_str("dirname", $3); };
diff --git a/ModFile.cc b/ModFile.cc
index 1b114d1e..5719d019 100644
--- a/ModFile.cc
+++ b/ModFile.cc
@@ -808,9 +808,6 @@ ModFile::writeOutputFiles(const string &basename, bool clear_all, bool clear_glo
         static_model.writeOutput(mOutputFile, block);
     }
 
-  if (mod_file_struct.var_model_present)
-    mOutputFile << "varidx = 1;" << endl;
-
   // Print statements
   for (vector<Statement *>::const_iterator it = statements.begin();
        it != statements.end(); it++)
diff --git a/ParsingDriver.cc b/ParsingDriver.cc
index 4fdf1396..d90f0933 100644
--- a/ParsingDriver.cc
+++ b/ParsingDriver.cc
@@ -1282,7 +1282,11 @@ ParsingDriver::stoch_simul()
 void
 ParsingDriver::var_model()
 {
-  mod_file->addStatement(new VARStatement(symbol_list, options_list, mod_file->symbol_table));
+  OptionsList::string_options_t::const_iterator it = options_list.string_options.find("var.model_name");
+  if (it == options_list.string_options.end())
+    error("You must pass the model_name option to the var_model statement.");
+  const string *name = new string(it->second);
+  mod_file->addStatement(new VarModelStatement(symbol_list, options_list, *name, mod_file->symbol_table));
   symbol_list.clear();
   options_list.clear();
 }
diff --git a/Statement.hh b/Statement.hh
index 1676ff31..ee7846ff 100644
--- a/Statement.hh
+++ b/Statement.hh
@@ -122,8 +122,6 @@ public:
   int orig_eq_nbr;
    //! Stores the number of equations added to the Ramsey model
   int ramsey_eq_nbr;
-  //! Whether a VAR statement is present
-  bool var_model_present;
 };
 
 class Statement
-- 
GitLab