Directives.cc 8.74 KB
Newer Older
Houtan Bastani's avatar
Houtan Bastani committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/*
 * Copyright © 2019 Dynare Team
 *
 * This file is part of Dynare.
 *
 * Dynare is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Dynare is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "Directives.hh"
21
22
23
#include "Driver.hh"

#include <fstream>
Houtan Bastani's avatar
Houtan Bastani committed
24
25
26
27

using namespace macro;

void
28
Eval::interpret(ostream &output, bool no_line_macro, vector<filesystem::path> &paths)
Houtan Bastani's avatar
Houtan Bastani committed
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
{
  try
    {
      output << expr->eval()->to_string();
    }
  catch (StackTrace &ex)
    {
      ex.push("Evaluation", location);
      error(ex);
    }
  catch (exception &e)
    {
      error(StackTrace("Evaluation", e.what(), location));
    }
}

45
void
46
Include::interpret(ostream &output, bool no_line_macro, vector<filesystem::path> &paths)
Houtan Bastani's avatar
Houtan Bastani committed
47
{
48
  using namespace filesystem;
Houtan Bastani's avatar
Houtan Bastani committed
49
50
51
52
53
  try
    {
      StringPtr msp = dynamic_pointer_cast<String>(expr->eval());
      if (!msp)
        throw StackTrace("File name does not evaluate to a string");
54
      path filename = msp->to_string();
55
56
57
      ifstream incfile(filename, ios::binary);
      if (incfile.fail())
        {
Houtan Bastani's avatar
Houtan Bastani committed
58
          for (auto & file : paths)
59
            {
Houtan Bastani's avatar
Houtan Bastani committed
60
              incfile = ifstream(file / filename, ios::binary);
61
62
63
64
              if (incfile.good())
                break;
            }
          if (incfile.fail())
65
66
67
68
69
70
71
72
            {
              ostringstream errmsg;
              errmsg << "   * " << current_path().string() << endl;
              for (auto & dir : paths)
                errmsg << "   * " << absolute(dir).string() << endl;
              error(StackTrace("@#includepath", "Could not open " + filename.string() +
                               ". The following directories were searched:\n" + errmsg.str(), location));
            }
73
        }
74
      Driver m(env, no_line_macro);
75
76
77
78
      // Calling `string()` method on filename and filename.stem() because of bug in
      // MinGW 8.3.0 that ignores implicit conversion to string from filename::path.
      // Test if bug exists when version of MinGW is upgraded on Debian runners
      m.parse(filename.string(), filename.stem().string(), incfile, output, false, vector<pair<string, string>>{}, paths);
Houtan Bastani's avatar
Houtan Bastani committed
79
80
81
82
83
84
85
86
87
88
89
90
    }
  catch (StackTrace &ex)
    {
      ex.push("@#include", location);
      error(ex);
    }
  catch (exception &e)
    {
      error(StackTrace("@#include", e.what(), location));
    }
}

91
void
92
IncludePath::interpret(ostream &output, bool no_line_macro, vector<filesystem::path> &paths)
Houtan Bastani's avatar
Houtan Bastani committed
93
{
94
  using namespace filesystem;
Houtan Bastani's avatar
Houtan Bastani committed
95
96
97
98
99
  try
    {
      StringPtr msp = dynamic_pointer_cast<String>(expr->eval());
      if (!msp)
        throw StackTrace("File name does not evaluate to a string");
100
101
102
103
104
      path ip = static_cast<string>(*msp);
      if (!is_directory(ip))
        throw StackTrace(ip.string() + " does not evaluate to a valid directory");
      if (!exists(ip))
        warning(StackTrace("@#includepath", ip.string() + " does not exist", location));
105
      paths.emplace_back(ip);
Houtan Bastani's avatar
Houtan Bastani committed
106
107
108
109
110
111
112
113
114
115
116
117
118
    }
  catch (StackTrace &ex)
    {
      ex.push("@#includepath", location);
      error(ex);
    }
  catch (exception &e)
    {
      error(StackTrace("@#includepath", e.what(), location));
    }
}

void
119
Define::interpret(ostream &output, bool no_line_macro, vector<filesystem::path> &paths)
Houtan Bastani's avatar
Houtan Bastani committed
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
{
  try
    {
      if (var)
        env.define(var, value);
      else if (func)
        env.define(func, value);
      else
        throw StackTrace("LHS of can be either a variable or a function");
    }
  catch (StackTrace &ex)
    {
      ex.push("@#define", location);
      error(ex);
    }
  catch (exception &e)
    {
      error(StackTrace("@#define", e.what(), location));
    }
}

void
142
Echo::interpret(ostream &output, bool no_line_macro, vector<filesystem::path> &paths)
Houtan Bastani's avatar
Houtan Bastani committed
143
144
145
146
147
148
149
150
151
152
153
154
155
156
{
  try
    {
      cout << "@#echo (" << getLocation() << "): " << expr->eval()->to_string() << endl;
    }
  catch (StackTrace &ex)
    {
      ex.push("@#echo", location);
      error(ex);
    }
  catch (exception &e)
    {
      error(StackTrace("@#echo", e.what(), location));
    }
157
  printEndLineInfo(output, no_line_macro);
Houtan Bastani's avatar
Houtan Bastani committed
158
159
160
}

void
161
Error::interpret(ostream &output, bool no_line_macro, vector<filesystem::path> &paths)
Houtan Bastani's avatar
Houtan Bastani committed
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
{
  try
    {
      throw StackTrace(expr->eval()->to_string());
    }
  catch (StackTrace &ex)
    {
      ex.push("@#error", location);
      error(ex);
    }
  catch (exception &e)
    {
      error(StackTrace("@#error", e.what(), location));
    }
}

178
void
179
EchoMacroVars::interpret(ostream &output, bool no_line_macro, vector<filesystem::path> &paths)
180
181
{
  if (save)
182
    env.print(output, vars, location.begin.line, true);
183
  else
184
    env.print(cout, vars);
185
186
187
  printEndLineInfo(output, no_line_macro);
}

Houtan Bastani's avatar
Houtan Bastani committed
188
void
189
For::interpret(ostream &output, bool no_line_macro, vector<filesystem::path> &paths)
Houtan Bastani's avatar
Houtan Bastani committed
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
{
  ArrayPtr ap;
  try
    {
      ap = dynamic_pointer_cast<Array>(index_vals->eval());
      if (!ap)
        throw StackTrace("The index must loop through an array");
    }
  catch (StackTrace &ex)
    {
      ex.push("@#for", location);
      error(ex);
    }
  catch (exception &e)
    {
      error(StackTrace("@#for", e.what(), location));
    }

  for (size_t i = 0; i < ap->size(); i++)
    {
      if (index_vec.size() == 1)
        env.define(index_vec.at(0), ap->at(i));
      else
        {
          BaseTypePtr btp = dynamic_pointer_cast<BaseType>(ap->at(i));
          if (!btp)
            error(StackTrace("@#for", "Unexpected error encountered in for loop", location));

          if (btp->getType() == codes::BaseType::Tuple)
            {
              TuplePtr mtp = dynamic_pointer_cast<Tuple>(btp);
              if (index_vec.size() != mtp->size())
                error(StackTrace("@#for", "Encountered tuple of size " + to_string(mtp->size())
                                 + " but only have " + to_string(index_vec.size()) + " index variables", location));
              else
                for (size_t j = 0; j < index_vec.size(); j++)
                  env.define(index_vec.at(j), mtp->at(j));
            }
        }

      bool printLine = true;
      for (auto & statement : statements)
        {
          if (printLine)
            {
              statement->printLineInfo(output, no_line_macro);
              printLine = false;
            }
238
          statement->interpret(output, no_line_macro, paths);
Houtan Bastani's avatar
Houtan Bastani committed
239
240
241
242
243
244
        }
    }
  printEndLineInfo(output, no_line_macro);
}

void
245
If::interpret(ostream &output, bool no_line_macro, vector<filesystem::path> &paths)
Houtan Bastani's avatar
Houtan Bastani committed
246
{
247
  for (const auto & [expr, body] : expr_and_body)
248
249
    try
      {
250
        auto tmp = expr->eval();
251
252
        RealPtr dp = dynamic_pointer_cast<Real>(tmp);
        BoolPtr bp = dynamic_pointer_cast<Bool>(tmp);
253
254
255
256
257
        if (!bp && !dp)
          error(StackTrace("@#if",
                           "The condition must evaluate to a boolean or a double", location));
        if ((bp && *bp) || (dp && *dp))
          {
258
            interpretBody(body, output, no_line_macro, paths);
259
260
261
262
263
264
265
266
267
268
269
270
            break;
          }
      }
    catch (StackTrace &ex)
      {
        ex.push("@#if", location);
        error(ex);
      }
    catch (exception &e)
      {
        error(StackTrace("@#if", e.what(), location));
      }
Houtan Bastani's avatar
Houtan Bastani committed
271
272
273
274
  printEndLineInfo(output, no_line_macro);
}

void
275
If::interpretBody(const vector<DirectivePtr> &body, ostream &output, bool no_line_macro, vector<filesystem::path> &paths)
Houtan Bastani's avatar
Houtan Bastani committed
276
277
{
  bool printLine = !no_line_macro;
278
  for (auto & statement : body)
Houtan Bastani's avatar
Houtan Bastani committed
279
280
281
282
283
284
    {
      if (printLine)
        {
          statement->printLineInfo(output, no_line_macro);
          printLine = false;
        }
285
      statement->interpret(output, no_line_macro, paths);
Houtan Bastani's avatar
Houtan Bastani committed
286
287
288
289
    }
}

void
290
Ifdef::interpret(ostream &output, bool no_line_macro, vector<filesystem::path> &paths)
Houtan Bastani's avatar
Houtan Bastani committed
291
{
292
293
294
  for (const auto & [expr, body] : expr_and_body)
    if (VariablePtr vp = dynamic_pointer_cast<Variable>(expr);
        dynamic_pointer_cast<BaseType>(expr)
295
296
        || (vp && env.isVariableDefined(vp->getName())))
      {
297
        interpretBody(body, output, no_line_macro, paths);
298
299
        break;
      }
Houtan Bastani's avatar
Houtan Bastani committed
300
301
302
303
  printEndLineInfo(output, no_line_macro);
}

void
304
Ifndef::interpret(ostream &output, bool no_line_macro, vector<filesystem::path> &paths)
Houtan Bastani's avatar
Houtan Bastani committed
305
{
306
307
308
  for (const auto & [expr, body] : expr_and_body)
    if (VariablePtr vp = dynamic_pointer_cast<Variable>(expr);
        !(dynamic_pointer_cast<BaseType>(expr)
309
310
          || (vp && env.isVariableDefined(vp->getName()))))
      {
311
        interpretBody(body, output, no_line_macro, paths);
312
313
        break;
      }
Houtan Bastani's avatar
Houtan Bastani committed
314
315
  printEndLineInfo(output, no_line_macro);
}