patrick
/
plasp
Archived
1
0
Fork 0
This repository has been archived on 2023-07-19. You can view files and clone it, but cannot push or open issues or pull requests.
plasp/include/plasp/pddl/expressions/Binary.h

112 lines
3.0 KiB
C
Raw Normal View History

2016-06-04 17:20:25 +02:00
#ifndef __PLASP__PDDL__EXPRESSIONS__BINARY_H
#define __PLASP__PDDL__EXPRESSIONS__BINARY_H
#include <plasp/pddl/ConsistencyException.h>
#include <plasp/pddl/Context.h>
#include <plasp/pddl/Expression.h>
#include <plasp/pddl/expressions/Variable.h>
namespace plasp
{
namespace pddl
{
namespace expressions
{
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Binary
//
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Derived>
class Binary: public ExpressionCRTP<Derived>
2016-06-04 17:20:25 +02:00
{
public:
template<typename ExpressionParser>
static std::unique_ptr<Derived> parse(Context &context,
ExpressionContext &expressionContext, ExpressionParser parseExpression);
2016-06-04 17:20:25 +02:00
public:
2016-06-13 01:38:56 +02:00
const std::array<const Expression *, 2> &arguments() const;
2016-06-04 17:20:25 +02:00
private:
2016-06-13 01:38:56 +02:00
template<size_t i>
void setArgument(const Expression *argument);
template<size_t i>
void setArgument(ExpressionPointer &&argument);
2016-06-13 01:38:56 +02:00
std::array<const Expression *, 2> m_arguments;
std::array<ExpressionPointer, 2> m_argumentStorage;
2016-06-04 17:20:25 +02:00
};
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Derived>
2016-06-04 17:20:25 +02:00
template<typename ExpressionParser>
std::unique_ptr<Derived> Binary<Derived>::parse(Context &context,
ExpressionContext &expressionContext, ExpressionParser parseExpression)
2016-06-04 17:20:25 +02:00
{
auto &parser = context.parser;
const auto position = parser.position();
if (!parser.testAndSkip<std::string>("(")
|| !parser.testIdentifierAndSkip(Derived::Identifier))
{
parser.seek(position);
return nullptr;
}
auto expression = std::make_unique<Derived>();
2016-06-04 17:20:25 +02:00
// Assume that expression identifier (imply, exists, etc.) is already parsed
// Parse arguments of the expression
expression->Binary<Derived>::setArgument<0>(parseExpression(context, expressionContext));
expression->Binary<Derived>::setArgument<1>(parseExpression(context, expressionContext));
parser.expect<std::string>(")");
return expression;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Derived>
2016-06-13 01:38:56 +02:00
template<size_t i>
void Binary<Derived>::setArgument(const Expression *expression)
{
2016-06-13 01:38:56 +02:00
static_assert(i <= 2, "Index out of range");
m_argumentStorage[i] = nullptr;
m_arguments[i] = expression;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Derived>
template<size_t i>
void Binary<Derived>::setArgument(ExpressionPointer &&expression)
{
static_assert(i <= 2, "Index out of range");
m_argumentStorage[i] = std::move(expression);
m_arguments[i] = m_argumentStorage[i].get();
}
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Derived>
2016-06-13 01:38:56 +02:00
const std::array<const Expression *, 2> &Binary<Derived>::arguments() const
{
2016-06-13 01:38:56 +02:00
return m_arguments;
2016-06-04 17:20:25 +02:00
}
////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
}
#endif