Select Git revision
Forked from
Dynare / preprocessor
Source project has a limited visibility.
-
Houtan Bastani authoredHoutan Bastani authored
Directives.cc 8.85 KiB
/*
* Copyright © 2019-2023 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 <https://www.gnu.org/licenses/>.
*/
#include "Directives.hh"
#include "Driver.hh"
#include <fstream>
#include <utility>
using namespace macro;
void
Eval::interpret(ostream& output, Environment& env, [[maybe_unused]] vector<filesystem::path>& paths)
{
try
{
output << expr->eval(env)->to_string();
}
catch (StackTrace& ex)
{
ex.push("Evaluation", location);
error(ex);
}
catch (exception& e)
{
error(StackTrace("Evaluation", e.what(), location));
}
}
void
Include::interpret(ostream& output, Environment& env, vector<filesystem::path>& paths)
{
using namespace filesystem;
try
{
StringPtr msp = dynamic_pointer_cast<String>(expr->eval(env));
if (!msp)
throw StackTrace("File name does not evaluate to a string");
path filename = msp->to_string();
ifstream incfile(filename, ios::binary);
if (incfile.fail())
{
for (const auto& dir : paths)
{
incfile = ifstream(dir / filename, ios::binary);
if (incfile.good())
break;
}
if (incfile.fail())
{
ostringstream errmsg;
errmsg << " * " << current_path().string() << endl;
for (const auto& dir : paths)
errmsg << " * " << absolute(dir).string() << endl;