2008-02-03 11:28:36 +01:00
|
|
|
/*
|
2017-05-18 18:36:38 +02:00
|
|
|
* Copyright (C) 2008-2017 Dynare Team
|
2008-02-03 11:28:36 +01:00
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2008-06-28 13:20:45 +02:00
|
|
|
#include <cstdlib>
|
2008-02-03 11:28:36 +01:00
|
|
|
#include <iostream>
|
|
|
|
#include <fstream>
|
2010-12-06 17:59:30 +01:00
|
|
|
#include <sstream>
|
2011-06-10 11:52:55 +02:00
|
|
|
#include <boost/lexical_cast.hpp>
|
2008-02-03 11:28:36 +01:00
|
|
|
|
|
|
|
#include "MacroDriver.hh"
|
|
|
|
|
2008-03-28 18:38:10 +01:00
|
|
|
MacroDriver::MacroDriver()
|
2008-02-03 11:28:36 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
MacroDriver::~MacroDriver()
|
|
|
|
{
|
2009-12-16 18:13:23 +01:00
|
|
|
for (set<const MacroValue *>::iterator it = values.begin();
|
|
|
|
it != values.end(); it++)
|
2008-02-29 17:21:01 +01:00
|
|
|
delete *it;
|
2008-02-03 11:28:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-12-04 14:16:42 +01:00
|
|
|
MacroDriver::parse(const string &f, const string &fb, const string &modfiletxt,
|
|
|
|
ostream &out, bool debug, bool no_line_macro_arg, map<string, string> defines,
|
|
|
|
vector<string> path)
|
2008-02-03 11:28:36 +01:00
|
|
|
{
|
|
|
|
file = f;
|
2017-12-04 14:16:42 +01:00
|
|
|
basename = fb;
|
|
|
|
no_line_macro = no_line_macro_arg;
|
2008-02-03 11:28:36 +01:00
|
|
|
|
2010-12-06 17:59:30 +01:00
|
|
|
/*
|
|
|
|
Copy the file into a stringstream, and add an extra end-of-line. This is a
|
|
|
|
workaround for trac ticket #73: with this workaround, MOD files ending with
|
|
|
|
an @#endif or an @#endfor - but no newline - no longer trigger an error.
|
|
|
|
*/
|
|
|
|
stringstream file_with_endl;
|
2017-05-16 16:30:27 +02:00
|
|
|
for (map<string, string>::iterator it = defines.begin();
|
|
|
|
it != defines.end(); it++)
|
2011-06-10 11:52:55 +02:00
|
|
|
try
|
|
|
|
{
|
|
|
|
boost::lexical_cast<int>(it->second);
|
|
|
|
file_with_endl << "@#define " << it->first << " = " << it->second << endl;
|
|
|
|
}
|
2017-05-16 16:30:27 +02:00
|
|
|
catch (boost::bad_lexical_cast &)
|
2011-06-10 11:52:55 +02:00
|
|
|
{
|
|
|
|
file_with_endl << "@#define " << it->first << " = \"" << it->second << "\"" << endl;
|
|
|
|
}
|
preprocessor: allow passing mod file as string. Closes #1509
Usage: ./dynare_m $'<<mod file text>>'
The $’’ expands special characters. This is necessary because our setup for native matlab statements require that they end with a newline. In other words, the rest of the mod file can be sent on one line, but if there is a native matlab statement you must enter a `\n` after it.
NB: In this case, apostrophes must be escaped: ' becomes \'
e.g., to run tests/example1.mod:
./dynare_m $'//Example 1 from Collard\'s guide to Dynare\nvar y, c, k, a, h, b;varexo e, u;verbatim;% I want these comments included in\n% example1.m 1999q1 1999y\n%\nvar = 1;\nend;parameters beta, rho, alpha, delta, theta, psi, tau;alpha = 0.36;rho = 0.95;tau = 0.025;beta = 0.99;delta = 0.025;psi = 0;theta = 2.95;phi = 0.1;\nmodel;c*theta*h^(1+psi)=(1-alpha)*y;k = beta*(((exp(b)*c)/(exp(b(+1))*c(+1)))*(exp(b(+1))*alpha*y(+1)+(1-delta)*k));y = exp(a)*(k(-1)^alpha)*(h^(1-alpha));k = exp(b)*(y-c)+(1-delta)*k(-1);a = rho*a(-1)+tau*b(-1) + e;b = tau*a(-1)+rho*b(-1) + u;end;initval;y = 1.08068253095672;c = 0.80359242014163;h = 0.29175631001732;k = 11.08360443260358;a = 0;b = 0;e = 0;u = 0;end;shocks;var e; stderr 0.009;var u; stderr 0.009;var e, u = phi*0.009*0.009;end; stoch_simul;'
2017-09-08 16:18:48 +02:00
|
|
|
file_with_endl << modfiletxt << endl;
|
2010-12-06 17:59:30 +01:00
|
|
|
|
2015-08-27 16:49:00 +02:00
|
|
|
lexer = new MacroFlex(&file_with_endl, &out, no_line_macro, path);
|
2008-03-28 18:21:45 +01:00
|
|
|
lexer->set_debug(debug);
|
2008-02-03 11:28:36 +01:00
|
|
|
|
|
|
|
Macro::parser parser(*this, out);
|
2008-03-28 18:21:45 +01:00
|
|
|
parser.set_debug_level(debug);
|
2009-01-21 14:45:44 +01:00
|
|
|
|
|
|
|
// Output first @#line statement
|
|
|
|
if (!no_line_macro)
|
|
|
|
out << "@#line \"" << file << "\" 1" << endl;
|
|
|
|
|
|
|
|
// Launch macro-processing
|
2008-02-03 11:28:36 +01:00
|
|
|
parser.parse();
|
|
|
|
|
|
|
|
delete lexer;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2008-02-22 13:03:38 +01:00
|
|
|
MacroDriver::error(const Macro::parser::location_type &l, const string &m) const
|
2008-02-03 11:28:36 +01:00
|
|
|
{
|
2008-02-22 13:03:38 +01:00
|
|
|
cerr << "ERROR in macro-processor: " << l << ": " << m << endl;
|
2008-10-29 16:33:16 +01:00
|
|
|
exit(EXIT_FAILURE);
|
2008-02-03 11:28:36 +01:00
|
|
|
}
|
2008-02-22 13:03:38 +01:00
|
|
|
|
|
|
|
void
|
2008-02-29 17:21:01 +01:00
|
|
|
MacroDriver::set_variable(const string &name, const MacroValue *value)
|
2008-02-22 13:03:38 +01:00
|
|
|
{
|
|
|
|
env[name] = value;
|
|
|
|
}
|
|
|
|
|
2008-02-29 17:21:01 +01:00
|
|
|
const MacroValue *
|
2008-02-22 13:03:38 +01:00
|
|
|
MacroDriver::get_variable(const string &name) const throw (UnknownVariable)
|
|
|
|
{
|
2008-02-29 17:21:01 +01:00
|
|
|
map<string, const MacroValue *>::const_iterator it = env.find(name);
|
2008-02-22 13:03:38 +01:00
|
|
|
if (it == env.end())
|
|
|
|
throw UnknownVariable(name);
|
2008-02-29 17:21:01 +01:00
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MacroDriver::init_loop(const string &name, const MacroValue *value) throw (MacroValue::TypeError)
|
|
|
|
{
|
|
|
|
const ArrayMV<int> *mv1 = dynamic_cast<const ArrayMV<int> *>(value);
|
|
|
|
const ArrayMV<string> *mv2 = dynamic_cast<const ArrayMV<string> *>(value);
|
|
|
|
if (!mv1 && !mv2)
|
2008-04-10 10:16:54 +02:00
|
|
|
throw MacroValue::TypeError("Argument of @#for loop must be an array expression");
|
2008-02-29 17:21:01 +01:00
|
|
|
loop_stack.push(make_pair(name, make_pair(value, 0)));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
MacroDriver::iter_loop()
|
|
|
|
{
|
|
|
|
if (loop_stack.empty())
|
|
|
|
throw "No loop on which to iterate!";
|
|
|
|
|
|
|
|
int &i = loop_stack.top().second.second;
|
|
|
|
const MacroValue *mv = loop_stack.top().second.first;
|
|
|
|
string name = loop_stack.top().first;
|
|
|
|
|
|
|
|
const ArrayMV<int> *mv1 = dynamic_cast<const ArrayMV<int> *>(mv);
|
|
|
|
if (mv1)
|
|
|
|
{
|
|
|
|
if (i >= (int) mv1->values.size())
|
|
|
|
{
|
|
|
|
loop_stack.pop();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
env[name] = new IntMV(*this, mv1->values[i++]);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const ArrayMV<string> *mv2 = dynamic_cast<const ArrayMV<string> *>(mv);
|
|
|
|
if (i >= (int) mv2->values.size())
|
|
|
|
{
|
|
|
|
loop_stack.pop();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
env[name] = new StringMV(*this, mv2->values[i++]);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2008-02-22 13:03:38 +01:00
|
|
|
}
|
2008-03-07 16:58:35 +01:00
|
|
|
|
|
|
|
void
|
|
|
|
MacroDriver::begin_if(const MacroValue *value) throw (MacroValue::TypeError)
|
|
|
|
{
|
|
|
|
const IntMV *ival = dynamic_cast<const IntMV *>(value);
|
|
|
|
if (!ival)
|
2008-04-10 10:16:54 +02:00
|
|
|
throw MacroValue::TypeError("Argument of @#if must be an integer");
|
2008-03-07 16:58:35 +01:00
|
|
|
last_if = (bool) ival->value;
|
|
|
|
}
|
|
|
|
|
2012-01-02 18:08:14 +01:00
|
|
|
void
|
|
|
|
MacroDriver::begin_ifdef(const string &name)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
get_variable(name);
|
2012-06-26 14:50:16 +02:00
|
|
|
begin_if(new IntMV(*this, 1));
|
2012-01-02 18:08:14 +01:00
|
|
|
}
|
|
|
|
catch (UnknownVariable &)
|
|
|
|
{
|
2012-06-26 14:50:16 +02:00
|
|
|
begin_if(new IntMV(*this, 0));
|
2012-01-02 18:08:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-26 15:12:31 +02:00
|
|
|
void
|
|
|
|
MacroDriver::begin_ifndef(const string &name)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
get_variable(name);
|
|
|
|
begin_if(new IntMV(*this, 0));
|
|
|
|
}
|
|
|
|
catch (UnknownVariable &)
|
|
|
|
{
|
|
|
|
begin_if(new IntMV(*this, 1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-07 16:58:35 +01:00
|
|
|
void
|
|
|
|
MacroDriver::echo(const Macro::parser::location_type &l, const MacroValue *value) const throw (MacroValue::TypeError)
|
|
|
|
{
|
|
|
|
const StringMV *sval = dynamic_cast<const StringMV *>(value);
|
|
|
|
if (!sval)
|
2008-04-10 10:16:54 +02:00
|
|
|
throw MacroValue::TypeError("Argument of @#echo must be a string");
|
2008-03-07 16:58:35 +01:00
|
|
|
|
|
|
|
cerr << "ECHO in macro-processor: " << l << ": " << sval->value << endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MacroDriver::error(const Macro::parser::location_type &l, const MacroValue *value) const throw (MacroValue::TypeError)
|
|
|
|
{
|
|
|
|
const StringMV *sval = dynamic_cast<const StringMV *>(value);
|
|
|
|
if (!sval)
|
2008-04-10 10:16:54 +02:00
|
|
|
throw MacroValue::TypeError("Argument of @#error must be a string");
|
2008-03-07 16:58:35 +01:00
|
|
|
|
|
|
|
error(l, sval->value);
|
|
|
|
}
|
2017-12-01 16:55:10 +01:00
|
|
|
|
2017-12-04 14:16:42 +01:00
|
|
|
string
|
|
|
|
MacroDriver::printvars(const Macro::parser::location_type &l, const bool tostdout) const
|
2017-12-01 16:55:10 +01:00
|
|
|
{
|
2017-12-04 14:16:42 +01:00
|
|
|
if (tostdout)
|
|
|
|
{
|
|
|
|
cout << "Macroprocessor: Printing macro variable values from " << file
|
|
|
|
<< " at line " << l.begin.line << endl;
|
|
|
|
for (map<string, const MacroValue *>::const_iterator it = env.begin();
|
|
|
|
it != env.end(); it++)
|
|
|
|
cout << " " << it->first << " = " << it->second->print() << endl;
|
|
|
|
cout << endl;
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
stringstream intomfile;
|
|
|
|
if (!no_line_macro)
|
|
|
|
intomfile << "@#line \"" << file << "\" " << l.begin.line << endl;
|
|
|
|
|
2017-12-01 16:55:10 +01:00
|
|
|
for (map<string, const MacroValue *>::const_iterator it = env.begin();
|
|
|
|
it != env.end(); it++)
|
2017-12-07 18:11:00 +01:00
|
|
|
intomfile<< "options_.macrovars_line_" << l.begin.line << "." << it->first << " = " << it->second->print() << ";" << endl;
|
2017-12-04 14:16:42 +01:00
|
|
|
return intomfile.str();
|
2017-12-01 16:55:10 +01:00
|
|
|
}
|