Big refactoring (build still broken).
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
|
||||
#include <plasp/pddl/Context.h>
|
||||
#include <plasp/pddl/Identifier.h>
|
||||
#include <plasp/pddl/expressions/Type.h>
|
||||
#include <plasp/utils/ParserException.h>
|
||||
|
||||
namespace plasp
|
||||
|
@@ -4,6 +4,10 @@
|
||||
|
||||
#include <plasp/pddl/ConsistencyException.h>
|
||||
#include <plasp/pddl/Identifier.h>
|
||||
#include <plasp/pddl/expressions/Constant.h>
|
||||
#include <plasp/pddl/expressions/PredicateDeclaration.h>
|
||||
#include <plasp/pddl/expressions/PrimitiveType.h>
|
||||
#include <plasp/pddl/expressions/Variable.h>
|
||||
#include <plasp/utils/ParserException.h>
|
||||
|
||||
namespace plasp
|
||||
@@ -67,23 +71,23 @@ const Requirements &Domain::requirements() const
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
const std::vector<std::unique_ptr<PrimitiveType>> &Domain::types() const
|
||||
const expressions::PrimitiveTypes &Domain::types() const
|
||||
{
|
||||
return m_context.primitiveTypes;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
const std::vector<std::unique_ptr<Constant>> &Domain::constants() const
|
||||
const expressions::Constants &Domain::constants() const
|
||||
{
|
||||
return m_context.constants;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
const std::vector<std::unique_ptr<Predicate>> &Domain::predicates() const
|
||||
const expressions::PredicateDeclarations &Domain::predicates() const
|
||||
{
|
||||
return m_context.predicates;
|
||||
return m_context.predicateDeclarations;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
@@ -237,7 +241,7 @@ void Domain::parseTypeSection(utils::Parser &parser)
|
||||
if (parser.currentCharacter() == '(')
|
||||
throw utils::ParserException(parser.row(), parser.column(), "Only primitive types are allowed in type section");
|
||||
|
||||
PrimitiveType::parseDeclaration(parser, m_context);
|
||||
expressions::PrimitiveType::parseTypedDeclaration(parser, m_context);
|
||||
|
||||
parser.skipWhiteSpace();
|
||||
}
|
||||
@@ -254,7 +258,7 @@ void Domain::parseConstantSection(utils::Parser &parser)
|
||||
// Store constants
|
||||
while (parser.currentCharacter() != ')')
|
||||
{
|
||||
Constant::parseDeclaration(parser, m_context);
|
||||
expressions::Constant::parseDeclaration(parser, m_context);
|
||||
|
||||
parser.skipWhiteSpace();
|
||||
}
|
||||
@@ -271,7 +275,7 @@ void Domain::parsePredicateSection(utils::Parser &parser)
|
||||
// Store predicates and their arguments
|
||||
while (parser.currentCharacter() != ')')
|
||||
{
|
||||
Predicate::parseDeclaration(parser, m_context);
|
||||
expressions::PredicateDeclaration::parse(parser, m_context);
|
||||
|
||||
parser.skipWhiteSpace();
|
||||
}
|
||||
@@ -322,7 +326,7 @@ void Domain::checkConsistency()
|
||||
});
|
||||
|
||||
// Verify that all used predicates have been declared
|
||||
std::for_each(m_context.predicates.cbegin(), m_context.predicates.cend(),
|
||||
std::for_each(m_context.predicateDeclarations.cbegin(), m_context.predicateDeclarations.cend(),
|
||||
[&](const auto &predicate)
|
||||
{
|
||||
if (!predicate->isDeclared())
|
||||
@@ -330,6 +334,8 @@ void Domain::checkConsistency()
|
||||
});
|
||||
|
||||
// Verify that all variables have types
|
||||
// Verify that constants are unique
|
||||
// Verify that all primitive types are unique
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
@@ -1,52 +0,0 @@
|
||||
#include <plasp/pddl/EitherType.h>
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
#include <plasp/pddl/Context.h>
|
||||
#include <plasp/pddl/Identifier.h>
|
||||
|
||||
namespace plasp
|
||||
{
|
||||
namespace pddl
|
||||
{
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// EitherType
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
EitherType &EitherType::parse(utils::Parser &parser, Context &context)
|
||||
{
|
||||
parser.skipWhiteSpace();
|
||||
|
||||
auto eitherType = std::make_unique<EitherType>(EitherType());
|
||||
|
||||
parser.expect<std::string>("(either");
|
||||
parser.skipWhiteSpace();
|
||||
|
||||
while (parser.currentCharacter() != ')')
|
||||
{
|
||||
eitherType->m_allowedTypes.push_back(&PrimitiveType::parse(parser, context));
|
||||
|
||||
parser.skipWhiteSpace();
|
||||
}
|
||||
|
||||
context.eitherTypes.emplace_back(std::move(eitherType));
|
||||
|
||||
parser.expect<std::string>(")");
|
||||
|
||||
return *context.eitherTypes.back();
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
const std::vector<const PrimitiveType *> &EitherType::allowedTypes() const
|
||||
{
|
||||
return m_allowedTypes;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
}
|
||||
}
|
@@ -6,6 +6,8 @@
|
||||
#include <plasp/pddl/expressions/Not.h>
|
||||
#include <plasp/pddl/expressions/Or.h>
|
||||
#include <plasp/pddl/expressions/Predicate.h>
|
||||
#include <plasp/pddl/expressions/PredicateDeclaration.h>
|
||||
#include <plasp/pddl/expressions/Reference.h>
|
||||
#include <plasp/utils/ParserException.h>
|
||||
|
||||
namespace plasp
|
||||
@@ -113,14 +115,14 @@ ExpressionPointer parseExpressionContent(const std::string &expressionIdentifier
|
||||
else
|
||||
{
|
||||
// Check if predicate with that name exists
|
||||
const auto match = std::find_if(context.predicates.cbegin(), context.predicates.cend(),
|
||||
const auto match = std::find_if(context.predicateDeclarations.cbegin(), context.predicateDeclarations.cend(),
|
||||
[&](const auto &predicate)
|
||||
{
|
||||
return predicate->name() == expressionIdentifier;
|
||||
});
|
||||
|
||||
// If predicate exists, parse it
|
||||
if (match != context.predicates.cend())
|
||||
if (match != context.predicateDeclarations.cend())
|
||||
expression = expressions::Predicate::parse(expressionIdentifier, parser, context, parameters);
|
||||
else
|
||||
throw utils::ParserException(parser.row(), parser.column(), "Expression \"" + expressionIdentifier + "\" not allowed in this context");
|
||||
@@ -176,14 +178,14 @@ ExpressionPointer parseEffectBodyExpressionContent(const std::string &expression
|
||||
else
|
||||
{
|
||||
// Check if predicate with that name exists
|
||||
const auto match = std::find_if(context.predicates.cbegin(), context.predicates.cend(),
|
||||
const auto match = std::find_if(context.predicateDeclarations.cbegin(), context.predicateDeclarations.cend(),
|
||||
[&](const auto &predicate)
|
||||
{
|
||||
return predicate->name() == expressionIdentifier;
|
||||
});
|
||||
|
||||
// If predicate exists, parse it
|
||||
if (match != context.predicates.cend())
|
||||
if (match != context.predicateDeclarations.cend())
|
||||
expression = expressions::Predicate::parse(expressionIdentifier, parser, context, parameters);
|
||||
else
|
||||
throw utils::ParserException(parser.row(), parser.column(), "Expression \"" + expressionIdentifier + "\" not allowed in this context");
|
||||
@@ -204,14 +206,14 @@ ExpressionPointer parsePredicate(utils::Parser &parser, Context &context,
|
||||
ExpressionPointer expression;
|
||||
|
||||
// Check if predicate with that name exists
|
||||
const auto match = std::find_if(context.predicates.cbegin(), context.predicates.cend(),
|
||||
const auto match = std::find_if(context.predicateDeclarations.cbegin(), context.predicateDeclarations.cend(),
|
||||
[&](const auto &predicate)
|
||||
{
|
||||
return predicate->name() == predicateName;
|
||||
});
|
||||
|
||||
// If predicate exists, parse it
|
||||
if (match == context.predicates.cend())
|
||||
if (match == context.predicateDeclarations.cend())
|
||||
throw utils::ParserException(parser.row(), parser.column(), "Unknown predicate \"" + predicateName + "\"");
|
||||
|
||||
expression = expressions::Predicate::parse(predicateName, parser, context, parameters);
|
||||
|
@@ -1,14 +1,17 @@
|
||||
#include <plasp/pddl/Constant.h>
|
||||
#include <plasp/pddl/expressions/Constant.h>
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
#include <plasp/pddl/Context.h>
|
||||
#include <plasp/pddl/Identifier.h>
|
||||
#include <plasp/pddl/ExpressionVisitor.h>
|
||||
#include <plasp/pddl/expressions/PrimitiveType.h>
|
||||
|
||||
namespace plasp
|
||||
{
|
||||
namespace pddl
|
||||
{
|
||||
namespace expressions
|
||||
{
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
@@ -16,66 +19,51 @@ namespace pddl
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
Constant::Constant(std::string name)
|
||||
Constant::Constant()
|
||||
: m_isDirty{false},
|
||||
m_isDeclared{false},
|
||||
m_name(name),
|
||||
m_type{nullptr}
|
||||
{
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
Constant &Constant::parse(utils::Parser &parser, Context &context)
|
||||
ConstantPointer Constant::parseDeclaration(utils::Parser &parser, Context &context)
|
||||
{
|
||||
parser.skipWhiteSpace();
|
||||
|
||||
const auto constantName = parser.parseIdentifier(isIdentifier);
|
||||
const auto match = context.constantsHashMap.find(constantName);
|
||||
const auto constantExists = (match != context.constantsHashMap.cend());
|
||||
auto constant = std::make_unique<Constant>(Constant());
|
||||
|
||||
// Return existing primitive types
|
||||
if (constantExists)
|
||||
{
|
||||
auto &constant = *match->second;
|
||||
constant->m_name = parser.parseIdentifier(isIdentifier);
|
||||
|
||||
constant.setDirty();
|
||||
// Flag constant for potentially upcoming type declaration
|
||||
constant->setDirty();
|
||||
|
||||
return constant;
|
||||
}
|
||||
|
||||
// Store new primitive type
|
||||
context.constants.emplace_back(std::make_unique<Constant>(Constant(constantName)));
|
||||
|
||||
auto &constant = *context.constants.back();
|
||||
|
||||
// Add a pointer to the primitive type to the hash map
|
||||
context.constantsHashMap.emplace(std::make_pair(constantName, &constant));
|
||||
|
||||
// Flag type for potentially upcoming parent type declaration
|
||||
constant.setDirty();
|
||||
// TODO: Store constant in hash map
|
||||
|
||||
return constant;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
Constant &Constant::parseDeclaration(utils::Parser &parser, Context &context)
|
||||
void Constant::parseTypedDeclaration(utils::Parser &parser, Context &context)
|
||||
{
|
||||
// Parse and store constant
|
||||
auto &constant = parse(parser, context);
|
||||
context.constants.emplace_back(parseDeclaration(parser, context));
|
||||
|
||||
const auto &constant = context.constants.back();
|
||||
|
||||
// Flag constant as correctly declared in the types section
|
||||
constant.setDeclared();
|
||||
constant->setDeclared();
|
||||
|
||||
parser.skipWhiteSpace();
|
||||
|
||||
// Check for typing information
|
||||
if (!parser.advanceIf('-'))
|
||||
return constant;
|
||||
return;
|
||||
|
||||
// If existing, parse and store parent type
|
||||
auto &type = PrimitiveType::parse(parser, context);
|
||||
auto *type = PrimitiveType::parseExisting(parser, context.primitiveTypes);
|
||||
|
||||
// Assign parent type to all types that were previously flagged
|
||||
std::for_each(context.constants.begin(), context.constants.end(),
|
||||
@@ -84,11 +72,16 @@ Constant &Constant::parseDeclaration(utils::Parser &parser, Context &context)
|
||||
if (!constant->isDirty())
|
||||
return;
|
||||
|
||||
constant->setType(&type);
|
||||
constant->setType(type);
|
||||
constant->setDirty(false);
|
||||
});
|
||||
}
|
||||
|
||||
return constant;
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
void Constant::accept(plasp::pddl::ExpressionVisitor &expressionVisitor) const
|
||||
{
|
||||
expressionVisitor.visit(*this);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
@@ -144,3 +137,4 @@ const PrimitiveType *Constant::type() const
|
||||
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,29 +1,27 @@
|
||||
#include <plasp/pddl/Type.h>
|
||||
#include <plasp/pddl/expressions/Either.h>
|
||||
|
||||
#include <plasp/pddl/Context.h>
|
||||
#include <plasp/pddl/ExpressionVisitor.h>
|
||||
|
||||
namespace plasp
|
||||
{
|
||||
namespace pddl
|
||||
{
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Type
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
TypePtr parseType(utils::Parser &parser, Context &context)
|
||||
namespace expressions
|
||||
{
|
||||
parser.skipWhiteSpace();
|
||||
|
||||
if (parser.currentCharacter() == '(')
|
||||
return &EitherType::parse(parser, context);
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Either
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
return &PrimitiveType::parse(parser, context);
|
||||
void Either::accept(plasp::pddl::ExpressionVisitor &expressionVisitor) const
|
||||
{
|
||||
expressionVisitor.visit(*this);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,6 +1,11 @@
|
||||
#include <plasp/pddl/expressions/Predicate.h>
|
||||
|
||||
#include <plasp/pddl/Context.h>
|
||||
#include <plasp/pddl/ExpressionVisitor.h>
|
||||
#include <plasp/pddl/Identifier.h>
|
||||
#include <plasp/pddl/expressions/Constant.h>
|
||||
#include <plasp/pddl/expressions/Reference.h>
|
||||
#include <plasp/pddl/expressions/Variable.h>
|
||||
|
||||
namespace plasp
|
||||
{
|
||||
@@ -15,22 +20,46 @@ namespace expressions
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
Predicate::Predicate()
|
||||
: m_isDeclared{false}
|
||||
{
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
PredicatePointer Predicate::parse(std::string name, utils::Parser &parser,
|
||||
Context &context, const Variables ¶meters)
|
||||
{
|
||||
auto expression = std::make_unique<Predicate>(Predicate());
|
||||
auto predicate = std::make_unique<Predicate>(Predicate());
|
||||
|
||||
expression->m_name = name;
|
||||
predicate->m_name = name;
|
||||
|
||||
parser.skipWhiteSpace();
|
||||
|
||||
// Parse arguments
|
||||
while (parser.currentCharacter() != ')')
|
||||
expression->m_arguments.emplace_back(Variable::parse(parser, parameters));
|
||||
{
|
||||
// Parse variables
|
||||
if (parser.currentCharacter() == '?')
|
||||
{
|
||||
const auto *variable = Variable::parseExisting(parser, parameters);
|
||||
auto variableReference = std::make_unique<Reference<Variable>>(variable);
|
||||
predicate->m_arguments.emplace_back(std::move(variableReference));
|
||||
}
|
||||
// Parse constants
|
||||
else
|
||||
{
|
||||
const auto *constant = Constant::parseExisting(parser, context.constants);
|
||||
auto constantReference = std::make_unique<Reference<Constant>>(constant);
|
||||
predicate->m_arguments.emplace_back(std::move(constantReference));
|
||||
}
|
||||
|
||||
parser.skipWhiteSpace();
|
||||
}
|
||||
|
||||
// TODO: check that signature matches one of the declared ones
|
||||
|
||||
return expression;
|
||||
return predicate;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
@@ -42,7 +71,28 @@ void Predicate::accept(plasp::pddl::ExpressionVisitor &expressionVisitor) const
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
const std::vector<const Variable *> &Predicate::arguments() const
|
||||
void Predicate::setDeclared()
|
||||
{
|
||||
m_isDeclared = true;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
bool Predicate::isDeclared() const
|
||||
{
|
||||
return m_isDeclared;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
const std::string &Predicate::name() const
|
||||
{
|
||||
return m_name;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
const Expressions &Predicate::arguments() const
|
||||
{
|
||||
return m_arguments;
|
||||
}
|
||||
|
@@ -1,36 +1,39 @@
|
||||
#include <plasp/pddl/Predicate.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <plasp/pddl/expressions/PredicateDeclaration.h>
|
||||
|
||||
#include <plasp/pddl/Context.h>
|
||||
#include <plasp/pddl/ExpressionVisitor.h>
|
||||
#include <plasp/pddl/Identifier.h>
|
||||
#include <plasp/pddl/expressions/Constant.h>
|
||||
#include <plasp/pddl/expressions/Reference.h>
|
||||
#include <plasp/pddl/expressions/Variable.h>
|
||||
|
||||
namespace plasp
|
||||
{
|
||||
namespace pddl
|
||||
{
|
||||
namespace expressions
|
||||
{
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Predicate
|
||||
// PredicateDeclaration
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
Predicate::Predicate(std::string name)
|
||||
: m_isDeclared{false},
|
||||
m_name{name}
|
||||
PredicateDeclaration::PredicateDeclaration()
|
||||
: m_isDeclared{false}
|
||||
{
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
Predicate &Predicate::parseDeclaration(utils::Parser &parser, Context &context)
|
||||
void PredicateDeclaration::parse(utils::Parser &parser, Context &context)
|
||||
{
|
||||
parser.expect<std::string>("(");
|
||||
|
||||
const auto predicateName = parser.parseIdentifier(isIdentifier);
|
||||
auto predicate = std::make_unique<PredicateDeclaration>(PredicateDeclaration());
|
||||
|
||||
auto predicate = std::make_unique<Predicate>(Predicate(predicateName));
|
||||
predicate->m_name = parser.parseIdentifier(isIdentifier);
|
||||
|
||||
// Flag predicate as correctly declared in the types section
|
||||
predicate->setDeclared();
|
||||
@@ -47,42 +50,41 @@ Predicate &Predicate::parseDeclaration(utils::Parser &parser, Context &context)
|
||||
|
||||
parser.expect<std::string>(")");
|
||||
|
||||
const auto predicateArity = predicate->m_arguments.size();
|
||||
const PredicateHashMapKey key = {predicateName, predicateArity};
|
||||
|
||||
// Store new predicate
|
||||
context.predicates.emplace_back(std::move(predicate));
|
||||
|
||||
// Add a pointer to the predicate to the hash map
|
||||
context.predicatesHashMap.emplace(std::make_pair(key, context.predicates.back().get()));
|
||||
|
||||
return *context.predicates.back();
|
||||
context.predicateDeclarations.emplace_back(std::move(predicate));
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
void Predicate::setDeclared()
|
||||
void PredicateDeclaration::accept(plasp::pddl::ExpressionVisitor &expressionVisitor) const
|
||||
{
|
||||
expressionVisitor.visit(*this);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
void PredicateDeclaration::setDeclared()
|
||||
{
|
||||
m_isDeclared = true;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
bool Predicate::isDeclared() const
|
||||
bool PredicateDeclaration::isDeclared() const
|
||||
{
|
||||
return m_isDeclared;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
const std::string &Predicate::name() const
|
||||
const std::string &PredicateDeclaration::name() const
|
||||
{
|
||||
return m_name;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
const expressions::Variables &Predicate::arguments() const
|
||||
const Variables &PredicateDeclaration::arguments() const
|
||||
{
|
||||
return m_arguments;
|
||||
}
|
||||
@@ -91,3 +93,4 @@ const expressions::Variables &Predicate::arguments() const
|
||||
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,14 +1,16 @@
|
||||
#include <plasp/pddl/PrimitiveType.h>
|
||||
#include <plasp/pddl/expressions/PrimitiveType.h>
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
#include <plasp/pddl/Context.h>
|
||||
#include <plasp/pddl/Identifier.h>
|
||||
#include <plasp/pddl/ExpressionVisitor.h>
|
||||
|
||||
namespace plasp
|
||||
{
|
||||
namespace pddl
|
||||
{
|
||||
namespace expressions
|
||||
{
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
@@ -16,70 +18,66 @@ namespace pddl
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
PrimitiveType::PrimitiveType(std::string name)
|
||||
PrimitiveType::PrimitiveType()
|
||||
: m_isDirty{false},
|
||||
m_isDeclared{false},
|
||||
m_name(name)
|
||||
m_isDeclared{false}
|
||||
{
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
PrimitiveType &PrimitiveType::parse(utils::Parser &parser, Context &context)
|
||||
PrimitiveTypePointer PrimitiveType::parseDeclaration(utils::Parser &parser, Context &context)
|
||||
{
|
||||
parser.skipWhiteSpace();
|
||||
|
||||
const auto typeName = parser.parseIdentifier(isIdentifier);
|
||||
const auto match = context.primitiveTypesHashMap.find(typeName);
|
||||
const auto typeExists = (match != context.primitiveTypesHashMap.cend());
|
||||
|
||||
// Return existing primitive types
|
||||
if (typeExists)
|
||||
// TODO: refactor
|
||||
if (context.primitiveTypes.empty())
|
||||
{
|
||||
auto &type = *match->second;
|
||||
auto object = std::make_unique<PrimitiveType>(PrimitiveType());
|
||||
object->m_name = "object";
|
||||
object->setDirty();
|
||||
object->setDeclared();
|
||||
|
||||
type.setDirty();
|
||||
|
||||
return type;
|
||||
context.primitiveTypes.emplace_back(std::move(object));
|
||||
}
|
||||
|
||||
// Store new primitive type
|
||||
context.primitiveTypes.emplace_back(std::make_unique<PrimitiveType>(PrimitiveType(typeName)));
|
||||
parser.skipWhiteSpace();
|
||||
|
||||
auto &type = *context.primitiveTypes.back();
|
||||
auto type = std::make_unique<PrimitiveType>(PrimitiveType());
|
||||
|
||||
// Add a pointer to the primitive type to the hash map
|
||||
context.primitiveTypesHashMap.emplace(std::make_pair(typeName, &type));
|
||||
type->m_name = parser.parseIdentifier(isIdentifier);
|
||||
|
||||
// Flag type for potentially upcoming parent type declaration
|
||||
type.setDirty();
|
||||
type->setDirty();
|
||||
|
||||
// TODO: Store constant in hash map
|
||||
|
||||
return type;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
PrimitiveType &PrimitiveType::parseDeclaration(utils::Parser &parser, Context &context)
|
||||
void PrimitiveType::parseTypedDeclaration(utils::Parser &parser, Context &context)
|
||||
{
|
||||
// Parse and store type
|
||||
auto &type = parse(parser, context);
|
||||
context.primitiveTypes.emplace_back(parseDeclaration(parser, context));
|
||||
|
||||
const auto &type = context.primitiveTypes.back();
|
||||
|
||||
// Flag type as correctly declared in the types section
|
||||
type.setDeclared();
|
||||
type->setDeclared();
|
||||
|
||||
parser.skipWhiteSpace();
|
||||
|
||||
// Check for type inheritance
|
||||
if (!parser.advanceIf('-'))
|
||||
return type;
|
||||
return;
|
||||
|
||||
// If existing, parse and store parent type
|
||||
auto &parentType = parse(parser, context);
|
||||
auto *parentType = parseExisting(parser, context.primitiveTypes);
|
||||
|
||||
parentType.setDirty(false);
|
||||
parentType->setDirty(false);
|
||||
|
||||
// Flag parent tpe as correctly declared in the types section
|
||||
parentType.setDeclared();
|
||||
parentType->setDeclared();
|
||||
|
||||
// Assign parent type to all types that were previously flagged
|
||||
std::for_each(context.primitiveTypes.begin(), context.primitiveTypes.end(),
|
||||
@@ -88,11 +86,16 @@ PrimitiveType &PrimitiveType::parseDeclaration(utils::Parser &parser, Context &c
|
||||
if (!childType->isDirty())
|
||||
return;
|
||||
|
||||
childType->addParentType(&parentType);
|
||||
childType->addParentType(parentType);
|
||||
childType->setDirty(false);
|
||||
});
|
||||
}
|
||||
|
||||
return type;
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
void PrimitiveType::accept(plasp::pddl::ExpressionVisitor &expressionVisitor) const
|
||||
{
|
||||
expressionVisitor.visit(*this);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
@@ -148,3 +151,4 @@ const std::vector<const PrimitiveType *> &PrimitiveType::parentTypes() const
|
||||
|
||||
}
|
||||
}
|
||||
}
|
46
src/plasp/pddl/expressions/Type.cpp
Normal file
46
src/plasp/pddl/expressions/Type.cpp
Normal file
@@ -0,0 +1,46 @@
|
||||
#include <plasp/pddl/expressions/Type.h>
|
||||
|
||||
#include <plasp/pddl/Context.h>
|
||||
#include <plasp/pddl/expressions/Reference.h>
|
||||
|
||||
namespace plasp
|
||||
{
|
||||
namespace pddl
|
||||
{
|
||||
namespace expressions
|
||||
{
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Type
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
ExpressionPointer parseExistingPrimitiveType(utils::Parser &parser, Context &context, const Variables ¶meters)
|
||||
{
|
||||
auto reference = std::make_unique<Reference<PrimitiveType>>(PrimitiveType::parseExisting(parser, context.primitiveTypes));
|
||||
|
||||
return reference;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/*ExpressionPointer parseExistingType(utils::Parser &parser, Context &context, const Variables ¶meters)
|
||||
{
|
||||
parser.skipWhiteSpace();
|
||||
|
||||
// Parse either type (always begins with opening parenthesis)
|
||||
if (parser.currentCharacter() == '(')
|
||||
return Either::parse(parser, context, parameters, parseExistingPrimitiveType);
|
||||
|
||||
// Parse primitive type
|
||||
auto type = std::make_unique<Reference<PrimitiveType>>(PrimitiveType::parseExisting(parser, context));
|
||||
|
||||
return type;
|
||||
}*/
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
}
|
||||
}
|
||||
}
|
@@ -5,6 +5,8 @@
|
||||
#include <plasp/pddl/Context.h>
|
||||
#include <plasp/pddl/ExpressionVisitor.h>
|
||||
#include <plasp/pddl/Identifier.h>
|
||||
#include <plasp/pddl/expressions/Either.h>
|
||||
#include <plasp/pddl/expressions/Type.h>
|
||||
#include <plasp/utils/ParserException.h>
|
||||
|
||||
namespace plasp
|
||||
@@ -36,6 +38,8 @@ VariablePointer Variable::parseDeclaration(utils::Parser &parser)
|
||||
auto variable = std::make_unique<Variable>(Variable());
|
||||
|
||||
variable->m_name = parser.parseIdentifier(isIdentifier);
|
||||
|
||||
// Flag variable for potentially upcoming type declaration
|
||||
variable->setDirty();
|
||||
|
||||
return variable;
|
||||
@@ -43,10 +47,12 @@ VariablePointer Variable::parseDeclaration(utils::Parser &parser)
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
void Variable::parseTypedDeclaration(utils::Parser &parser, Context &context, Variables &variables)
|
||||
void Variable::parseTypedDeclaration(utils::Parser &parser, Context &context, Variables ¶meters)
|
||||
{
|
||||
// Parse and store variable itself
|
||||
variables.emplace_back(parseDeclaration(parser));
|
||||
parameters.emplace_back(parseDeclaration(parser));
|
||||
|
||||
auto ¶meter = parameters.back();
|
||||
|
||||
parser.skipWhiteSpace();
|
||||
|
||||
@@ -54,24 +60,41 @@ void Variable::parseTypedDeclaration(utils::Parser &parser, Context &context, Va
|
||||
if (!parser.advanceIf('-'))
|
||||
return;
|
||||
|
||||
// Parse argument type
|
||||
const auto type = parseType(parser, context);
|
||||
// TODO: do not allow nested either expressions
|
||||
|
||||
// Set the argument type for all previously flagged arguments
|
||||
std::for_each(variables.begin(), variables.end(),
|
||||
[&](auto &variable)
|
||||
const auto setType =
|
||||
[&](const auto *type)
|
||||
{
|
||||
if (!variable->isDirty())
|
||||
return;
|
||||
// Set the argument type for all previously flagged arguments
|
||||
std::for_each(parameters.begin(), parameters.end(),
|
||||
[&](auto ¶meter)
|
||||
{
|
||||
if (!parameter->isDirty())
|
||||
return;
|
||||
|
||||
variable->setType(type);
|
||||
variable->setDirty(false);
|
||||
});
|
||||
parameter->setType(type);
|
||||
parameter->setDirty(false);
|
||||
});
|
||||
};
|
||||
|
||||
// Parse argument of "either" type (always begins with opening parenthesis)
|
||||
if (parser.currentCharacter() == '(')
|
||||
{
|
||||
parameter->m_eitherExpression = Either::parse(parser, context, parameters, parseExistingPrimitiveType);
|
||||
|
||||
setType(parameter->m_eitherExpression.get());
|
||||
return;
|
||||
}
|
||||
|
||||
// Parse primitive type
|
||||
const auto *type = PrimitiveType::parseExisting(parser, context.primitiveTypes);
|
||||
|
||||
setType(type);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
const Variable *Variable::parse(utils::Parser &parser, const Variables &variables)
|
||||
const Variable *Variable::parseExisting(utils::Parser &parser, const Variables &variables)
|
||||
{
|
||||
parser.skipWhiteSpace();
|
||||
|
||||
@@ -107,7 +130,7 @@ const std::string &Variable::name() const
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
TypePtr Variable::type() const
|
||||
const Expression *Variable::type() const
|
||||
{
|
||||
return m_type;
|
||||
}
|
||||
@@ -128,7 +151,7 @@ bool Variable::isDirty() const
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
void Variable::setType(TypePtr type)
|
||||
void Variable::setType(const Expression *type)
|
||||
{
|
||||
m_type = type;
|
||||
}
|
||||
|
Reference in New Issue
Block a user