Started implementing color output (currently for head only).
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
#include <algorithm>
|
||||
|
||||
#include <anthem/Utils.h>
|
||||
#include <anthem/output/Formatting.h>
|
||||
|
||||
namespace anthem
|
||||
{
|
||||
|
@@ -4,6 +4,7 @@
|
||||
#include <anthem/Body.h>
|
||||
#include <anthem/Head.h>
|
||||
#include <anthem/Utils.h>
|
||||
#include <anthem/output/ClingoOutput.h>
|
||||
|
||||
namespace anthem
|
||||
{
|
||||
@@ -18,7 +19,7 @@ struct StatementVisitor
|
||||
{
|
||||
void visit(const Clingo::AST::Program &program, const Clingo::AST::Statement &statement, Context &context)
|
||||
{
|
||||
std::cout << "[program] " << program.name << std::endl;
|
||||
context.logger.log(output::Priority::Debug, program.name);
|
||||
|
||||
if (!program.parameters.empty())
|
||||
throwErrorAtLocation(statement.location, "program parameters currently unsupported", context);
|
||||
@@ -26,6 +27,8 @@ struct StatementVisitor
|
||||
|
||||
void visit(const Clingo::AST::Rule &rule, const Clingo::AST::Statement &, Context &context)
|
||||
{
|
||||
auto &outputStream = context.logger.outputStream();
|
||||
|
||||
// Concatenate all head terms
|
||||
rule.head.data.accept(HeadLiteralCollectFunctionTermsVisitor(), rule.head, context);
|
||||
|
||||
@@ -37,16 +40,18 @@ struct StatementVisitor
|
||||
const auto &headTerm = **i;
|
||||
|
||||
if (i != context.headTerms.cbegin())
|
||||
std::cout << ", ";
|
||||
outputStream << ", ";
|
||||
|
||||
std::cout
|
||||
<< AuxiliaryHeadVariablePrefix << (i - context.headTerms.cbegin())
|
||||
<< " in " << headTerm;
|
||||
const auto variableName = std::string(AuxiliaryBodyVariablePrefix) + std::to_string(i - context.headTerms.cbegin());
|
||||
|
||||
outputStream
|
||||
<< output::Variable(variableName.c_str())
|
||||
<< " " << output::Keyword("in") << " " << headTerm;
|
||||
}
|
||||
}
|
||||
|
||||
if (rule.body.empty() && context.headTerms.empty())
|
||||
std::cout << "true";
|
||||
outputStream << output::Boolean("true");
|
||||
else
|
||||
{
|
||||
// Print translated body literals
|
||||
@@ -64,7 +69,7 @@ struct StatementVisitor
|
||||
}
|
||||
}
|
||||
|
||||
std::cout << " -> ";
|
||||
outputStream << " " << output::Operator("->") << " ";
|
||||
|
||||
// Print consequent of the implication
|
||||
rule.head.data.accept(HeadLiteralPrintSubstitutedVisitor(), rule.head, context);
|
||||
|
@@ -4,6 +4,8 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include <anthem/Context.h>
|
||||
|
||||
namespace anthem
|
||||
{
|
||||
|
||||
@@ -13,8 +15,8 @@ namespace anthem
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
void translate(const std::vector<std::string> &fileNames);
|
||||
void translate(const char *fileName, std::istream &stream);
|
||||
void translate(const std::vector<std::string> &fileNames, Context &context);
|
||||
void translate(const char *fileName, std::istream &stream, Context &context);
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
|
35
include/anthem/output/ClingoOutput.h
Normal file
35
include/anthem/output/ClingoOutput.h
Normal file
@@ -0,0 +1,35 @@
|
||||
#ifndef __ANTHEM__OUTPUT__CLINGO_OUTPUT_H
|
||||
#define __ANTHEM__OUTPUT__CLINGO_OUTPUT_H
|
||||
|
||||
#include <clingo.hh>
|
||||
|
||||
#include <anthem/output/ColorStream.h>
|
||||
|
||||
namespace anthem
|
||||
{
|
||||
namespace output
|
||||
{
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// ClingoOutput
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
ColorStream &operator<<(ColorStream &stream, const Clingo::Symbol &symbol);
|
||||
ColorStream &operator<<(ColorStream &stream, const Clingo::AST::Sign &sign);
|
||||
ColorStream &operator<<(ColorStream &stream, const Clingo::AST::Variable &variable);
|
||||
ColorStream &operator<<(ColorStream &stream, const Clingo::AST::BinaryOperator &binaryOperator);
|
||||
ColorStream &operator<<(ColorStream &stream, const Clingo::AST::UnaryOperation &unaryOperation);
|
||||
ColorStream &operator<<(ColorStream &stream, const Clingo::AST::BinaryOperation &binaryOperation);
|
||||
ColorStream &operator<<(ColorStream &stream, const Clingo::AST::Interval &interval);
|
||||
ColorStream &operator<<(ColorStream &stream, const Clingo::AST::Function &function);
|
||||
ColorStream &operator<<(ColorStream &stream, const Clingo::AST::Pool &pool);
|
||||
ColorStream &operator<<(ColorStream &stream, const Clingo::AST::Term &term);
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
@@ -84,19 +84,18 @@ inline ColorStream &operator<<(ColorStream &stream, const ResetFormat &)
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
struct Token
|
||||
struct Function
|
||||
{
|
||||
Function(const char *name)
|
||||
: name{name}
|
||||
{
|
||||
};
|
||||
|
||||
const char *name;
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
struct Function: public Token
|
||||
{
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
inline ColorStream &operator<<(ColorStream &stream, const Function &function)
|
||||
{
|
||||
return (stream
|
||||
@@ -107,8 +106,14 @@ inline ColorStream &operator<<(ColorStream &stream, const Function &function)
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
struct Keyword: public Token
|
||||
struct Keyword
|
||||
{
|
||||
Keyword(const char *name)
|
||||
: name{name}
|
||||
{
|
||||
};
|
||||
|
||||
const char *name;
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
@@ -116,30 +121,64 @@ struct Keyword: public Token
|
||||
inline ColorStream &operator<<(ColorStream &stream, const Keyword &keyword)
|
||||
{
|
||||
return (stream
|
||||
<< Format({Color::Blue, FontWeight::Normal})
|
||||
<< Format({Color::Blue, FontWeight::Bold})
|
||||
<< keyword.name
|
||||
<< ResetFormat());
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
struct Number: public Token
|
||||
struct Operator
|
||||
{
|
||||
Operator(const char *name)
|
||||
: name{name}
|
||||
{
|
||||
};
|
||||
|
||||
const char *name;
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
inline ColorStream &operator<<(ColorStream &stream, const Number &number)
|
||||
inline ColorStream &operator<<(ColorStream &stream, const Operator &operator_)
|
||||
{
|
||||
return (stream << operator_.name);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<typename T>
|
||||
struct Number
|
||||
{
|
||||
Number(T value)
|
||||
: value{value}
|
||||
{
|
||||
};
|
||||
|
||||
T value;
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<typename T>
|
||||
inline ColorStream &operator<<(ColorStream &stream, const Number<T> &number)
|
||||
{
|
||||
return (stream
|
||||
<< Format({Color::Yellow, FontWeight::Normal})
|
||||
<< number.name
|
||||
<< number.value
|
||||
<< ResetFormat());
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
struct Variable: public Token
|
||||
struct Variable
|
||||
{
|
||||
Variable(const char *name)
|
||||
: name{name}
|
||||
{
|
||||
};
|
||||
|
||||
const char *name;
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
@@ -154,8 +193,14 @@ inline ColorStream &operator<<(ColorStream &stream, const Variable &variable)
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
struct String: public Token
|
||||
struct String
|
||||
{
|
||||
String(const char *content)
|
||||
: content{content}
|
||||
{
|
||||
};
|
||||
|
||||
const char *content;
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
@@ -164,14 +209,20 @@ inline ColorStream &operator<<(ColorStream &stream, const String &string)
|
||||
{
|
||||
return (stream
|
||||
<< Format({Color::Green, FontWeight::Normal})
|
||||
<< "\"" << string.name << "\""
|
||||
<< "\"" << string.content << "\""
|
||||
<< ResetFormat());
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
struct Boolean: public Token
|
||||
struct Boolean
|
||||
{
|
||||
Boolean(bool value)
|
||||
: value{value}
|
||||
{
|
||||
};
|
||||
|
||||
bool value;
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
@@ -180,14 +231,20 @@ inline ColorStream &operator<<(ColorStream &stream, const Boolean &boolean)
|
||||
{
|
||||
return (stream
|
||||
<< Format({Color::Red, FontWeight::Normal})
|
||||
<< boolean.name
|
||||
<< (boolean.value == true ? "true" : "false")
|
||||
<< ResetFormat());
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
struct Reserved: public Token
|
||||
struct Reserved
|
||||
{
|
||||
Reserved(const char *name)
|
||||
: name{name}
|
||||
{
|
||||
};
|
||||
|
||||
const char *name;
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
@@ -202,8 +259,14 @@ inline ColorStream &operator<<(ColorStream &stream, const Reserved &reserved)
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
struct Heading1: public Token
|
||||
struct Heading1
|
||||
{
|
||||
Heading1(const char *content)
|
||||
: content{content}
|
||||
{
|
||||
};
|
||||
|
||||
const char *content;
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
@@ -213,7 +276,7 @@ inline ColorStream &operator<<(ColorStream &stream, const Heading1 &heading1)
|
||||
return (stream
|
||||
<< Format({Color::Blue, FontWeight::Bold})
|
||||
<< "%---------------------------------------" << std::endl
|
||||
<< "% " << heading1.name << std::endl
|
||||
<< "% " << heading1.content << std::endl
|
||||
<< "%---------------------------------------"
|
||||
<< ResetFormat()
|
||||
<< std::endl);
|
||||
@@ -221,8 +284,14 @@ inline ColorStream &operator<<(ColorStream &stream, const Heading1 &heading1)
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
struct Heading2: public Token
|
||||
struct Heading2
|
||||
{
|
||||
Heading2(const char *content)
|
||||
: content{content}
|
||||
{
|
||||
};
|
||||
|
||||
const char *content;
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
@@ -231,7 +300,7 @@ inline ColorStream &operator<<(ColorStream &stream, const Heading2 &heading2)
|
||||
{
|
||||
return (stream
|
||||
<< Format({Color::Blue, FontWeight::Bold})
|
||||
<< "% " << heading2.name
|
||||
<< "% " << heading2.content
|
||||
<< ResetFormat());
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user