2016-11-22 03:15:52 +01:00
|
|
|
#include <anthem/Translation.h>
|
|
|
|
|
|
|
|
#include <fstream>
|
|
|
|
#include <iostream>
|
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
#include <clingo.hh>
|
|
|
|
|
2016-11-24 00:52:28 +01:00
|
|
|
#include <anthem/Context.h>
|
2017-03-16 15:45:55 +01:00
|
|
|
#include <anthem/Simplification.h>
|
2016-11-22 03:15:52 +01:00
|
|
|
#include <anthem/StatementVisitor.h>
|
2017-03-15 16:00:00 +01:00
|
|
|
#include <anthem/output/AST.h>
|
2016-11-22 03:15:52 +01:00
|
|
|
|
|
|
|
namespace anthem
|
|
|
|
{
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// Translation
|
|
|
|
//
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-11-24 02:42:32 +01:00
|
|
|
void translate(const std::vector<std::string> &fileNames, Context &context)
|
2016-11-22 03:15:52 +01:00
|
|
|
{
|
|
|
|
for (const auto &fileName : fileNames)
|
|
|
|
{
|
|
|
|
std::ifstream file(fileName, std::ios::in);
|
|
|
|
|
2016-11-24 02:42:32 +01:00
|
|
|
translate(fileName.c_str(), file, context);
|
2016-11-22 03:15:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-11-24 02:42:32 +01:00
|
|
|
void translate(const char *fileName, std::istream &stream, Context &context)
|
2016-11-22 03:15:52 +01:00
|
|
|
{
|
2016-11-24 03:36:14 +01:00
|
|
|
// TODO: refactor
|
2016-11-24 22:58:13 +01:00
|
|
|
context.logger.log(output::Priority::Info, (std::string("reading ") + fileName).c_str());
|
2016-11-22 03:15:52 +01:00
|
|
|
|
|
|
|
auto fileContent = std::string(std::istreambuf_iterator<char>(stream), {});
|
|
|
|
|
|
|
|
const auto translateStatement =
|
2016-11-24 00:52:28 +01:00
|
|
|
[&context](const Clingo::AST::Statement &statement)
|
2016-11-22 03:15:52 +01:00
|
|
|
{
|
2017-03-16 15:45:55 +01:00
|
|
|
auto formula = statement.data.accept(StatementVisitor(), statement, context);
|
2017-03-15 16:00:00 +01:00
|
|
|
|
|
|
|
if (!formula)
|
|
|
|
return;
|
|
|
|
|
2017-03-16 15:45:55 +01:00
|
|
|
if (!context.simplify)
|
|
|
|
{
|
|
|
|
context.logger.outputStream() << formula.value() << std::endl;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-03-23 00:44:10 +01:00
|
|
|
simplify(formula.value());
|
|
|
|
context.logger.outputStream() << formula.value() << std::endl;
|
2016-11-22 03:15:52 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
const auto logger =
|
2016-11-29 02:15:28 +01:00
|
|
|
[&context](const Clingo::WarningCode, const char *text)
|
2016-11-22 03:15:52 +01:00
|
|
|
{
|
2016-11-24 02:42:32 +01:00
|
|
|
context.logger.log(output::Priority::Error, text);
|
2016-11-22 03:15:52 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
Clingo::parse_program(fileContent.c_str(), translateStatement, logger);
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
}
|