2008-02-03 11:28:36 +01:00
|
|
|
/*
|
2009-01-21 15:39:24 +01:00
|
|
|
* Copyright (C) 2008-2009 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>
|
|
|
|
|
|
|
|
#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
|
2009-01-21 14:45:44 +01:00
|
|
|
MacroDriver::parse(const string &f, ostream &out, bool debug, bool no_line_macro)
|
2008-02-03 11:28:36 +01:00
|
|
|
{
|
|
|
|
file = f;
|
|
|
|
|
2008-02-08 18:55:37 +01:00
|
|
|
ifstream in(f.c_str(), ios::binary);
|
2008-02-29 17:21:01 +01:00
|
|
|
if (in.fail())
|
|
|
|
{
|
|
|
|
cerr << "ERROR: Could not open file: " << f << endl;
|
2008-10-29 16:33:16 +01:00
|
|
|
exit(EXIT_FAILURE);
|
2008-02-29 17:21:01 +01:00
|
|
|
}
|
2008-02-03 11:28:36 +01:00
|
|
|
|
2009-01-21 14:45:44 +01:00
|
|
|
lexer = new MacroFlex(&in, &out, no_line_macro);
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|