From 2bc60d3eeac1ffd5f5bae8e6a2ac4cf0e6b8e719 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20L=C3=BChne?= Date: Thu, 1 Jun 2017 04:05:11 +0200 Subject: [PATCH] Started implementing support for #show statements. --- include/anthem/AST.h | 21 ++++++++++++++++++++- include/anthem/Context.h | 5 +++++ include/anthem/StatementVisitor.h | 28 +++++++++++++++++++++++++++- src/anthem/Translation.cpp | 2 ++ 4 files changed, 54 insertions(+), 2 deletions(-) diff --git a/include/anthem/AST.h b/include/anthem/AST.h index d256a79..4e29756 100644 --- a/include/anthem/AST.h +++ b/include/anthem/AST.h @@ -201,7 +201,6 @@ struct Interval //////////////////////////////////////////////////////////////////////////////////////////////////// -// TODO: implement declaration/signature struct Predicate { explicit Predicate(std::string &&name) @@ -231,6 +230,26 @@ struct Predicate //////////////////////////////////////////////////////////////////////////////////////////////////// +// TODO: make more use of this class +struct PredicateSignature +{ + explicit PredicateSignature(std::string &&name, size_t arity) + : name{std::move(name)}, + arity{arity} + { + } + + PredicateSignature(const PredicateSignature &other) = delete; + PredicateSignature &operator=(const PredicateSignature &other) = delete; + PredicateSignature(PredicateSignature &&other) noexcept = default; + PredicateSignature &operator=(PredicateSignature &&other) noexcept = default; + + std::string name; + size_t arity; +}; + +//////////////////////////////////////////////////////////////////////////////////////////////////// + struct SpecialInteger { enum class Type diff --git a/include/anthem/Context.h b/include/anthem/Context.h index fdf1f44..a2c7d07 100644 --- a/include/anthem/Context.h +++ b/include/anthem/Context.h @@ -1,6 +1,9 @@ #ifndef __ANTHEM__CONTEXT_H #define __ANTHEM__CONTEXT_H +#include + +#include #include namespace anthem @@ -25,6 +28,8 @@ struct Context bool simplify = false; bool complete = false; + + std::experimental::optional> visiblePredicateSignatures; }; //////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/include/anthem/StatementVisitor.h b/include/anthem/StatementVisitor.h index d47004f..1d16980 100644 --- a/include/anthem/StatementVisitor.h +++ b/include/anthem/StatementVisitor.h @@ -150,10 +150,36 @@ struct StatementVisitor } } + void visit(const Clingo::AST::ShowSignature &showSignature, const Clingo::AST::Statement &statement, std::vector &, Context &context) + { + if (showSignature.csp) + throw LogicException(statement.location, "CSP #show statements are not supported"); + + auto &signature = showSignature.signature; + + if (signature.negative()) + throw LogicException(statement.location, "negative #show atom signatures are currently unsupported"); + + if (!context.visiblePredicateSignatures) + context.visiblePredicateSignatures.emplace(); + + if (std::strlen(signature.name()) == 0) + return; + + context.logger.log(output::Priority::Debug) << "showing “" << signature.name() << "/" << signature.arity() << "”"; + + context.visiblePredicateSignatures.value().emplace_back(std::string(signature.name()), signature.arity()); + } + + void visit(const Clingo::AST::ShowTerm &, const Clingo::AST::Statement &statement, std::vector &, Context &) + { + throw LogicException(statement.location, "only #show statements for atoms (not terms) are supported currently"); + } + template void visit(const T &, const Clingo::AST::Statement &statement, std::vector &, Context &) { - throw LogicException(statement.location, "statement currently unsupported, expected rule"); + throw LogicException(statement.location, "statement currently unsupported"); } }; diff --git a/src/anthem/Translation.cpp b/src/anthem/Translation.cpp index ab46e0f..3223179 100644 --- a/src/anthem/Translation.cpp +++ b/src/anthem/Translation.cpp @@ -64,6 +64,8 @@ void translate(const char *fileName, std::istream &stream, Context &context) ast::PrintContext printContext; + // TODO: respect predicate visibility in output + if (!context.complete) { for (const auto &scopedFormula : scopedFormulas)