From 17448a4a043ddc64b876971ff0c3813daa641166 Mon Sep 17 00:00:00 2001 From: Dominik Drexler Date: Fri, 12 Apr 2024 14:28:49 +0200 Subject: [PATCH] added back previous templated scoping mechanism which is sufficient after rework --- include/loki/pddl/scope.hpp | 78 +++++----- include/loki/pddl/scope.tpp | 87 +++++++++++ src/parser.cpp | 6 +- src/pddl/parser/common.cpp | 10 +- src/pddl/parser/constants.cpp | 8 +- src/pddl/parser/effects.cpp | 2 +- src/pddl/parser/functions.cpp | 14 +- src/pddl/parser/ground_literal.cpp | 6 +- src/pddl/parser/literal.cpp | 8 +- src/pddl/parser/objects.cpp | 10 +- src/pddl/parser/parameters.cpp | 4 +- src/pddl/parser/predicates.cpp | 8 +- src/pddl/parser/reference_utils.cpp | 8 +- src/pddl/parser/structure.cpp | 6 +- src/pddl/parser/types.cpp | 14 +- src/pddl/scope.cpp | 228 ---------------------------- 16 files changed, 177 insertions(+), 320 deletions(-) create mode 100644 include/loki/pddl/scope.tpp diff --git a/include/loki/pddl/scope.hpp b/include/loki/pddl/scope.hpp index d5271692..6d800920 100644 --- a/include/loki/pddl/scope.hpp +++ b/include/loki/pddl/scope.hpp @@ -43,11 +43,28 @@ namespace loki /// The position points to the matched location /// in the input stream and is used for error reporting. template -using BindingValueType = std::tuple>; +using BindingValueType = std::tuple, std::optional>; /// @brief Datastructure to store bindings of a type T. template -using Bindings = std::unordered_map>; +using BindingMapType = std::unordered_map>; + +/// @brief Encapsulates bindings for different types. +template +class Bindings +{ +private: + std::tuple...> bindings; + +public: + /// @brief Returns a binding if it exists. + template + std::optional> get(const std::string& key) const; + + /// @brief Inserts a binding of type T + template + void insert(const std::string& key, const PDDLElement& binding, const std::optional& position); +}; /// @brief Wraps bindings in a scope with reference to a parent scope. class Scope @@ -55,12 +72,7 @@ class Scope private: const Scope* m_parent_scope; - Bindings m_types; - Bindings m_objects; - Bindings m_function_skeletons; - Bindings m_variables; - Bindings m_predicates; - Bindings m_derived_predicates; + Bindings bindings; public: explicit Scope(const Scope* parent_scope = nullptr); @@ -71,26 +83,18 @@ class Scope Scope(Scope&& other) = delete; Scope& operator=(Scope&& other) = delete; - /// @brief Return a binding if it exists. - std::optional> get_type(const std::string& name) const; - std::optional> get_object(const std::string& name) const; - std::optional> get_function_skeleton(const std::string& name) const; - std::optional> get_variable(const std::string& name) const; - std::optional> get_predicate(const std::string& name) const; - std::optional> get_derived_predicate(const std::string& name) const; - - /// @brief Insert a binding. - void insert_type(const std::string& name, const pddl::Type& type, const std::optional& position); - void insert_object(const std::string& name, const pddl::Object& object, const std::optional& position); - void insert_function_skeleton(const std::string& name, const pddl::FunctionSkeleton& function_skeleton, const std::optional& position); - void insert_variable(const std::string& name, const pddl::Variable& variable, const std::optional& position); - void insert_predicate(const std::string& name, const pddl::Predicate& predicate, const std::optional& position); - void insert_derived_predicate(const std::string& name, const pddl::Predicate& derived_predicate, const std::optional& position); + /// @brief Returns a binding if it exists. + template + std::optional> get(const std::string& name) const; + + /// @brief Insert a binding of type T. + template + void insert(const std::string& name, const PDDLElement& element, const std::optional& position); }; /// @brief Encapsulates the result of search for a binding with the corresponding ErrorHandler. template -using ScopeStackSearchResult = std::tuple, const PDDLErrorHandler&>; +using ScopeStackSearchResult = std::tuple, const std::optional, const PDDLErrorHandler&>; /// @brief Implements a scoping mechanism to store bindings which are mappings from name to a pointer to a PDDL object /// type and a position in the input stream that can be used to construct error messages with the given ErrorHandler. @@ -130,21 +134,13 @@ class ScopeStack /// @brief Deletes the topmost scope from the stack. void close_scope(); - /// @brief Return a binding if it exists. - std::optional> get_type(const std::string& name) const; - std::optional> get_object(const std::string& name) const; - std::optional> get_function_skeleton(const std::string& name) const; - std::optional> get_variable(const std::string& name) const; - std::optional> get_predicate(const std::string& name) const; - std::optional> get_derived_predicate(const std::string& name) const; - - /// @brief Insert a binding. - void insert_type(const std::string& name, const pddl::Type& type, const std::optional& position); - void insert_object(const std::string& name, const pddl::Object& object, const std::optional& position); - void insert_function_skeleton(const std::string& name, const pddl::FunctionSkeleton& function_skeleton, const std::optional& position); - void insert_variable(const std::string& name, const pddl::Variable& variable, const std::optional& position); - void insert_predicate(const std::string& name, const pddl::Predicate& predicate, const std::optional& position); - void insert_derived_predicate(const std::string& name, const pddl::Predicate& derived_predicate, const std::optional& position); + /// @brief Returns a binding if it exists. + template + std::optional> get(const std::string& name) const; + + /// @brief Insert a binding of type T. + template + void insert(const std::string& name, const PDDLElement& element, const std::optional& position); /// @brief Get the error handler to print an error message. const PDDLErrorHandler& get_error_handler() const; @@ -155,4 +151,6 @@ class ScopeStack } -#endif +#include "scope.tpp" + +#endif \ No newline at end of file diff --git a/include/loki/pddl/scope.tpp b/include/loki/pddl/scope.tpp new file mode 100644 index 00000000..2e2561cc --- /dev/null +++ b/include/loki/pddl/scope.tpp @@ -0,0 +1,87 @@ +/* + * Copyright (C) 2023 Dominik Drexler + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +namespace loki +{ + +template +template +std::optional> Bindings::get(const std::string& key) const +{ + const auto& t_bindings = std::get>(bindings); + auto it = t_bindings.find(key); + if (it != t_bindings.end()) + { + return { it->second }; + } + return std::nullopt; +} + +template +template +void Bindings::insert(const std::string& key, const PDDLElement& element, const std::optional& position) +{ + assert(element); + auto& t_bindings = std::get>(bindings); + assert(!t_bindings.count(key)); + t_bindings.emplace(key, std::make_tuple(element, position)); +} + +template +std::optional> Scope::get(const std::string& name) const +{ + const auto result = bindings.get(name); + if (result.has_value()) + return result.value(); + if (m_parent_scope) + { + return m_parent_scope->get(name); + } + return std::nullopt; +} + +template +void Scope::insert(const std::string& name, const PDDLElement& element, const std::optional& position) +{ + assert(element); + assert(!this->get(name)); + bindings.insert(name, element, position); +} + +template +std::optional> ScopeStack::get(const std::string& name) const +{ + assert(!m_stack.empty()); + auto result = m_stack.back()->get(name); + if (result.has_value()) + { + return std::make_tuple(std::get<0>(result.value()), std::get<1>(result.value()), std::cref(m_error_handler)); + } + if (m_parent) + return m_parent->get(name); + return std::nullopt; +} + +/// @brief Insert a binding of type T. +template +void ScopeStack::insert(const std::string& name, const PDDLElement& element, const std::optional& position) +{ + assert(!m_stack.empty()); + m_stack.back()->insert(name, element, position); +} + +} \ No newline at end of file diff --git a/src/parser.cpp b/src/parser.cpp index f330b561..e0c173e2 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -63,8 +63,8 @@ DomainParser::DomainParser(const fs::path& file_path) : // Create base types. const auto base_type_object = context.factories.types.get_or_create("object", pddl::TypeList()); const auto base_type_number = context.factories.types.get_or_create("number", pddl::TypeList()); - context.scopes.insert_type("object", base_type_object, {}); - context.scopes.insert_type("number", base_type_number, {}); + context.scopes.insert("object", base_type_object, {}); + context.scopes.insert("number", base_type_number, {}); // Create equal predicate with name "=" and two parameters "?left_arg" and "?right_arg" const auto binary_parameterlist = pddl::ParameterList { @@ -75,7 +75,7 @@ DomainParser::DomainParser(const fs::path& file_path) : }; const auto equal_predicate = context.factories.predicates.get_or_create("=", binary_parameterlist); - context.scopes.insert_predicate("=", equal_predicate, {}); + context.scopes.insert("=", equal_predicate, {}); m_domain = parse(node, context); diff --git a/src/pddl/parser/common.cpp b/src/pddl/parser/common.cpp index 44627d7c..7a57b93f 100644 --- a/src/pddl/parser/common.cpp +++ b/src/pddl/parser/common.cpp @@ -45,7 +45,7 @@ pddl::Term TermDeclarationTermVisitor::operator()(const ast::Name& node) const { const auto constant_name = parse(node); // Test for undefined constant. - const auto binding = context.scopes.get_object(constant_name); + const auto binding = context.scopes.get(constant_name); if (!binding.has_value()) { throw UndefinedConstantError(constant_name, context.scopes.get_error_handler()(node, "")); @@ -63,7 +63,7 @@ pddl::Term TermDeclarationTermVisitor::operator()(const ast::Variable& node) con { const auto variable = parse(node, context); // Test for multiple definition - const auto binding = context.scopes.get_variable(variable->get_name()); + const auto binding = context.scopes.get(variable->get_name()); if (binding.has_value()) { const auto message_1 = context.scopes.get_error_handler()(node, "Defined here:"); @@ -73,7 +73,7 @@ pddl::Term TermDeclarationTermVisitor::operator()(const ast::Variable& node) con throw MultiDefinitionVariableError(variable->get_name(), message_1 + message_2); } // Add binding to scope - context.scopes.insert_variable(variable->get_name(), variable, node); + context.scopes.insert(variable->get_name(), variable, node); // Construct Term and return it const auto term = context.factories.terms.get_or_create(variable); // Add position of PDDL object @@ -87,7 +87,7 @@ pddl::Term TermReferenceTermVisitor::operator()(const ast::Name& node) const { const auto object_name = parse(node); // Test for undefined constant. - const auto binding = context.scopes.get_object(object_name); + const auto binding = context.scopes.get(object_name); if (!binding.has_value()) { throw UndefinedConstantError(object_name, context.scopes.get_error_handler()(node, "")); @@ -105,7 +105,7 @@ pddl::Term TermReferenceTermVisitor::operator()(const ast::Variable& node) const { const auto variable = parse(node, context); // Test for undefined variable - const auto binding = context.scopes.get_variable(variable->get_name()); + const auto binding = context.scopes.get(variable->get_name()); if (!binding.has_value()) { throw UndefinedVariableError(variable->get_name(), context.scopes.get_error_handler()(node, "")); diff --git a/src/pddl/parser/constants.cpp b/src/pddl/parser/constants.cpp index c55542ed..da98136b 100644 --- a/src/pddl/parser/constants.cpp +++ b/src/pddl/parser/constants.cpp @@ -28,7 +28,7 @@ namespace loki static void test_multiple_definition(const pddl::Object& constant, const ast::Name& node, const Context& context) { const auto constant_name = constant->get_name(); - const auto binding = context.scopes.get_object(constant_name); + const auto binding = context.scopes.get(constant_name); if (binding.has_value()) { const auto message_1 = context.scopes.get_error_handler()(node, "Defined here:"); @@ -45,7 +45,7 @@ static void test_multiple_definition(const pddl::Object& constant, const ast::Na static void insert_context_information(const pddl::Object& constant, const ast::Name& node, Context& context) { context.positions.push_back(constant, node); - context.scopes.insert_object(constant->get_name(), constant, node); + context.scopes.insert(constant->get_name(), constant, node); } static pddl::Object parse_constant_definition(const ast::Name& node, const pddl::TypeList& type_list, Context& context) @@ -76,8 +76,8 @@ ConstantListVisitor::ConstantListVisitor(Context& context_) : context(context_) pddl::ObjectList ConstantListVisitor::operator()(const std::vector& name_nodes) { // std::vector has single base type "object" - assert(context.scopes.get_type("object").has_value()); - const auto [type, _position, _error_handler] = context.scopes.get_type("object").value(); + assert(context.scopes.get("object").has_value()); + const auto [type, _position, _error_handler] = context.scopes.get("object").value(); return parse_constant_definitions(name_nodes, pddl::TypeList { type }, context); } diff --git a/src/pddl/parser/effects.cpp b/src/pddl/parser/effects.cpp index aedbecdb..874777ec 100644 --- a/src/pddl/parser/effects.cpp +++ b/src/pddl/parser/effects.cpp @@ -70,7 +70,7 @@ pddl::Effect parse(const ast::EffectProductionNumericFluentTotalCost& node, Cont const auto assign_operator_increase = parse(node.assign_operator_increase); auto function_name = parse(node.function_symbol_total_cost.name); assert(function_name == "total-cost"); - auto binding = context.scopes.get_function_skeleton(function_name); + auto binding = context.scopes.get(function_name); if (!binding.has_value()) { throw UndefinedFunctionSkeletonError(function_name, context.scopes.get_error_handler()(node.function_symbol_total_cost, "")); diff --git a/src/pddl/parser/functions.cpp b/src/pddl/parser/functions.cpp index ec4bd7a3..350298c9 100644 --- a/src/pddl/parser/functions.cpp +++ b/src/pddl/parser/functions.cpp @@ -109,7 +109,7 @@ pddl::Function parse(const ast::FunctionHead& node, Context& context) pddl::FunctionSkeleton parse_function_skeleton_reference(const ast::FunctionSymbol& node, Context& context) { auto function_name = parse(node.name); - auto binding = context.scopes.get_function_skeleton(function_name); + auto binding = context.scopes.get(function_name); if (!binding.has_value()) { throw UndefinedFunctionSkeletonError(function_name, context.scopes.get_error_handler()(node, "")); @@ -122,7 +122,7 @@ pddl::FunctionSkeleton parse_function_skeleton_reference(const ast::FunctionSymb static void test_multiple_definition(const pddl::FunctionSkeleton& function_skeleton, const ast::Name& node, const Context& context) { const auto function_name = function_skeleton->get_name(); - const auto binding = context.scopes.get_function_skeleton(function_name); + const auto binding = context.scopes.get(function_name); if (binding.has_value()) { const auto message_1 = context.scopes.get_error_handler()(node, "Defined here:"); @@ -139,7 +139,7 @@ static void test_multiple_definition(const pddl::FunctionSkeleton& function_skel static void insert_context_information(const pddl::FunctionSkeleton& function_skeleton, const ast::Name& node, Context& context) { context.positions.push_back(function_skeleton, node); - context.scopes.insert_function_skeleton(function_skeleton->get_name(), function_skeleton, node); + context.scopes.insert(function_skeleton->get_name(), function_skeleton, node); } pddl::FunctionSkeleton parse(const ast::AtomicFunctionSkeletonTotalCost& node, Context& context) @@ -162,8 +162,8 @@ pddl::FunctionSkeleton parse(const ast::AtomicFunctionSkeletonTotalCost& node, C context.references.untrack(pddl::RequirementEnum::NUMERIC_FLUENTS); } - assert(context.scopes.get_type("number").has_value()); - const auto [type, _position, _error_handler] = context.scopes.get_type("number").value(); + assert(context.scopes.get("number").has_value()); + const auto [type, _position, _error_handler] = context.scopes.get("number").value(); auto function_name = parse(node.function_symbol.name); auto function_skeleton = context.factories.function_skeletons.get_or_create(function_name, pddl::ParameterList {}, type); @@ -185,8 +185,8 @@ pddl::FunctionSkeleton parse(const ast::AtomicFunctionSkeletonGeneral& node, Con auto function_parameters = boost::apply_visitor(ParameterListVisitor(context), node.arguments); context.scopes.close_scope(); - assert(context.scopes.get_type("number").has_value()); - const auto [type, _position, _error_handler] = context.scopes.get_type("number").value(); + assert(context.scopes.get("number").has_value()); + const auto [type, _position, _error_handler] = context.scopes.get("number").value(); auto function_name = parse(node.function_symbol.name); auto function_skeleton = context.factories.function_skeletons.get_or_create(function_name, function_parameters, type); diff --git a/src/pddl/parser/ground_literal.cpp b/src/pddl/parser/ground_literal.cpp index 34348d82..a59de8a4 100644 --- a/src/pddl/parser/ground_literal.cpp +++ b/src/pddl/parser/ground_literal.cpp @@ -28,7 +28,7 @@ namespace loki pddl::GroundAtom parse(const ast::AtomicFormulaOfNamesPredicate& node, Context& context) { const auto name = parse(node.predicate.name); - const auto binding = context.scopes.get_predicate(name); + const auto binding = context.scopes.get(name); if (!binding.has_value()) { throw UndefinedPredicateError(name, context.scopes.get_error_handler()(node, "")); @@ -54,8 +54,8 @@ pddl::GroundAtom parse(const ast::AtomicFormulaOfNamesEquality& node, Context& c { throw UndefinedRequirementError(pddl::RequirementEnum::EQUALITY, context.scopes.get_error_handler()(node, "")); } - assert(context.scopes.get_predicate("=").has_value()); - const auto [equal_predicate, _position, _error_handler] = context.scopes.get_predicate("=").value(); + assert(context.scopes.get("=").has_value()); + const auto [equal_predicate, _position, _error_handler] = context.scopes.get("=").value(); const auto object_left = parse_object_reference(node.name_left, context); const auto object_right = parse_object_reference(node.name_right, context); const auto atom = context.factories.ground_atoms.get_or_create(equal_predicate, pddl::ObjectList { object_left, object_right }); diff --git a/src/pddl/parser/literal.cpp b/src/pddl/parser/literal.cpp index b9bc9d37..2a7b40ec 100644 --- a/src/pddl/parser/literal.cpp +++ b/src/pddl/parser/literal.cpp @@ -26,7 +26,7 @@ namespace loki pddl::Atom parse(const ast::AtomicFormulaOfTermsPredicate& node, Context& context) { auto predicate_name = parse(node.predicate.name); - auto binding = context.scopes.get_predicate(predicate_name); + auto binding = context.scopes.get(predicate_name); if (!binding.has_value()) { throw UndefinedPredicateError(predicate_name, context.scopes.get_error_handler()(node.predicate, "")); @@ -55,8 +55,8 @@ pddl::Atom parse(const ast::AtomicFormulaOfTermsEquality& node, Context& context throw UndefinedRequirementError(pddl::RequirementEnum::EQUALITY, context.scopes.get_error_handler()(node, "")); } context.references.untrack(pddl::RequirementEnum::EQUALITY); - assert(context.scopes.get_predicate("=").has_value()); - const auto [equal_predicate, _position, _error_handler] = context.scopes.get_predicate("=").value(); + assert(context.scopes.get("=").has_value()); + const auto [equal_predicate, _position, _error_handler] = context.scopes.get("=").value(); auto left_term = boost::apply_visitor(TermReferenceTermVisitor(context), node.term_left); auto right_term = boost::apply_visitor(TermReferenceTermVisitor(context), node.term_right); const auto atom = context.factories.atoms.get_or_create(equal_predicate, pddl::TermList { left_term, right_term }); @@ -69,7 +69,7 @@ pddl::Atom parse(const ast::AtomicFormulaOfTerms& node, Context& context) { retu pddl::Atom parse(const ast::DerivedAtomicFormulaOfTerms& node, Context& context) { auto predicate_name = parse(node.derived_predicate.name); - auto binding = context.scopes.get_derived_predicate(predicate_name); + auto binding = context.scopes.get(predicate_name); if (!binding.has_value()) { throw UndefinedPredicateError(predicate_name, context.scopes.get_error_handler()(node.derived_predicate, "")); diff --git a/src/pddl/parser/objects.cpp b/src/pddl/parser/objects.cpp index 539a283b..6ce879b9 100644 --- a/src/pddl/parser/objects.cpp +++ b/src/pddl/parser/objects.cpp @@ -31,7 +31,7 @@ ObjectListVisitor::ObjectListVisitor(Context& context_) : context(context_) {} pddl::Object parse_object_reference(const ast::Name& name_node, Context& context) { const auto name = parse(name_node); - const auto binding = context.scopes.get_object(name); + const auto binding = context.scopes.get(name); if (!binding.has_value()) { throw UndefinedObjectError(name, context.scopes.get_error_handler()(name_node, "")); @@ -44,7 +44,7 @@ pddl::Object parse_object_reference(const ast::Name& name_node, Context& context static void test_multiple_definition_object(const std::string& object_name, const ast::Name& name_node, const Context& context) { - const auto binding = context.scopes.get_object(object_name); + const auto binding = context.scopes.get(object_name); if (binding.has_value()) { const auto message_1 = context.scopes.get_error_handler()(name_node, "Defined here:"); @@ -64,7 +64,7 @@ static pddl::Object parse_object_definition(const ast::Name& name_node, const pd test_multiple_definition_object(name, name_node, context); const auto object = context.factories.objects.get_or_create(name, type_list); context.positions.push_back(object, name_node); - context.scopes.insert_object(name, object, name_node); + context.scopes.insert(name, object, name_node); return object; } @@ -81,8 +81,8 @@ static pddl::ObjectList parse_object_definitions(const std::vector& n pddl::ObjectList ObjectListVisitor::operator()(const std::vector& name_nodes) { // std::vector has single base type "object" - assert(context.scopes.get_type("object").has_value()); - const auto [type, _position, _error_handler] = context.scopes.get_type("object").value(); + assert(context.scopes.get("object").has_value()); + const auto [type, _position, _error_handler] = context.scopes.get("object").value(); auto object_list = parse_object_definitions(name_nodes, pddl::TypeList { type }, context); return object_list; } diff --git a/src/pddl/parser/parameters.cpp b/src/pddl/parser/parameters.cpp index 1558f884..a54e9005 100644 --- a/src/pddl/parser/parameters.cpp +++ b/src/pddl/parser/parameters.cpp @@ -28,7 +28,7 @@ namespace loki static void test_multiple_definition(const pddl::Variable& variable, const ast::Variable& node, const Context& context) { - const auto binding = context.scopes.get_variable(variable->get_name()); + const auto binding = context.scopes.get(variable->get_name()); if (binding.has_value()) { const auto message_1 = context.scopes.get_error_handler()(node, "Defined here:"); @@ -44,7 +44,7 @@ static void test_multiple_definition(const pddl::Variable& variable, const ast:: static void insert_context_information(const pddl::Variable& variable, const ast::Variable& node, Context& context) { - context.scopes.insert_variable(variable->get_name(), variable, node); + context.scopes.insert(variable->get_name(), variable, node); } static pddl::Parameter parse_parameter_definition(const ast::Variable& variable_node, const pddl::TypeList& type_list, Context& context) diff --git a/src/pddl/parser/predicates.cpp b/src/pddl/parser/predicates.cpp index bee0f89a..7c4967ce 100644 --- a/src/pddl/parser/predicates.cpp +++ b/src/pddl/parser/predicates.cpp @@ -27,7 +27,7 @@ namespace loki static void test_multiple_definition(const pddl::Predicate& predicate, const ast::Predicate& node, const Context& context) { const auto predicate_name = predicate->get_name(); - const auto binding = context.scopes.get_predicate(predicate_name); + const auto binding = context.scopes.get(predicate_name); if (binding.has_value()) { const auto message_1 = context.scopes.get_error_handler()(node, "Defined here:"); @@ -44,7 +44,7 @@ static void test_multiple_definition(const pddl::Predicate& predicate, const ast static void insert_context_information(const pddl::Predicate& predicate, const ast::Predicate& node, Context& context) { context.positions.push_back(predicate, node); - context.scopes.insert_predicate(predicate->get_name(), predicate, node); + context.scopes.insert(predicate->get_name(), predicate, node); } static pddl::Predicate parse_predicate_definition(const ast::AtomicFormulaSkeleton& node, Context& context) @@ -78,7 +78,7 @@ pddl::PredicateList parse(const ast::Predicates& predicates_node, Context& conte static void test_multiple_definition(const pddl::Predicate& predicate, const ast::DerivedPredicate& node, const Context& context) { const auto predicate_name = predicate->get_name(); - const auto binding = context.scopes.get_derived_predicate(predicate_name); + const auto binding = context.scopes.get(predicate_name); if (binding.has_value()) { const auto message_1 = context.scopes.get_error_handler()(node, "Defined here:"); @@ -95,7 +95,7 @@ static void test_multiple_definition(const pddl::Predicate& predicate, const ast static void insert_context_information(const pddl::Predicate& predicate, const ast::DerivedPredicate& node, Context& context) { context.positions.push_back(predicate, node); - context.scopes.insert_derived_predicate(predicate->get_name(), predicate, node); + context.scopes.insert(predicate->get_name(), predicate, node); } static pddl::Predicate parse_predicate_definition(const ast::DerivedAtomicFormulaSkeleton& node, Context& context) diff --git a/src/pddl/parser/reference_utils.cpp b/src/pddl/parser/reference_utils.cpp index ed4cbeb2..6de8a474 100644 --- a/src/pddl/parser/reference_utils.cpp +++ b/src/pddl/parser/reference_utils.cpp @@ -36,7 +36,7 @@ void test_variable_references(const pddl::ParameterList& parameter_list, const C { if (context.references.exists(parameter->get_variable())) { - const auto [variable, position, error_handler] = context.scopes.get_variable(parameter->get_variable()->get_name()).value(); + const auto [variable, position, error_handler] = context.scopes.get(parameter->get_variable()->get_name()).value(); throw UnusedVariableError(variable->get_name(), error_handler(position.value(), "")); } } @@ -56,7 +56,7 @@ void test_predicate_references(const pddl::PredicateList& predicate_list, const { if (context.references.exists(predicate)) { - const auto [_predicate, position, error_handler] = context.scopes.get_predicate(predicate->get_name()).value(); + const auto [_predicate, position, error_handler] = context.scopes.get(predicate->get_name()).value(); throw UnusedPredicateError(predicate->get_name(), error_handler(position.value(), "")); } } @@ -76,7 +76,7 @@ void test_function_skeleton_references(const pddl::FunctionSkeletonList& functio { if (context.references.exists(function_skeleton)) { - const auto [_function_skeleton, position, error_handler] = context.scopes.get_function_skeleton(function_skeleton->get_name()).value(); + const auto [_function_skeleton, position, error_handler] = context.scopes.get(function_skeleton->get_name()).value(); throw UnusedFunctionSkeletonError(function_skeleton->get_name(), error_handler(position.value(), "")); } } @@ -96,7 +96,7 @@ void test_object_references(const pddl::ObjectList& object_list, const Context& { if (context.references.exists(object)) { - const auto [_object, position, error_handler] = context.scopes.get_object(object->get_name()).value(); + const auto [_object, position, error_handler] = context.scopes.get(object->get_name()).value(); throw UnusedObjectError(object->get_name(), error_handler(position.value(), "")); } } diff --git a/src/pddl/parser/structure.cpp b/src/pddl/parser/structure.cpp index 9a3224fc..fd532c06 100644 --- a/src/pddl/parser/structure.cpp +++ b/src/pddl/parser/structure.cpp @@ -73,9 +73,9 @@ pddl::Axiom parse(const ast::Axiom& node, Context& context) // :implies const auto literal = parse(node.derived_literal, context); - const auto derived_predicate = context.factories.axioms.get_or_create(condition, literal); - context.positions.push_back(derived_predicate, node); - return derived_predicate; + const auto axiom = context.factories.axioms.get_or_create(condition, literal); + context.positions.push_back(axiom, node); + return axiom; } StructureVisitor::StructureVisitor(Context& context_) : context(context_) {} diff --git a/src/pddl/parser/types.cpp b/src/pddl/parser/types.cpp index 59281e3b..c046899f 100644 --- a/src/pddl/parser/types.cpp +++ b/src/pddl/parser/types.cpp @@ -69,7 +69,7 @@ TypeReferenceTypeVisitor::TypeReferenceTypeVisitor(const Context& context_) : co pddl::TypeList TypeReferenceTypeVisitor::operator()(const ast::TypeObject&) { - const auto binding = context.scopes.get_type("object"); + const auto binding = context.scopes.get("object"); assert(binding.has_value()); const auto [type, _position, _error_handler] = binding.value(); return { type }; @@ -77,7 +77,7 @@ pddl::TypeList TypeReferenceTypeVisitor::operator()(const ast::TypeObject&) pddl::TypeList TypeReferenceTypeVisitor::operator()(const ast::TypeNumber&) { - const auto binding = context.scopes.get_type("number"); + const auto binding = context.scopes.get("number"); assert(binding.has_value()); const auto [type, _position, _error_handler] = binding.value(); return { type }; @@ -86,7 +86,7 @@ pddl::TypeList TypeReferenceTypeVisitor::operator()(const ast::TypeNumber&) pddl::TypeList TypeReferenceTypeVisitor::operator()(const ast::Name& node) { auto name = parse(node); - auto binding = context.scopes.get_type(name); + auto binding = context.scopes.get(name); if (!binding.has_value()) { throw UndefinedTypeError(name, context.scopes.get_error_handler()(node, "")); @@ -112,7 +112,7 @@ pddl::TypeList TypeReferenceTypeVisitor::operator()(const ast::TypeEither& node) static void test_multiple_definition(const pddl::Type& type, const ast::Name& node, const Context& context) { const auto type_name = type->get_name(); - const auto binding = context.scopes.get_type(type_name); + const auto binding = context.scopes.get(type_name); if (binding.has_value()) { const auto message_1 = context.scopes.get_error_handler()(node, "Defined here:"); @@ -143,7 +143,7 @@ static void test_reserved_type(const pddl::Type& type, const ast::Name& node, co static void insert_context_information(const pddl::Type& type, const ast::Name& node, Context& context) { context.positions.push_back(type, node); - context.scopes.insert_type(type->get_name(), type, node); + context.scopes.insert(type->get_name(), type, node); } static pddl::Type parse_type_definition(const ast::Name& node, const pddl::TypeList& type_list, Context& context) @@ -171,8 +171,8 @@ TypeDeclarationTypedListOfNamesVisitor::TypeDeclarationTypedListOfNamesVisitor(C pddl::TypeList TypeDeclarationTypedListOfNamesVisitor::operator()(const std::vector& name_nodes) { // std::vector has single base type "object" - assert(context.scopes.get_type("object").has_value()); - const auto [type_object, _position, _error_handler] = context.scopes.get_type("object").value(); + assert(context.scopes.get("object").has_value()); + const auto [type_object, _position, _error_handler] = context.scopes.get("object").value(); const auto type_list = parse_type_definitions(name_nodes, pddl::TypeList { type_object }, context); return type_list; } diff --git a/src/pddl/scope.cpp b/src/pddl/scope.cpp index 70785bfa..6e6445be 100644 --- a/src/pddl/scope.cpp +++ b/src/pddl/scope.cpp @@ -21,120 +21,6 @@ namespace loki { Scope::Scope(const Scope* parent_scope) : m_parent_scope(parent_scope) {} -std::optional> Scope::get_type(const std::string& name) const -{ - const auto it = m_types.find(name); - if (it != m_types.end()) - return it->second; - if (m_parent_scope) - { - return m_parent_scope->get_type(name); - } - return std::nullopt; -} - -std::optional> Scope::get_object(const std::string& name) const -{ - const auto it = m_objects.find(name); - if (it != m_objects.end()) - return it->second; - if (m_parent_scope) - { - return m_parent_scope->get_object(name); - } - return std::nullopt; -} - -std::optional> Scope::get_function_skeleton(const std::string& name) const -{ - const auto it = m_function_skeletons.find(name); - if (it != m_function_skeletons.end()) - return it->second; - if (m_parent_scope) - { - return m_parent_scope->get_function_skeleton(name); - } - return std::nullopt; -} - -std::optional> Scope::get_variable(const std::string& name) const -{ - const auto it = m_variables.find(name); - if (it != m_variables.end()) - return it->second; - if (m_parent_scope) - { - return m_parent_scope->get_variable(name); - } - return std::nullopt; -} - -std::optional> Scope::get_predicate(const std::string& name) const -{ - const auto it = m_predicates.find(name); - if (it != m_predicates.end()) - return it->second; - if (m_parent_scope) - { - return m_parent_scope->get_predicate(name); - } - return std::nullopt; -} - -std::optional> Scope::get_derived_predicate(const std::string& name) const -{ - const auto it = m_derived_predicates.find(name); - if (it != m_derived_predicates.end()) - return it->second; - if (m_parent_scope) - { - return m_parent_scope->get_derived_predicate(name); - } - return std::nullopt; -} - -void Scope::insert_type(const std::string& name, const pddl::Type& element, const std::optional& position) -{ - assert(element); - assert(!this->get_type(name)); - m_types.emplace(name, BindingValueType(element, position)); -} - -void Scope::insert_object(const std::string& name, const pddl::Object& element, const std::optional& position) -{ - assert(element); - assert(!this->get_object(name)); - m_objects.emplace(name, BindingValueType(element, position)); -} - -void Scope::insert_function_skeleton(const std::string& name, const pddl::FunctionSkeleton& element, const std::optional& position) -{ - assert(element); - assert(!this->get_function_skeleton(name)); - m_function_skeletons.emplace(name, BindingValueType(element, position)); -} - -void Scope::insert_variable(const std::string& name, const pddl::Variable& element, const std::optional& position) -{ - assert(element); - assert(!this->get_variable(name)); - m_variables.emplace(name, BindingValueType(element, position)); -} - -void Scope::insert_predicate(const std::string& name, const pddl::Predicate& element, const std::optional& position) -{ - assert(element); - assert(!this->get_predicate(name)); - m_predicates.emplace(name, BindingValueType(element, position)); -} - -void Scope::insert_derived_predicate(const std::string& name, const pddl::Predicate& element, const std::optional& position) -{ - assert(element); - assert(!this->get_derived_predicate(name)); - m_derived_predicates.emplace(name, BindingValueType(element, position)); -} - ScopeStack::ScopeStack(const PDDLErrorHandler& error_handler, const ScopeStack* parent) : m_error_handler(error_handler), m_parent(parent) {} void ScopeStack::open_scope() { m_stack.push_back(m_stack.empty() ? std::make_unique() : std::make_unique(m_stack.back().get())); } @@ -145,120 +31,6 @@ void ScopeStack::close_scope() m_stack.pop_back(); } -std::optional> ScopeStack::get_type(const std::string& name) const -{ - assert(!m_stack.empty()); - auto result = m_stack.back()->get_type(name); - if (result.has_value()) - { - return std::make_tuple(std::get<0>(result.value()), std::get<1>(result.value()), std::cref(m_error_handler)); - } - if (m_parent) - return m_parent->get_type(name); - return std::nullopt; -} - -std::optional> ScopeStack::get_object(const std::string& name) const -{ - assert(!m_stack.empty()); - auto result = m_stack.back()->get_object(name); - if (result.has_value()) - { - return std::make_tuple(std::get<0>(result.value()), std::get<1>(result.value()), std::cref(m_error_handler)); - } - if (m_parent) - return m_parent->get_object(name); - return std::nullopt; -} - -std::optional> ScopeStack::get_function_skeleton(const std::string& name) const -{ - assert(!m_stack.empty()); - auto result = m_stack.back()->get_function_skeleton(name); - if (result.has_value()) - { - return std::make_tuple(std::get<0>(result.value()), std::get<1>(result.value()), std::cref(m_error_handler)); - } - if (m_parent) - return m_parent->get_function_skeleton(name); - return std::nullopt; -} - -std::optional> ScopeStack::get_variable(const std::string& name) const -{ - assert(!m_stack.empty()); - auto result = m_stack.back()->get_variable(name); - if (result.has_value()) - { - return std::make_tuple(std::get<0>(result.value()), std::get<1>(result.value()), std::cref(m_error_handler)); - } - if (m_parent) - return m_parent->get_variable(name); - return std::nullopt; -} - -std::optional> ScopeStack::get_predicate(const std::string& name) const -{ - assert(!m_stack.empty()); - auto result = m_stack.back()->get_predicate(name); - if (result.has_value()) - { - return std::make_tuple(std::get<0>(result.value()), std::get<1>(result.value()), std::cref(m_error_handler)); - } - if (m_parent) - return m_parent->get_predicate(name); - return std::nullopt; -} - -std::optional> ScopeStack::get_derived_predicate(const std::string& name) const -{ - assert(!m_stack.empty()); - auto result = m_stack.back()->get_derived_predicate(name); - if (result.has_value()) - { - return std::make_tuple(std::get<0>(result.value()), std::get<1>(result.value()), std::cref(m_error_handler)); - } - if (m_parent) - return m_parent->get_derived_predicate(name); - return std::nullopt; -} - -void ScopeStack::insert_type(const std::string& name, const pddl::Type& element, const std::optional& position) -{ - assert(!m_stack.empty()); - m_stack.back()->insert_type(name, element, position); -} - -void ScopeStack::insert_object(const std::string& name, const pddl::Object& element, const std::optional& position) -{ - assert(!m_stack.empty()); - m_stack.back()->insert_object(name, element, position); -} - -void ScopeStack::insert_function_skeleton(const std::string& name, const pddl::FunctionSkeleton& element, const std::optional& position) -{ - assert(!m_stack.empty()); - m_stack.back()->insert_function_skeleton(name, element, position); -} - -void ScopeStack::insert_variable(const std::string& name, const pddl::Variable& element, const std::optional& position) -{ - assert(!m_stack.empty()); - m_stack.back()->insert_variable(name, element, position); -} - -void ScopeStack::insert_predicate(const std::string& name, const pddl::Predicate& element, const std::optional& position) -{ - assert(!m_stack.empty()); - m_stack.back()->insert_predicate(name, element, position); -} - -void ScopeStack::insert_derived_predicate(const std::string& name, const pddl::Predicate& element, const std::optional& position) -{ - assert(!m_stack.empty()); - m_stack.back()->insert_derived_predicate(name, element, position); -} - const PDDLErrorHandler& ScopeStack::get_error_handler() const { return m_error_handler; } const std::deque>& ScopeStack::get_stack() const { return m_stack; }