From 8da663a1104f3329937acc9502c2e07a2dd36908 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= <sebastien@dynare.org>
Date: Tue, 12 Jul 2022 12:19:17 +0200
Subject: [PATCH] Further factorization in methods for writing static and
 dynamic files

---
 src/DynamicModel.cc |  8 --------
 src/ModelTree.hh    | 11 +++++++++++
 src/StaticModel.cc  |  8 --------
 3 files changed, 11 insertions(+), 16 deletions(-)

diff --git a/src/DynamicModel.cc b/src/DynamicModel.cc
index 52d9af3b..a3dd2d72 100644
--- a/src/DynamicModel.cc
+++ b/src/DynamicModel.cc
@@ -1268,14 +1268,6 @@ DynamicModel::writeDynamicMFile(const string &basename) const
 {
   auto [d_output, tt_output] = writeModelFileHelper<ExprNodeOutputType::matlabDynamicModel>();
 
-  // Check that we don't have more than 32 nested parenthesis because Matlab does not suppor this. See Issue #1201
-  map<string, string> tmp_paren_vars;
-  bool message_printed{false};
-  for (auto &it : tt_output)
-    fixNestedParenthesis(it, tmp_paren_vars, message_printed);
-  for (auto &it : d_output)
-    fixNestedParenthesis(it, tmp_paren_vars, message_printed);
-
   ostringstream init_output, end_output;
   init_output << "residual = zeros(" << equations.size() << ", 1);";
   writeDynamicModelHelper(basename, "dynamic_resid", "residual",
diff --git a/src/ModelTree.hh b/src/ModelTree.hh
index e0456fbd..8ff7d07c 100644
--- a/src/ModelTree.hh
+++ b/src/ModelTree.hh
@@ -643,6 +643,17 @@ ModelTree::writeModelFileHelper() const
           d_output[i] << i_output.str() << j_output.str() << v_output.str();
       }
 
+  if constexpr(isMatlabOutput(output_type))
+    {
+      // Check that we don't have more than 32 nested parenthesis because MATLAB does not suppor this. See Issue #1201
+      map<string, string> tmp_paren_vars;
+      bool message_printed {false};
+      for (auto &it : tt_output)
+        fixNestedParenthesis(it, tmp_paren_vars, message_printed);
+      for (auto &it : d_output)
+        fixNestedParenthesis(it, tmp_paren_vars, message_printed);
+    }
+
   return { move(d_output), move(tt_output) };
 }
 
diff --git a/src/StaticModel.cc b/src/StaticModel.cc
index d769bcc4..9ed61063 100644
--- a/src/StaticModel.cc
+++ b/src/StaticModel.cc
@@ -941,14 +941,6 @@ StaticModel::writeStaticMFile(const string &basename) const
 {
   auto [d_output, tt_output] = writeModelFileHelper<ExprNodeOutputType::matlabStaticModel>();
 
-  // Check that we don't have more than 32 nested parenthesis because Matlab does not suppor this. See Issue #1201
-  map<string, string> tmp_paren_vars;
-  bool message_printed{false};
-  for (auto &it : tt_output)
-    fixNestedParenthesis(it, tmp_paren_vars, message_printed);
-  for (auto &it : d_output)
-    fixNestedParenthesis(it, tmp_paren_vars, message_printed);
-
   ostringstream init_output, end_output;
   init_output << "residual = zeros(" << equations.size() << ", 1);";
   end_output << "if ~isreal(residual)" << endl
-- 
GitLab