From 6af3b7b875934f18cfdff50bc73776d3cbb72445 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20L=C3=BChne?= Date: Thu, 24 Nov 2016 00:52:28 +0100 Subject: [PATCH] Made all visitors consistent in taking an Context object as an argument. --- include/anthem/Body.h | 54 ++++++------ include/anthem/Context.h | 9 +- include/anthem/Head.h | 138 +++++++++++++++--------------- include/anthem/StatementVisitor.h | 82 ++++++++---------- include/anthem/Utils.h | 28 ++++-- include/anthem/input/Location.h | 10 ++- src/anthem/Translation.cpp | 7 +- 7 files changed, 171 insertions(+), 157 deletions(-) diff --git a/include/anthem/Body.h b/include/anthem/Body.h index 9eacf26..02ebd14 100644 --- a/include/anthem/Body.h +++ b/include/anthem/Body.h @@ -17,35 +17,35 @@ namespace anthem struct TermPrintVisitor { - void visit(const Clingo::Symbol &, const Clingo::AST::Literal &, const Clingo::AST::Term &term, Context &) + void visit(const Clingo::Symbol &, const Clingo::AST::Literal &, const Clingo::AST::Term &term, Context &context) { - throwErrorAtLocation(term.location, "“symbol” terms currently unsupported in this context"); + throwErrorAtLocation(term.location, "“symbol” terms currently unsupported in this context", context); } - void visit(const Clingo::AST::Variable &, const Clingo::AST::Literal &, const Clingo::AST::Term &term, Context &) + void visit(const Clingo::AST::Variable &, const Clingo::AST::Literal &, const Clingo::AST::Term &term, Context &context) { - throwErrorAtLocation(term.location, "“variable” terms currently unsupported in this context"); + throwErrorAtLocation(term.location, "“variable” terms currently unsupported in this context", context); } - void visit(const Clingo::AST::UnaryOperation &, const Clingo::AST::Literal &, const Clingo::AST::Term &term, Context &) + void visit(const Clingo::AST::UnaryOperation &, const Clingo::AST::Literal &, const Clingo::AST::Term &term, Context &context) { - throwErrorAtLocation(term.location, "“unary operation” terms currently unsupported in this context"); + throwErrorAtLocation(term.location, "“unary operation” terms currently unsupported in this context", context); } - void visit(const Clingo::AST::BinaryOperation &, const Clingo::AST::Literal &, const Clingo::AST::Term &term, Context &) + void visit(const Clingo::AST::BinaryOperation &, const Clingo::AST::Literal &, const Clingo::AST::Term &term, Context &context) { - throwErrorAtLocation(term.location, "“binary operation” terms currently unsupported in this context"); + throwErrorAtLocation(term.location, "“binary operation” terms currently unsupported in this context", context); } - void visit(const Clingo::AST::Interval &, const Clingo::AST::Literal &, const Clingo::AST::Term &term, Context &) + void visit(const Clingo::AST::Interval &, const Clingo::AST::Literal &, const Clingo::AST::Term &term, Context &context) { - throwErrorAtLocation(term.location, "“interval” terms currently unsupported in this context"); + throwErrorAtLocation(term.location, "“interval” terms currently unsupported in this context", context); } void visit(const Clingo::AST::Function &function, const Clingo::AST::Literal &literal, const Clingo::AST::Term &term, Context &context) { if (literal.sign == Clingo::AST::Sign::DoubleNegation) - throwErrorAtLocation(literal.location, "double-negated literals currently unsupported"); + throwErrorAtLocation(literal.location, "double-negated literals currently unsupported", context); if (function.arguments.empty()) { @@ -98,9 +98,9 @@ struct TermPrintVisitor context.auxiliaryBodyLiteralID += function.arguments.size(); } - void visit(const Clingo::AST::Pool &, const Clingo::AST::Literal &, const Clingo::AST::Term &term, Context &) + void visit(const Clingo::AST::Pool &, const Clingo::AST::Literal &, const Clingo::AST::Term &term, Context &context) { - throwErrorAtLocation(term.location, "“pool” terms currently unsupported"); + throwErrorAtLocation(term.location, "“pool” terms currently unsupported", context); } }; @@ -108,9 +108,9 @@ struct TermPrintVisitor struct LiteralPrintVisitor { - void visit(const Clingo::AST::Boolean &, const Clingo::AST::Literal &literal, Context &) + void visit(const Clingo::AST::Boolean &, const Clingo::AST::Literal &literal, Context &context) { - throwErrorAtLocation(literal.location, "“boolean” literals currently unsupported in this context"); + throwErrorAtLocation(literal.location, "“boolean” literals currently unsupported in this context", context); } void visit(const Clingo::AST::Term &term, const Clingo::AST::Literal &literal, Context &context) @@ -164,9 +164,9 @@ struct LiteralPrintVisitor context.auxiliaryBodyLiteralID += 2; } - void visit(const Clingo::AST::CSPLiteral &, const Clingo::AST::Literal &literal, Context &) + void visit(const Clingo::AST::CSPLiteral &, const Clingo::AST::Literal &literal, Context &context) { - throwErrorAtLocation(literal.location, "CSP literals currently unsupported"); + throwErrorAtLocation(literal.location, "CSP literals currently unsupported", context); } }; @@ -179,29 +179,29 @@ struct BodyLiteralPrintVisitor literal.data.accept(LiteralPrintVisitor(), literal, context); } - void visit(const Clingo::AST::ConditionalLiteral &, const Clingo::AST::BodyLiteral &bodyLiteral, Context &) + void visit(const Clingo::AST::ConditionalLiteral &, const Clingo::AST::BodyLiteral &bodyLiteral, Context &context) { - throwErrorAtLocation(bodyLiteral.location, "“conditional literal” body literals currently unsupported"); + throwErrorAtLocation(bodyLiteral.location, "“conditional literal” body literals currently unsupported", context); } - void visit(const Clingo::AST::Aggregate &, const Clingo::AST::BodyLiteral &bodyLiteral, Context &) + void visit(const Clingo::AST::Aggregate &, const Clingo::AST::BodyLiteral &bodyLiteral, Context &context) { - throwErrorAtLocation(bodyLiteral.location, "“aggregate” body literals currently unsupported"); + throwErrorAtLocation(bodyLiteral.location, "“aggregate” body literals currently unsupported", context); } - void visit(const Clingo::AST::BodyAggregate &, const Clingo::AST::BodyLiteral &bodyLiteral, Context &) + void visit(const Clingo::AST::BodyAggregate &, const Clingo::AST::BodyLiteral &bodyLiteral, Context &context) { - throwErrorAtLocation(bodyLiteral.location, "“body aggregate” body literals currently unsupported"); + throwErrorAtLocation(bodyLiteral.location, "“body aggregate” body literals currently unsupported", context); } - void visit(const Clingo::AST::TheoryAtom &, const Clingo::AST::BodyLiteral &bodyLiteral, Context &) + void visit(const Clingo::AST::TheoryAtom &, const Clingo::AST::BodyLiteral &bodyLiteral, Context &context) { - throwErrorAtLocation(bodyLiteral.location, "“theory atom” body literals currently unsupported"); + throwErrorAtLocation(bodyLiteral.location, "“theory atom” body literals currently unsupported", context); } - void visit(const Clingo::AST::Disjoint &, const Clingo::AST::BodyLiteral &bodyLiteral, Context &) + void visit(const Clingo::AST::Disjoint &, const Clingo::AST::BodyLiteral &bodyLiteral, Context &context) { - throwErrorAtLocation(bodyLiteral.location, "“disjoint” body literals currently unsupported"); + throwErrorAtLocation(bodyLiteral.location, "“disjoint” body literals currently unsupported", context); } }; diff --git a/include/anthem/Context.h b/include/anthem/Context.h index 0e3a17e..7ed2cb4 100644 --- a/include/anthem/Context.h +++ b/include/anthem/Context.h @@ -1,6 +1,10 @@ #ifndef __ANTHEM__CONTEXT_H #define __ANTHEM__CONTEXT_H +#include + +#include + namespace anthem { @@ -12,7 +16,10 @@ namespace anthem struct Context { - int auxiliaryBodyLiteralID = 0; + output::Logger logger; + + std::vector headTerms; + size_t auxiliaryBodyLiteralID = 0; }; //////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/include/anthem/Head.h b/include/anthem/Head.h index 2f717a3..8d81462 100644 --- a/include/anthem/Head.h +++ b/include/anthem/Head.h @@ -16,45 +16,45 @@ namespace anthem struct TermCollectFunctionTermsVisitor { - void visit(const Clingo::Symbol &symbol, const Clingo::AST::Term &term, std::vector &) + void visit(const Clingo::Symbol &symbol, const Clingo::AST::Term &term, Context &context) { - throwErrorAtLocation(term.location, "“symbol” terms not allowed, function expected"); + throwErrorAtLocation(term.location, "“symbol” terms not allowed, function expected", context); } - void visit(const Clingo::AST::Variable &, const Clingo::AST::Term &term, std::vector &) + void visit(const Clingo::AST::Variable &, const Clingo::AST::Term &term, Context &context) { - throwErrorAtLocation(term.location, "“variable” terms currently unsupported, function expected"); + throwErrorAtLocation(term.location, "“variable” terms currently unsupported, function expected", context); } - void visit(const Clingo::AST::UnaryOperation &, const Clingo::AST::Term &term, std::vector &) + void visit(const Clingo::AST::UnaryOperation &, const Clingo::AST::Term &term, Context &context) { - throwErrorAtLocation(term.location, "“unary operation” terms currently unsupported, function expected"); + throwErrorAtLocation(term.location, "“unary operation” terms currently unsupported, function expected", context); } - void visit(const Clingo::AST::BinaryOperation &, const Clingo::AST::Term &term, std::vector &) + void visit(const Clingo::AST::BinaryOperation &, const Clingo::AST::Term &term, Context &context) { - throwErrorAtLocation(term.location, "“binary operation” terms currently unsupported, function expected"); + throwErrorAtLocation(term.location, "“binary operation” terms currently unsupported, function expected", context); } - void visit(const Clingo::AST::Interval &, const Clingo::AST::Term &term, std::vector &) + void visit(const Clingo::AST::Interval &, const Clingo::AST::Term &term, Context &context) { - throwErrorAtLocation(term.location, "“interval” terms currently unsupported, function expected"); + throwErrorAtLocation(term.location, "“interval” terms currently unsupported, function expected", context); } - void visit(const Clingo::AST::Function &function, const Clingo::AST::Term &term, std::vector &terms) + void visit(const Clingo::AST::Function &function, const Clingo::AST::Term &term, Context &context) { if (function.external) - throwErrorAtLocation(term.location, "external functions currently unsupported"); + throwErrorAtLocation(term.location, "external functions currently unsupported", context); - terms.reserve(terms.size() + function.arguments.size()); + context.headTerms.reserve(context.headTerms.size() + function.arguments.size()); for (const auto &argument : function.arguments) - terms.emplace_back(&argument); + context.headTerms.emplace_back(&argument); } - void visit(const Clingo::AST::Pool &, const Clingo::AST::Term &term, std::vector &) + void visit(const Clingo::AST::Pool &, const Clingo::AST::Term &term, Context &context) { - throwErrorAtLocation(term.location, "“pool” terms currently unsupported, function expected"); + throwErrorAtLocation(term.location, "“pool” terms currently unsupported, function expected", context); } }; @@ -62,23 +62,23 @@ struct TermCollectFunctionTermsVisitor struct LiteralCollectFunctionTermsVisitor { - void visit(const Clingo::AST::Boolean &, const Clingo::AST::Literal &, std::vector &) + void visit(const Clingo::AST::Boolean &, const Clingo::AST::Literal &, Context &context) { } - void visit(const Clingo::AST::Term &term, const Clingo::AST::Literal &, std::vector &terms) + void visit(const Clingo::AST::Term &term, const Clingo::AST::Literal &, Context &context) { - term.data.accept(TermCollectFunctionTermsVisitor(), term, terms); + term.data.accept(TermCollectFunctionTermsVisitor(), term, context); } - void visit(const Clingo::AST::Comparison &, const Clingo::AST::Literal &literal, std::vector &) + void visit(const Clingo::AST::Comparison &, const Clingo::AST::Literal &literal, Context &context) { - throwErrorAtLocation(literal.location, "only disjunctions of literals allowed as head literals"); + throwErrorAtLocation(literal.location, "only disjunctions of literals allowed as head literals", context); } - void visit(const Clingo::AST::CSPLiteral &, const Clingo::AST::Literal &literal, std::vector &) + void visit(const Clingo::AST::CSPLiteral &, const Clingo::AST::Literal &literal, Context &context) { - throwErrorAtLocation(literal.location, "only disjunctions of literals allowed as head literals"); + throwErrorAtLocation(literal.location, "only disjunctions of literals allowed as head literals", context); } }; @@ -86,35 +86,35 @@ struct LiteralCollectFunctionTermsVisitor struct HeadLiteralCollectFunctionTermsVisitor { - void visit(const Clingo::AST::Literal &literal, const Clingo::AST::HeadLiteral &, std::vector &terms) + void visit(const Clingo::AST::Literal &literal, const Clingo::AST::HeadLiteral &, Context &context) { - literal.data.accept(LiteralCollectFunctionTermsVisitor(), literal, terms); + literal.data.accept(LiteralCollectFunctionTermsVisitor(), literal, context); } - void visit(const Clingo::AST::Disjunction &disjunction, const Clingo::AST::HeadLiteral &headLiteral, std::vector &terms) + void visit(const Clingo::AST::Disjunction &disjunction, const Clingo::AST::HeadLiteral &headLiteral, Context &context) { for (const auto &conditionLiteral : disjunction.elements) { if (!conditionLiteral.condition.empty()) - throwErrorAtLocation(headLiteral.location, "conditional head literals currently unsupported"); + throwErrorAtLocation(headLiteral.location, "conditional head literals currently unsupported", context); - conditionLiteral.literal.data.accept(LiteralCollectFunctionTermsVisitor(), conditionLiteral.literal, terms); + conditionLiteral.literal.data.accept(LiteralCollectFunctionTermsVisitor(), conditionLiteral.literal, context); } } - void visit(const Clingo::AST::Aggregate &, const Clingo::AST::HeadLiteral &headLiteral, std::vector &) + void visit(const Clingo::AST::Aggregate &, const Clingo::AST::HeadLiteral &headLiteral, Context &context) { - throwErrorAtLocation(headLiteral.location, "“aggregate” head literals currently unsupported"); + throwErrorAtLocation(headLiteral.location, "“aggregate” head literals currently unsupported", context); } - void visit(const Clingo::AST::HeadAggregate &, const Clingo::AST::HeadLiteral &headLiteral, std::vector &) + void visit(const Clingo::AST::HeadAggregate &, const Clingo::AST::HeadLiteral &headLiteral, Context &context) { - throwErrorAtLocation(headLiteral.location, "“head aggregate” head literals currently unsupported"); + throwErrorAtLocation(headLiteral.location, "“head aggregate” head literals currently unsupported", context); } - void visit(const Clingo::AST::TheoryAtom &, const Clingo::AST::HeadLiteral &headLiteral, std::vector &) + void visit(const Clingo::AST::TheoryAtom &, const Clingo::AST::HeadLiteral &headLiteral, Context &context) { - throwErrorAtLocation(headLiteral.location, "“theory” head literals currently unsupported"); + throwErrorAtLocation(headLiteral.location, "“theory” head literals currently unsupported", context); } }; @@ -122,35 +122,35 @@ struct HeadLiteralCollectFunctionTermsVisitor struct TermPrintSubstitutedVisitor { - void visit(const Clingo::Symbol &symbol, const Clingo::AST::Term &term, const std::vector &) + void visit(const Clingo::Symbol &symbol, const Clingo::AST::Term &term, Context &context) { - throwErrorAtLocation(term.location, "“symbol” terms not allowed, function expected"); + throwErrorAtLocation(term.location, "“symbol” terms not allowed, function expected", context); } - void visit(const Clingo::AST::Variable &, const Clingo::AST::Term &term, const std::vector &) + void visit(const Clingo::AST::Variable &, const Clingo::AST::Term &term, Context &context) { - throwErrorAtLocation(term.location, "“variable” terms currently unsupported, function expected"); + throwErrorAtLocation(term.location, "“variable” terms currently unsupported, function expected", context); } - void visit(const Clingo::AST::UnaryOperation &, const Clingo::AST::Term &term, const std::vector &) + void visit(const Clingo::AST::UnaryOperation &, const Clingo::AST::Term &term, Context &context) { - throwErrorAtLocation(term.location, "“unary operation” terms currently unsupported, function expected"); + throwErrorAtLocation(term.location, "“unary operation” terms currently unsupported, function expected", context); } - void visit(const Clingo::AST::BinaryOperation &, const Clingo::AST::Term &term, const std::vector &) + void visit(const Clingo::AST::BinaryOperation &, const Clingo::AST::Term &term, Context &context) { - throwErrorAtLocation(term.location, "“binary operation” terms currently unsupported, function expected"); + throwErrorAtLocation(term.location, "“binary operation” terms currently unsupported, function expected", context); } - void visit(const Clingo::AST::Interval &, const Clingo::AST::Term &term, const std::vector &) + void visit(const Clingo::AST::Interval &, const Clingo::AST::Term &term, Context &context) { - throwErrorAtLocation(term.location, "“interval” terms currently unsupported, function expected"); + throwErrorAtLocation(term.location, "“interval” terms currently unsupported, function expected", context); } - void visit(const Clingo::AST::Function &function, const Clingo::AST::Term &term, const std::vector &terms) + void visit(const Clingo::AST::Function &function, const Clingo::AST::Term &term, Context &context) { if (function.external) - throwErrorAtLocation(term.location, "external functions currently unsupported"); + throwErrorAtLocation(term.location, "external functions currently unsupported", context); std::cout << function.name; @@ -166,19 +166,19 @@ struct TermPrintSubstitutedVisitor const auto &argument = *i; - const auto matchingTerm = std::find(terms.cbegin(), terms.cend(), &argument); + const auto matchingTerm = std::find(context.headTerms.cbegin(), context.headTerms.cend(), &argument); assert(matchingTerm != terms.cend()); - std::cout << AuxiliaryHeadVariablePrefix << (matchingTerm - terms.cbegin()); + std::cout << AuxiliaryHeadVariablePrefix << (matchingTerm - context.headTerms.cbegin()); } std::cout << ")"; } - void visit(const Clingo::AST::Pool &, const Clingo::AST::Term &term, const std::vector &) + void visit(const Clingo::AST::Pool &, const Clingo::AST::Term &term, Context &context) { - throwErrorAtLocation(term.location, "“pool” terms currently unsupported, function expected"); + throwErrorAtLocation(term.location, "“pool” terms currently unsupported, function expected", context); } }; @@ -186,24 +186,24 @@ struct TermPrintSubstitutedVisitor struct LiteralPrintSubstitutedVisitor { - void visit(const Clingo::AST::Boolean &boolean, const Clingo::AST::Literal &, const std::vector &) + void visit(const Clingo::AST::Boolean &boolean, const Clingo::AST::Literal &, Context &context) { std::cout << (boolean.value == true ? "true" : "false"); } - void visit(const Clingo::AST::Term &term, const Clingo::AST::Literal &, const std::vector &terms) + void visit(const Clingo::AST::Term &term, const Clingo::AST::Literal &, Context &context) { - term.data.accept(TermPrintSubstitutedVisitor(), term, terms); + term.data.accept(TermPrintSubstitutedVisitor(), term, context); } - void visit(const Clingo::AST::Comparison &, const Clingo::AST::Literal &literal, const std::vector &) + void visit(const Clingo::AST::Comparison &, const Clingo::AST::Literal &literal, Context &context) { - throwErrorAtLocation(literal.location, "only disjunctions of literals allowed as head literals"); + throwErrorAtLocation(literal.location, "only disjunctions of literals allowed as head literals", context); } - void visit(const Clingo::AST::CSPLiteral &, const Clingo::AST::Literal &literal, const std::vector &) + void visit(const Clingo::AST::CSPLiteral &, const Clingo::AST::Literal &literal, Context &context) { - throwErrorAtLocation(literal.location, "only disjunctions of literals allowed as head literals"); + throwErrorAtLocation(literal.location, "only disjunctions of literals allowed as head literals", context); } }; @@ -211,45 +211,45 @@ struct LiteralPrintSubstitutedVisitor struct HeadLiteralPrintSubstitutedVisitor { - void visit(const Clingo::AST::Literal &literal, const Clingo::AST::HeadLiteral &, const std::vector &terms) + void visit(const Clingo::AST::Literal &literal, const Clingo::AST::HeadLiteral &, Context &context) { if (literal.sign == Clingo::AST::Sign::DoubleNegation) - throwErrorAtLocation(literal.location, "double-negated literals currently unsupported"); + throwErrorAtLocation(literal.location, "double-negated literals currently unsupported", context); else if (literal.sign == Clingo::AST::Sign::Negation) std::cout << "not "; - literal.data.accept(LiteralPrintSubstitutedVisitor(), literal, terms); + literal.data.accept(LiteralPrintSubstitutedVisitor(), literal, context); } - void visit(const Clingo::AST::Disjunction &disjunction, const Clingo::AST::HeadLiteral &headLiteral, const std::vector &terms) + void visit(const Clingo::AST::Disjunction &disjunction, const Clingo::AST::HeadLiteral &headLiteral, Context &context) { for (auto i = disjunction.elements.cbegin(); i != disjunction.elements.cend(); i++) { const auto &conditionLiteral = *i; if (!conditionLiteral.condition.empty()) - throwErrorAtLocation(headLiteral.location, "conditional head literals currently unsupported"); + throwErrorAtLocation(headLiteral.location, "conditional head literals currently unsupported", context); if (i != disjunction.elements.cbegin()) std::cout << " or "; - visit(conditionLiteral.literal, headLiteral, terms); + visit(conditionLiteral.literal, headLiteral, context); } } - void visit(const Clingo::AST::Aggregate &, const Clingo::AST::HeadLiteral &headLiteral, const std::vector &) + void visit(const Clingo::AST::Aggregate &, const Clingo::AST::HeadLiteral &headLiteral, Context &context) { - throwErrorAtLocation(headLiteral.location, "“aggregate” head literals currently unsupported"); + throwErrorAtLocation(headLiteral.location, "“aggregate” head literals currently unsupported", context); } - void visit(const Clingo::AST::HeadAggregate &, const Clingo::AST::HeadLiteral &headLiteral, const std::vector &) + void visit(const Clingo::AST::HeadAggregate &, const Clingo::AST::HeadLiteral &headLiteral, Context &context) { - throwErrorAtLocation(headLiteral.location, "“head aggregate” head literals currently unsupported"); + throwErrorAtLocation(headLiteral.location, "“head aggregate” head literals currently unsupported", context); } - void visit(const Clingo::AST::TheoryAtom &, const Clingo::AST::HeadLiteral &headLiteral, const std::vector &) + void visit(const Clingo::AST::TheoryAtom &, const Clingo::AST::HeadLiteral &headLiteral, Context &context) { - throwErrorAtLocation(headLiteral.location, "“theory” head literals currently unsupported"); + throwErrorAtLocation(headLiteral.location, "“theory” head literals currently unsupported", context); } }; diff --git a/include/anthem/StatementVisitor.h b/include/anthem/StatementVisitor.h index 7930053..0567e98 100644 --- a/include/anthem/StatementVisitor.h +++ b/include/anthem/StatementVisitor.h @@ -14,52 +14,38 @@ namespace anthem // //////////////////////////////////////////////////////////////////////////////////////////////////// -void throwErrorUnsupportedStatement(const char *statementType, const Clingo::AST::Statement &statement) -{ - const auto errorMessage = std::string("“") + statementType + "” statements currently unsupported"; - - throwErrorAtLocation(statement.location, errorMessage.c_str()); - - throw std::runtime_error(errorMessage); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// - struct StatementVisitor { - void visit(const Clingo::AST::Program &program, const Clingo::AST::Statement &statement) + void visit(const Clingo::AST::Program &program, const Clingo::AST::Statement &statement, Context &context) { std::cout << "[program] " << program.name << std::endl; if (!program.parameters.empty()) - throwErrorAtLocation(statement.location, "program parameters currently unsupported"); + throwErrorAtLocation(statement.location, "program parameters currently unsupported", context); } - void visit(const Clingo::AST::Rule &rule, const Clingo::AST::Statement &) + void visit(const Clingo::AST::Rule &rule, const Clingo::AST::Statement &, Context &context) { - Context context; - // Concatenate all head terms - std::vector headTerms; - rule.head.data.accept(HeadLiteralCollectFunctionTermsVisitor(), rule.head, headTerms); + rule.head.data.accept(HeadLiteralCollectFunctionTermsVisitor(), rule.head, context); // Print auxiliary variables replacing the head atom’s arguments - if (!headTerms.empty()) + if (!context.headTerms.empty()) { - for (auto i = headTerms.cbegin(); i != headTerms.cend(); i++) + for (auto i = context.headTerms.cbegin(); i != context.headTerms.cend(); i++) { const auto &headTerm = **i; - if (i != headTerms.cbegin()) + if (i != context.headTerms.cbegin()) std::cout << ", "; std::cout - << AuxiliaryHeadVariablePrefix << (i - headTerms.cbegin()) + << AuxiliaryHeadVariablePrefix << (i - context.headTerms.cbegin()) << " in " << headTerm; } } - if (rule.body.empty() && headTerms.empty()) + if (rule.body.empty() && context.headTerms.empty()) std::cout << "true"; else { @@ -68,11 +54,11 @@ struct StatementVisitor { const auto &bodyLiteral = *i; - if (!headTerms.empty()) + if (!context.headTerms.empty()) std::cout << " and "; if (bodyLiteral.sign != Clingo::AST::Sign::None) - throwErrorAtLocation(bodyLiteral.location, "only positive literals currently supported"); + throwErrorAtLocation(bodyLiteral.location, "only positive literals currently supported", context); bodyLiteral.data.accept(BodyLiteralPrintVisitor(), bodyLiteral, context); } @@ -81,62 +67,62 @@ struct StatementVisitor std::cout << " -> "; // Print consequent of the implication - rule.head.data.accept(HeadLiteralPrintSubstitutedVisitor(), rule.head, headTerms); + rule.head.data.accept(HeadLiteralPrintSubstitutedVisitor(), rule.head, context); } - void visit(const Clingo::AST::Definition &, const Clingo::AST::Statement &statement) + void visit(const Clingo::AST::Definition &, const Clingo::AST::Statement &statement, Context &context) { - throwErrorUnsupportedStatement("definition", statement); + throwErrorAtLocation(statement.location, "“definition” statements currently unsupported", context); } - void visit(const Clingo::AST::ShowSignature &, const Clingo::AST::Statement &statement) + void visit(const Clingo::AST::ShowSignature &, const Clingo::AST::Statement &statement, Context &context) { - throwErrorUnsupportedStatement("show signature", statement); + throwErrorAtLocation(statement.location, "“show signature” statements currently unsupported", context); } - void visit(const Clingo::AST::ShowTerm &, const Clingo::AST::Statement &statement) + void visit(const Clingo::AST::ShowTerm &, const Clingo::AST::Statement &statement, Context &context) { - throwErrorUnsupportedStatement("show term", statement); + throwErrorAtLocation(statement.location, "“show term” statements currently unsupported", context); } - void visit(const Clingo::AST::Minimize &, const Clingo::AST::Statement &statement) + void visit(const Clingo::AST::Minimize &, const Clingo::AST::Statement &statement, Context &context) { - throwErrorUnsupportedStatement("minimize", statement); + throwErrorAtLocation(statement.location, "“minimize” statements currently unsupported", context); } - void visit(const Clingo::AST::Script &, const Clingo::AST::Statement &statement) + void visit(const Clingo::AST::Script &, const Clingo::AST::Statement &statement, Context &context) { - throwErrorUnsupportedStatement("script", statement); + throwErrorAtLocation(statement.location, "“script” statements currently unsupported", context); } - void visit(const Clingo::AST::External &, const Clingo::AST::Statement &statement) + void visit(const Clingo::AST::External &, const Clingo::AST::Statement &statement, Context &context) { - throwErrorUnsupportedStatement("external", statement); + throwErrorAtLocation(statement.location, "“external” statements currently unsupported", context); } - void visit(const Clingo::AST::Edge &, const Clingo::AST::Statement &statement) + void visit(const Clingo::AST::Edge &, const Clingo::AST::Statement &statement, Context &context) { - throwErrorUnsupportedStatement("edge", statement); + throwErrorAtLocation(statement.location, "“edge” statements currently unsupported", context); } - void visit(const Clingo::AST::Heuristic &, const Clingo::AST::Statement &statement) + void visit(const Clingo::AST::Heuristic &, const Clingo::AST::Statement &statement, Context &context) { - throwErrorUnsupportedStatement("heuristic", statement); + throwErrorAtLocation(statement.location, "“heuristic” statements currently unsupported", context); } - void visit(const Clingo::AST::ProjectAtom &, const Clingo::AST::Statement &statement) + void visit(const Clingo::AST::ProjectAtom &, const Clingo::AST::Statement &statement, Context &context) { - throwErrorUnsupportedStatement("project atom", statement); + throwErrorAtLocation(statement.location, "“project atom” statements currently unsupported", context); } - void visit(const Clingo::AST::ProjectSignature &, const Clingo::AST::Statement &statement) + void visit(const Clingo::AST::ProjectSignature &, const Clingo::AST::Statement &statement, Context &context) { - throwErrorUnsupportedStatement("project signature", statement); + throwErrorAtLocation(statement.location, "“project signature” statements currently unsupported", context); } - void visit(const Clingo::AST::TheoryDefinition &, const Clingo::AST::Statement &statement) + void visit(const Clingo::AST::TheoryDefinition &, const Clingo::AST::Statement &statement, Context &context) { - throwErrorUnsupportedStatement("theory definition", statement); + throwErrorAtLocation(statement.location, "“theory definition” statements currently unsupported", context); } }; diff --git a/include/anthem/Utils.h b/include/anthem/Utils.h index 929364d..f4e425e 100644 --- a/include/anthem/Utils.h +++ b/include/anthem/Utils.h @@ -5,6 +5,9 @@ #include +#include +#include + namespace anthem { @@ -14,13 +17,26 @@ namespace anthem // //////////////////////////////////////////////////////////////////////////////////////////////////// -inline void throwErrorAtLocation(const Clingo::Location &location, const char *errorMessage) +template +T1 location_cast(const T2 &location); + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +template<> +input::Location location_cast(const Clingo::Location &location) { - std::cerr - << location.begin_file() << ":" - << location.begin_line() << ":" << location.begin_column() - << ": error: " - << errorMessage << std::endl; + return {location.begin_file(), location.end_file(), location.begin_line(), location.end_line(), + location.begin_column(), location.end_column()}; +} + +//////////////////////////////////////////////////////////////////////////////////////////////////// + +inline void throwErrorAtLocation(const Clingo::Location &clingoLocation, const char *errorMessage, + Context &context) +{ + const auto location = location_cast(clingoLocation); + + context.logger.log(output::Priority::Error, location, errorMessage); throw std::runtime_error(errorMessage); } diff --git a/include/anthem/input/Location.h b/include/anthem/input/Location.h index e15b337..7beb227 100644 --- a/include/anthem/input/Location.h +++ b/include/anthem/input/Location.h @@ -1,6 +1,8 @@ #ifndef __ANTHEM__OUTPUT__LOCATION_H #define __ANTHEM__OUTPUT__LOCATION_H +#include + namespace anthem { namespace input @@ -17,11 +19,11 @@ struct Location const char *sectionStart = nullptr; const char *sectionEnd = nullptr; - int rowStart = -1; - int rowEnd = -1; + std::size_t rowStart = -1; + std::size_t rowEnd = -1; - int columnStart = -1; - int columnEnd = -1; + std::size_t columnStart = -1; + std::size_t columnEnd = -1; }; //////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/src/anthem/Translation.cpp b/src/anthem/Translation.cpp index e902887..99a68cb 100644 --- a/src/anthem/Translation.cpp +++ b/src/anthem/Translation.cpp @@ -6,6 +6,7 @@ #include +#include #include namespace anthem @@ -35,10 +36,12 @@ void translate(const char *fileName, std::istream &stream) auto fileContent = std::string(std::istreambuf_iterator(stream), {}); + Context context; + const auto translateStatement = - [](const Clingo::AST::Statement &statement) + [&context](const Clingo::AST::Statement &statement) { - statement.data.accept(StatementVisitor(), statement); + statement.data.accept(StatementVisitor(), statement, context); std::cout << std::endl; };