Directives.cc 6.83 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
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/*
 * 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"

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));
    }
}

42
string
43
Include::interpretAndGetName() const
Houtan Bastani's avatar
Houtan Bastani committed
44
45
46
47
48
49
{
  try
    {
      StringPtr msp = dynamic_pointer_cast<String>(expr->eval());
      if (!msp)
        throw StackTrace("File name does not evaluate to a string");
50
      return msp->to_string();
Houtan Bastani's avatar
Houtan Bastani committed
51
52
53
54
55
56
57
58
59
60
    }
  catch (StackTrace &ex)
    {
      ex.push("@#include", location);
      error(ex);
    }
  catch (exception &e)
    {
      error(StackTrace("@#include", e.what(), location));
    }
61
  return "";
Houtan Bastani's avatar
Houtan Bastani committed
62
63
}

64
string
65
IncludePath::interpretAndGetPath() const
Houtan Bastani's avatar
Houtan Bastani committed
66
67
68
69
70
71
{
  try
    {
      StringPtr msp = dynamic_pointer_cast<String>(expr->eval());
      if (!msp)
        throw StackTrace("File name does not evaluate to a string");
72
      return *msp;
Houtan Bastani's avatar
Houtan Bastani committed
73
74
75
76
77
78
79
80
81
82
    }
  catch (StackTrace &ex)
    {
      ex.push("@#includepath", location);
      error(ex);
    }
  catch (exception &e)
    {
      error(StackTrace("@#includepath", e.what(), location));
    }
83
  return "";
Houtan Bastani's avatar
Houtan Bastani committed
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
}

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));
    }
125
  printEndLineInfo(output, no_line_macro);
Houtan Bastani's avatar
Houtan Bastani committed
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
}

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));
    }
}

146
147
148
149
150
151
152
153
154
155
void
EchoMacroVars::interpret(ostream &output, bool no_line_macro)
{
  if (save)
    env.print(output, location.begin.line, true);
  else
    env.print(cout);
  printEndLineInfo(output, no_line_macro);
}

Houtan Bastani's avatar
Houtan Bastani committed
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
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
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)
{
215
216
217
  for (auto & it : expr_and_body)
    try
      {
218
219
220
        auto tmp = it.first->eval();
        RealPtr dp = dynamic_pointer_cast<Real>(tmp);
        BoolPtr bp = dynamic_pointer_cast<Bool>(tmp);
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
        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
239
240
241
242
  printEndLineInfo(output, no_line_macro);
}

void
243
If::interpretBody(const vector<DirectivePtr> &body, ostream &output, bool no_line_macro)
Houtan Bastani's avatar
Houtan Bastani committed
244
245
{
  bool printLine = !no_line_macro;
246
  for (auto & statement : body)
Houtan Bastani's avatar
Houtan Bastani committed
247
248
249
250
251
252
253
254
255
256
257
    {
      if (printLine)
        {
          statement->printLineInfo(output, no_line_macro);
          printLine = false;
        }
      statement->interpret(output, no_line_macro);
    }
}

void
258
Ifdef::interpret(ostream &output, bool no_line_macro)
Houtan Bastani's avatar
Houtan Bastani committed
259
{
260
  for (auto & it : expr_and_body)
Houtan Bastani's avatar
Houtan Bastani committed
261
    {
262
263
264
      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
265
        {
266
267
          interpretBody(it.second, output, no_line_macro);
          break;
Houtan Bastani's avatar
Houtan Bastani committed
268
269
270
271
272
273
274
275
        }
    }
  printEndLineInfo(output, no_line_macro);
}

void
Ifndef::interpret(ostream &output, bool no_line_macro)
{
276
277
278
279
280
281
282
283
284
285
  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
286
287
  printEndLineInfo(output, no_line_macro);
}