Directives.cc 7.9 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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44

using namespace macro;

void
Eval::interpret(ostream &output, bool no_line_macro)
{
  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
46
void
Include::interpret(ostream &output, bool no_line_macro)
Houtan Bastani's avatar
Houtan Bastani committed
47
{
48
49
50
51
52
#ifdef _WIN32
  string FILESEP = "\\";
#else
  string FILESEP = "/";
#endif
Houtan Bastani's avatar
Houtan Bastani committed
53
54
55
56
57
  try
    {
      StringPtr msp = dynamic_pointer_cast<String>(expr->eval());
      if (!msp)
        throw StackTrace("File name does not evaluate to a string");
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
      string filename = msp->to_string();
      ifstream incfile(filename, ios::binary);
      if (incfile.fail())
        {
          ostringstream dirs;
          dirs << "." << FILESEP << endl;
          for (const auto & path : paths)
            {
              string testfile = path + FILESEP + filename;
              incfile = ifstream(testfile, ios::binary);
              if (incfile.good())
                break;
              dirs << path << endl;
            }
          if (incfile.fail())
            error(StackTrace("@#includepath", "Could not open " + filename +
                             ". The following directories were searched:\n" + dirs.str(), location));
        }

      string basename = filename;
      size_t pos = basename.find_last_of('.');
      if (pos != string::npos)
        basename.erase(pos);

      Driver m(env, paths, no_line_macro);
      m.parse(filename, basename, incfile, output, false, vector<pair<string, string>>{}, paths);
Houtan Bastani's avatar
Houtan Bastani committed
84
85
86
87
88
89
90
91
92
93
94
95
    }
  catch (StackTrace &ex)
    {
      ex.push("@#include", location);
      error(ex);
    }
  catch (exception &e)
    {
      error(StackTrace("@#include", e.what(), location));
    }
}

96
97
void
IncludePath::interpret(ostream &output, bool no_line_macro)
Houtan Bastani's avatar
Houtan Bastani committed
98
99
100
101
102
103
{
  try
    {
      StringPtr msp = dynamic_pointer_cast<String>(expr->eval());
      if (!msp)
        throw StackTrace("File name does not evaluate to a string");
104
      paths.emplace_back(*msp);
Houtan Bastani's avatar
Houtan Bastani committed
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
    }
  catch (StackTrace &ex)
    {
      ex.push("@#includepath", location);
      error(ex);
    }
  catch (exception &e)
    {
      error(StackTrace("@#includepath", e.what(), location));
    }
}

void
Define::interpret(ostream &output, bool no_line_macro)
{
  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
Echo::interpret(ostream &output, bool no_line_macro)
{
  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));
    }
156
  printEndLineInfo(output, no_line_macro);
Houtan Bastani's avatar
Houtan Bastani committed
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
}

void
Error::interpret(ostream &output, bool no_line_macro)
{
  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));
    }
}

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

Houtan Bastani's avatar
Houtan Bastani committed
187
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
236
237
238
239
240
241
242
243
244
245
void
For::interpret(ostream &output, bool no_line_macro)
{
  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;
            }
          statement->interpret(output, no_line_macro);
        }
    }
  printEndLineInfo(output, no_line_macro);
}

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

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

void
289
Ifdef::interpret(ostream &output, bool no_line_macro)
Houtan Bastani's avatar
Houtan Bastani committed
290
{
291
  for (auto & it : expr_and_body)
Houtan Bastani's avatar
Houtan Bastani committed
292
    {
293
294
295
      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
296
        {
297
298
          interpretBody(it.second, output, no_line_macro);
          break;
Houtan Bastani's avatar
Houtan Bastani committed
299
300
301
302
303
304
305
306
        }
    }
  printEndLineInfo(output, no_line_macro);
}

void
Ifndef::interpret(ostream &output, bool no_line_macro)
{
307
308
309
310
311
312
313
314
315
316
  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()))))
        {
          interpretBody(it.second, output, no_line_macro);
          break;
        }
    }
Houtan Bastani's avatar
Houtan Bastani committed
317
318
  printEndLineInfo(output, no_line_macro);
}