Implemented translation of anonymous variables.
This commit is contained in:
@@ -22,6 +22,7 @@ struct Context
|
||||
isChoiceRule = false;
|
||||
numberOfHeadLiterals = 0;
|
||||
auxiliaryBodyVariableID = 1;
|
||||
anonymousVariableID = 1;
|
||||
}
|
||||
|
||||
output::Logger logger;
|
||||
@@ -30,6 +31,7 @@ struct Context
|
||||
bool isChoiceRule = false;
|
||||
size_t numberOfHeadLiterals = 0;
|
||||
size_t auxiliaryBodyVariableID = 1;
|
||||
size_t anonymousVariableID = 1;
|
||||
|
||||
bool simplify = false;
|
||||
};
|
||||
|
@@ -83,8 +83,16 @@ struct TermTranslateVisitor
|
||||
return std::experimental::nullopt;
|
||||
}
|
||||
|
||||
std::experimental::optional<ast::Term> visit(const Clingo::AST::Variable &variable, const Clingo::AST::Term &, Context &)
|
||||
std::experimental::optional<ast::Term> visit(const Clingo::AST::Variable &variable, const Clingo::AST::Term &, Context &context)
|
||||
{
|
||||
if (strcmp(variable.name, "_") == 0)
|
||||
{
|
||||
std::string variableName = AnonymousVariablePrefix + std::to_string(context.anonymousVariableID);
|
||||
context.anonymousVariableID++;
|
||||
|
||||
return ast::Term::make<ast::Variable>(std::move(variableName), ast::Variable::Type::Reserved);
|
||||
}
|
||||
|
||||
return ast::Term::make<ast::Variable>(std::string(variable.name), ast::Variable::Type::UserDefined);
|
||||
}
|
||||
|
||||
|
@@ -58,6 +58,7 @@ inline bool isPrefix(const char *prefix, const char *string)
|
||||
|
||||
constexpr const auto AuxiliaryHeadVariablePrefix = "V";
|
||||
constexpr const auto AuxiliaryBodyVariablePrefix = "X";
|
||||
constexpr const auto AnonymousVariablePrefix = "A";
|
||||
constexpr const auto UserVariablePrefix = "_";
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
@@ -65,12 +66,14 @@ constexpr const auto UserVariablePrefix = "_";
|
||||
inline bool isReservedVariableName(const char *variableName)
|
||||
{
|
||||
if (!isPrefix(AuxiliaryBodyVariablePrefix, variableName)
|
||||
&& !isPrefix(AuxiliaryHeadVariablePrefix, variableName))
|
||||
&& !isPrefix(AuxiliaryHeadVariablePrefix, variableName)
|
||||
&& !isPrefix(AnonymousVariablePrefix, variableName))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
assert(std::strlen(AuxiliaryBodyVariablePrefix) == std::strlen(AuxiliaryHeadVariablePrefix));
|
||||
assert(std::strlen(AuxiliaryBodyVariablePrefix) == std::strlen(AnonymousVariablePrefix));
|
||||
|
||||
const auto prefixLength = std::strlen(AuxiliaryBodyVariablePrefix);
|
||||
|
||||
|
@@ -216,6 +216,7 @@ inline output::ColorStream &operator<<(output::ColorStream &stream, const String
|
||||
inline output::ColorStream &operator<<(output::ColorStream &stream, const Variable &variable)
|
||||
{
|
||||
assert(!variable.name.empty());
|
||||
assert(variable.name != "_");
|
||||
|
||||
if (variable.type == ast::Variable::Type::Reserved || !isReservedVariableName(variable.name.c_str()))
|
||||
return (stream << output::Variable(variable.name.c_str()));
|
||||
|
Reference in New Issue
Block a user