Support modulus operation (absolute value)
This adds support for computing the absolute value of a term along with an according unit test.
This commit is contained in:
@@ -293,6 +293,30 @@ struct String
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
struct UnaryOperation
|
||||
{
|
||||
enum class Operator
|
||||
{
|
||||
Absolute
|
||||
};
|
||||
|
||||
explicit UnaryOperation(Operator operator_, Term &&argument)
|
||||
: operator_{operator_},
|
||||
argument{std::move(argument)}
|
||||
{
|
||||
}
|
||||
|
||||
UnaryOperation(const UnaryOperation &other) = delete;
|
||||
UnaryOperation &operator=(const UnaryOperation &other) = delete;
|
||||
UnaryOperation(UnaryOperation &&other) noexcept = default;
|
||||
UnaryOperation &operator=(UnaryOperation &&other) noexcept = default;
|
||||
|
||||
Operator operator_;
|
||||
Term argument;
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
struct Variable
|
||||
{
|
||||
explicit Variable(VariableDeclaration *declaration)
|
||||
|
@@ -37,6 +37,7 @@ struct Or;
|
||||
struct Predicate;
|
||||
struct SpecialInteger;
|
||||
struct String;
|
||||
struct UnaryOperation;
|
||||
struct Variable;
|
||||
struct VariableDeclaration;
|
||||
using VariableDeclarationPointer = std::unique_ptr<VariableDeclaration>;
|
||||
@@ -68,6 +69,7 @@ using Term = Clingo::Variant<
|
||||
Interval,
|
||||
SpecialInteger,
|
||||
String,
|
||||
UnaryOperation,
|
||||
Variable>;
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
@@ -165,6 +165,14 @@ struct RecursiveTermVisitor
|
||||
return T::accept(string, term, std::forward<Arguments>(arguments)...);
|
||||
}
|
||||
|
||||
template <class... Arguments>
|
||||
ReturnType visit(UnaryOperation &unaryOperation, Term &term, Arguments &&... arguments)
|
||||
{
|
||||
unaryOperation.argument.accept(*this, unaryOperation.argument, std::forward<Arguments>(arguments)...);
|
||||
|
||||
return T::accept(unaryOperation, term, std::forward<Arguments>(arguments)...);
|
||||
}
|
||||
|
||||
template <class... Arguments>
|
||||
ReturnType visit(Variable &variable, Term &term, Arguments &&... arguments)
|
||||
{
|
||||
|
@@ -382,6 +382,19 @@ struct TermEqualityVisitor
|
||||
: Tristate::False;
|
||||
}
|
||||
|
||||
Tristate visit(const UnaryOperation &unaryOperation, const Term &otherTerm)
|
||||
{
|
||||
if (!otherTerm.is<UnaryOperation>())
|
||||
return Tristate::Unknown;
|
||||
|
||||
const auto &otherUnaryOperation = otherTerm.get<UnaryOperation>();
|
||||
|
||||
if (unaryOperation.operator_ != otherUnaryOperation.operator_)
|
||||
return Tristate::Unknown;
|
||||
|
||||
return equal(unaryOperation.argument, otherUnaryOperation.argument);
|
||||
}
|
||||
|
||||
Tristate visit(const Variable &variable, const Term &otherTerm)
|
||||
{
|
||||
if (!otherTerm.is<Variable>())
|
||||
|
@@ -48,6 +48,23 @@ ast::BinaryOperation::Operator translate(Clingo::AST::BinaryOperator binaryOpera
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
ast::UnaryOperation::Operator translate(Clingo::AST::UnaryOperator unaryOperator, const Clingo::AST::Term &term)
|
||||
{
|
||||
switch (unaryOperator)
|
||||
{
|
||||
case Clingo::AST::UnaryOperator::Absolute:
|
||||
return ast::UnaryOperation::Operator::Absolute;
|
||||
case Clingo::AST::UnaryOperator::Minus:
|
||||
throw TranslationException(term.location, "binary operation “minus” currently unsupported");
|
||||
case Clingo::AST::UnaryOperator::Negation:
|
||||
throw TranslationException(term.location, "binary operation “negation” currently unsupported");
|
||||
}
|
||||
|
||||
throw TranslationException(term.location, "unknown unary operation");
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
ast::Term translate(const Clingo::AST::Term &term, RuleContext &ruleContext, const ast::VariableStack &variableStack);
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
@@ -106,12 +123,6 @@ struct TermTranslateVisitor
|
||||
return ast::Term::make<ast::Variable>(ruleContext.freeVariables.back().get());
|
||||
}
|
||||
|
||||
std::optional<ast::Term> visit(const Clingo::AST::UnaryOperation &, const Clingo::AST::Term &term, RuleContext &, const ast::VariableStack &)
|
||||
{
|
||||
throw TranslationException(term.location, "“unary operation” terms currently unsupported");
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
std::optional<ast::Term> visit(const Clingo::AST::BinaryOperation &binaryOperation, const Clingo::AST::Term &term, RuleContext &ruleContext, const ast::VariableStack &variableStack)
|
||||
{
|
||||
const auto operator_ = translate(binaryOperation.binary_operator, term);
|
||||
@@ -121,6 +132,14 @@ struct TermTranslateVisitor
|
||||
return ast::Term::make<ast::BinaryOperation>(operator_, std::move(left), std::move(right));
|
||||
}
|
||||
|
||||
std::optional<ast::Term> visit(const Clingo::AST::UnaryOperation &unaryOperation, const Clingo::AST::Term &term, RuleContext &ruleContext, const ast::VariableStack &variableStack)
|
||||
{
|
||||
const auto operator_ = translate(unaryOperation.unary_operator, term);
|
||||
auto argument = translate(unaryOperation.argument, ruleContext, variableStack);
|
||||
|
||||
return ast::Term::make<ast::UnaryOperation>(operator_, std::move(argument));
|
||||
}
|
||||
|
||||
std::optional<ast::Term> visit(const Clingo::AST::Interval &interval, const Clingo::AST::Term &, RuleContext &ruleContext, const ast::VariableStack &variableStack)
|
||||
{
|
||||
auto left = translate(interval.left, ruleContext, variableStack);
|
||||
|
@@ -53,6 +53,7 @@ output::ColorStream &print(output::ColorStream &stream, const Interval &interval
|
||||
output::ColorStream &print(output::ColorStream &stream, const Predicate &predicate, PrintContext &printContext);
|
||||
output::ColorStream &print(output::ColorStream &stream, const SpecialInteger &specialInteger, PrintContext &printContext);
|
||||
output::ColorStream &print(output::ColorStream &stream, const String &string, PrintContext &printContext);
|
||||
output::ColorStream &print(output::ColorStream &stream, const UnaryOperation &unaryOperation, PrintContext &printContext);
|
||||
output::ColorStream &print(output::ColorStream &stream, const Variable &variable, PrintContext &printContext);
|
||||
output::ColorStream &print(output::ColorStream &stream, const VariableDeclaration &variableDeclaration, PrintContext &printContext);
|
||||
|
||||
@@ -282,6 +283,29 @@ inline output::ColorStream &print(output::ColorStream &stream, const String &str
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
inline output::ColorStream &print(output::ColorStream &stream, const UnaryOperation &unaryOperation, PrintContext &printContext)
|
||||
{
|
||||
switch (unaryOperation.operator_)
|
||||
{
|
||||
case UnaryOperation::Operator::Absolute:
|
||||
stream << "|";
|
||||
break;
|
||||
}
|
||||
|
||||
print(stream, unaryOperation.argument, printContext);
|
||||
|
||||
switch (unaryOperation.operator_)
|
||||
{
|
||||
case UnaryOperation::Operator::Absolute:
|
||||
stream << "|";
|
||||
break;
|
||||
}
|
||||
|
||||
return stream;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
inline output::ColorStream &print(output::ColorStream &stream, const Variable &variable, PrintContext &printContext)
|
||||
{
|
||||
assert(variable.declaration != nullptr);
|
||||
|
Reference in New Issue
Block a user