Directives.cc 8.53 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())
        {
58
          for (auto file : paths)
59
            {
60
61
              file /= filename;
              incfile = ifstream(file, ios::binary);
62
63
64
65
              if (incfile.good())
                break;
            }
          if (incfile.fail())
66
67
68
69
70
71
72
73
            {
              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));
            }
74
        }
75
76
      Driver m(env, no_line_macro);
      m.parse(filename, filename.stem(), incfile, output, false, vector<pair<string, string>>{}, paths);
Houtan Bastani's avatar
Houtan Bastani committed
77
78
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
void
90
IncludePath::interpret(ostream &output, bool no_line_macro, vector<filesystem::path> &paths)
Houtan Bastani's avatar
Houtan Bastani committed
91
{
92
  using namespace filesystem;
Houtan Bastani's avatar
Houtan Bastani committed
93
94
95
96
97
  try
    {
      StringPtr msp = dynamic_pointer_cast<String>(expr->eval());
      if (!msp)
        throw StackTrace("File name does not evaluate to a string");
98
99
100
101
102
      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));
103
      paths.emplace_back(*msp);
Houtan Bastani's avatar
Houtan Bastani committed
104
105
106
107
108
109
110
111
112
113
114
115
116
    }
  catch (StackTrace &ex)
    {
      ex.push("@#includepath", location);
      error(ex);
    }
  catch (exception &e)
    {
      error(StackTrace("@#includepath", e.what(), location));
    }
}

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

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

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

Houtan Bastani's avatar
Houtan Bastani committed
186
void
187
For::interpret(ostream &output, bool no_line_macro, vector<filesystem::path> &paths)
Houtan Bastani's avatar
Houtan Bastani committed
188
189
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
{
  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;
            }
236
          statement->interpret(output, no_line_macro, paths);
Houtan Bastani's avatar
Houtan Bastani committed
237
238
239
240
241
242
        }
    }
  printEndLineInfo(output, no_line_macro);
}

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

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

void
288
Ifdef::interpret(ostream &output, bool no_line_macro, vector<filesystem::path> &paths)
Houtan Bastani's avatar
Houtan Bastani committed
289
{
290
  for (auto & it : expr_and_body)
Houtan Bastani's avatar
Houtan Bastani committed
291
    {
292
293
294
      VariablePtr vp = dynamic_pointer_cast<Variable>(it.first);
      if (dynamic_pointer_cast<BaseType>(it.first)
          || (vp && env.isVariableDefined(vp->getName())))
Houtan Bastani's avatar
Houtan Bastani committed
295
        {
296
          interpretBody(it.second, output, no_line_macro, paths);
297
          break;
Houtan Bastani's avatar
Houtan Bastani committed
298
299
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
309
310
311
  for (auto & it : expr_and_body)
    {
      VariablePtr vp = dynamic_pointer_cast<Variable>(it.first);
      if (!(dynamic_pointer_cast<BaseType>(it.first)
            || (vp && env.isVariableDefined(vp->getName()))))
        {
312
          interpretBody(it.second, output, no_line_macro, paths);
313
314
315
          break;
        }
    }
Houtan Bastani's avatar
Houtan Bastani committed
316
317
  printEndLineInfo(output, no_line_macro);
}