From ac35ef610143695ec5e1e9b74a8fdff4803b6ac0 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= <sebastien@dynare.org>
Date: Tue, 14 Nov 2023 14:28:30 +0100
Subject: [PATCH] =?UTF-8?q?Error=20out=20if=20an=20=E2=80=9Cinitval?=
 =?UTF-8?q?=E2=80=9D=20block=20appears=20after=20an=20=E2=80=9Cendval?=
 =?UTF-8?q?=E2=80=9D=20block=20(without=20the=20=E2=80=9Clearnt=5Fin=3D?=
 =?UTF-8?q?=E2=80=A6=E2=80=9D=20option)?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Closes: #104
---
 src/NumericalInitialization.cc | 8 ++++++++
 src/Statement.hh               | 2 ++
 2 files changed, 10 insertions(+)

diff --git a/src/NumericalInitialization.cc b/src/NumericalInitialization.cc
index bd498c47..84f48893 100644
--- a/src/NumericalInitialization.cc
+++ b/src/NumericalInitialization.cc
@@ -187,6 +187,12 @@ void
 InitValStatement::checkPass([[maybe_unused]] ModFileStructure &mod_file_struct,
                             [[maybe_unused]] WarningConsolidation &warnings)
 {
+  if (mod_file_struct.endval_present)
+    {
+      cerr << "ERROR: an 'initval' block cannot appear after an 'endval' block" << endl; // See #104
+      exit(EXIT_FAILURE);
+    }
+
   set<int> exogs = getUninitializedVariables(SymbolType::exogenous);
   set<int> endogs = getUninitializedVariables(SymbolType::endogenous);
 
@@ -253,6 +259,8 @@ void
 EndValStatement::checkPass([[maybe_unused]] ModFileStructure &mod_file_struct,
                            [[maybe_unused]] WarningConsolidation &warnings)
 {
+  mod_file_struct.endval_present = true;
+
   set<int> exogs = getUninitializedVariables(SymbolType::exogenous);
   set<int> endogs = getUninitializedVariables(SymbolType::endogenous);
 
diff --git a/src/Statement.hh b/src/Statement.hh
index 6a8bdc7f..3fba9395 100644
--- a/src/Statement.hh
+++ b/src/Statement.hh
@@ -157,6 +157,8 @@ struct ModFileStructure
   /* Lists symbol IDs of parameters that appear in a “planner_discount” option.
      See dynare#1173 for more details. */
   set<int> parameters_in_planner_discount;
+  // Whether an endval block (without the learnt_it=… option) appears
+  bool endval_present{false};
   // Whether a shocks(surprise) block appears
   bool shocks_surprise_present{false};
   // Whether a shocks(learnt_in=…) block appears
-- 
GitLab