Directives.cc 8.34 KB
Newer Older
Houtan Bastani's avatar
Houtan Bastani committed
1
/*
2
 * Copyright © 2019-2020 Dynare Team
Houtan Bastani's avatar
Houtan Bastani committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 *
 * 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, 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, 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())
        {
Sébastien Villemot's avatar
Sébastien Villemot committed
58
          for (const auto &dir : paths)
59
            {
60
              incfile = ifstream(dir / filename, ios::binary);
61
62
63
64
              if (incfile.good())
                break;
            }
          if (incfile.fail())
65
66
67
            {
              ostringstream errmsg;
              errmsg << "   * " << current_path().string() << endl;
Sébastien Villemot's avatar
Sébastien Villemot committed
68
              for (const auto &dir : paths)
69
                errmsg << "   * " << absolute(dir).string() << endl;
Sébastien Villemot's avatar
Sébastien Villemot committed
70
71
              error(StackTrace("@#includepath", "Could not open " + filename.string()
                               +". The following directories were searched:\n" + errmsg.str(), location));
72
            }
73
        }
74
      Driver m(env);
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
    }
  catch (StackTrace &ex)
    {
      ex.push("@#include", location);
      error(ex);
    }
  catch (exception &e)
    {
      error(StackTrace("@#include", e.what(), location));
    }
89
  printLineInfo(output);
Houtan Bastani's avatar
Houtan Bastani committed
90
91
}

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

void
120
Define::interpret(ostream &output, vector<filesystem::path> &paths)
Houtan Bastani's avatar
Houtan Bastani committed
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
{
  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
143
Echo::interpret(ostream &output, vector<filesystem::path> &paths)
Houtan Bastani's avatar
Houtan Bastani committed
144
145
146
147
148
149
150
151
152
153
154
155
156
157
{
  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));
    }
158
  printEndLineInfo(output);
Houtan Bastani's avatar
Houtan Bastani committed
159
160
161
}

void
162
Error::interpret(ostream &output, vector<filesystem::path> &paths)
Houtan Bastani's avatar
Houtan Bastani committed
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
{
  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));
    }
}

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

Houtan Bastani's avatar
Houtan Bastani committed
189
void
190
For::interpret(ostream &output, vector<filesystem::path> &paths)
Houtan Bastani's avatar
Houtan Bastani committed
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
{
  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;
Sébastien Villemot's avatar
Sébastien Villemot committed
232
      for (const auto &statement : statements)
Houtan Bastani's avatar
Houtan Bastani committed
233
234
235
        {
          if (printLine)
            {
236
              statement->printLineInfo(output);
Houtan Bastani's avatar
Houtan Bastani committed
237
238
              printLine = false;
            }
239
          statement->interpret(output, paths);
Houtan Bastani's avatar
Houtan Bastani committed
240
241
        }
    }
242
  printEndLineInfo(output);
Houtan Bastani's avatar
Houtan Bastani committed
243
244
245
}

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

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

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

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