From adaee9387d43c9434b23078c47794672cdfc86ac Mon Sep 17 00:00:00 2001 From: Juan Cruz Viotti Date: Thu, 21 Nov 2024 22:02:55 -0400 Subject: [PATCH] Prepare new evaluators to extract handlers into functions (#214) Signed-off-by: Juan Cruz Viotti --- src/evaluator/evaluator.cc | 5 +-- src/evaluator/evaluator_complete.h | 61 +++++++++++++++--------------- src/evaluator/evaluator_fast.h | 51 +++++++++++++------------ 3 files changed, 59 insertions(+), 58 deletions(-) diff --git a/src/evaluator/evaluator.cc b/src/evaluator/evaluator.cc index e2bd3c91..64591a0c 100644 --- a/src/evaluator/evaluator.cc +++ b/src/evaluator/evaluator.cc @@ -19,7 +19,7 @@ using namespace sourcemeta::jsontoolkit; inline auto resolve_target(const std::optional> &property_target, - const JSON &instance) -> const JSON & { + const JSON &instance) noexcept -> const JSON & { if (property_target.has_value()) [[unlikely]] { // In this case, we still need to return a string in order // to cope with non-string keywords inside `propertyNames` @@ -37,7 +37,7 @@ inline auto resolve_string_target( const JSON &instance, const Pointer &relative_instance_location) noexcept -> std::optional> { if (property_target.has_value()) [[unlikely]] { - return property_target.value(); + return property_target; } const auto &target{get(instance, relative_instance_location)}; @@ -72,7 +72,6 @@ auto evaluate(const Template &schema, auto evaluate(const Template &schema, const sourcemeta::jsontoolkit::JSON &instance) -> bool { EvaluationContext context; - if (schema.second.dynamic || schema.second.track) { return evaluate_complete(instance, context, schema.first, schema.second, std::nullopt); diff --git a/src/evaluator/evaluator_complete.h b/src/evaluator/evaluator_complete.h index 36e2f51f..91689b59 100644 --- a/src/evaluator/evaluator_complete.h +++ b/src/evaluator/evaluator_complete.h @@ -1,19 +1,6 @@ #ifndef SOURCEMETA_BLAZE_EVALUATOR_COMPLETE_H_ #define SOURCEMETA_BLAZE_EVALUATOR_COMPLETE_H_ -namespace sourcemeta::blaze { - -auto evaluate_complete_instruction( - const sourcemeta::blaze::Instruction &instruction, - const sourcemeta::blaze::Modifiers &modifiers, - const std::optional &callback, - const sourcemeta::jsontoolkit::JSON &instance, - const std::optional< - std::reference_wrapper> - &property_target, - const std::uint64_t depth, sourcemeta::blaze::EvaluationContext &context) - -> bool { - #define EVALUATE_BEGIN(instruction_category, instruction_type, precondition) \ SOURCEMETA_TRACE_START(trace_id, SOURCEMETA_STRINGIFY(instruction_type)); \ const auto &instruction_category{std::get(instruction)}; \ @@ -88,8 +75,8 @@ auto evaluate_complete_instruction( const auto &target{maybe_target.value().get()}; \ bool result{false}; - // This is a slightly complicated dance to avoid traversing the relative - // instance location twice. +// This is a slightly complicated dance to avoid traversing the relative +// instance location twice. #define EVALUATE_BEGIN_TRY_TARGET(instruction_category, instruction_type, \ precondition) \ SOURCEMETA_TRACE_START(trace_id, SOURCEMETA_STRINGIFY(instruction_type)); \ @@ -99,7 +86,7 @@ auto evaluate_complete_instruction( SOURCEMETA_TRACE_END(trace_id, SOURCEMETA_STRINGIFY(instruction_type)); \ return true; \ } \ - auto target_check{ \ + const auto target_check{ \ try_get(target, instruction_category.relative_instance_location)}; \ if (!target_check.has_value()) { \ SOURCEMETA_TRACE_END(trace_id, SOURCEMETA_STRINGIFY(instruction_type)); \ @@ -220,21 +207,20 @@ auto evaluate_complete_instruction( std::cref(name), depth + 1, context) #define SOURCEMETA_EVALUATOR_COMPLETE -#include "dispatch.inc.h" -#undef SOURCEMETA_EVALUATOR_COMPLETE -#undef EVALUATE_BEGIN -#undef EVALUATE_BEGIN_IF_STRING -#undef EVALUATE_BEGIN_TRY_TARGET -#undef EVALUATE_BEGIN_NO_PRECONDITION -#undef EVALUATE_BEGIN_NO_PRECONDITION_AND_NO_PUSH -#undef EVALUATE_BEGIN_PASS_THROUGH -#undef EVALUATE_END -#undef EVALUATE_END_NO_POP -#undef EVALUATE_END_PASS_THROUGH -#undef EVALUATE_ANNOTATION -#undef EVALUATE_RECURSE -#undef EVALUATE_RECURSE_ON_PROPERTY_NAME +namespace sourcemeta::blaze { + +auto evaluate_complete_instruction( + const sourcemeta::blaze::Instruction &instruction, + const sourcemeta::blaze::Modifiers &modifiers, + const std::optional &callback, + const sourcemeta::jsontoolkit::JSON &instance, + const std::optional< + std::reference_wrapper> + &property_target, + const std::uint64_t depth, sourcemeta::blaze::EvaluationContext &context) + -> bool { +#include "dispatch.inc.h" } inline auto @@ -263,4 +249,19 @@ evaluate_complete(const sourcemeta::jsontoolkit::JSON &instance, } // namespace sourcemeta::blaze +#undef SOURCEMETA_EVALUATOR_COMPLETE + +#undef EVALUATE_BEGIN +#undef EVALUATE_BEGIN_IF_STRING +#undef EVALUATE_BEGIN_TRY_TARGET +#undef EVALUATE_BEGIN_NO_PRECONDITION +#undef EVALUATE_BEGIN_NO_PRECONDITION_AND_NO_PUSH +#undef EVALUATE_BEGIN_PASS_THROUGH +#undef EVALUATE_END +#undef EVALUATE_END_NO_POP +#undef EVALUATE_END_PASS_THROUGH +#undef EVALUATE_ANNOTATION +#undef EVALUATE_RECURSE +#undef EVALUATE_RECURSE_ON_PROPERTY_NAME + #endif diff --git a/src/evaluator/evaluator_fast.h b/src/evaluator/evaluator_fast.h index 72e77464..3b756b4e 100644 --- a/src/evaluator/evaluator_fast.h +++ b/src/evaluator/evaluator_fast.h @@ -1,17 +1,6 @@ #ifndef SOURCEMETA_BLAZE_EVALUATOR_FAST_H_ #define SOURCEMETA_BLAZE_EVALUATOR_FAST_H_ -namespace sourcemeta::blaze { - -auto evaluate_fast_instruction( - const sourcemeta::blaze::Instruction &instruction, - const sourcemeta::jsontoolkit::JSON &instance, - const std::optional< - std::reference_wrapper> - &property_target, - const std::uint64_t depth, sourcemeta::blaze::EvaluationContext &context) - -> bool { - #define EVALUATE_BEGIN(instruction_category, instruction_type, precondition) \ SOURCEMETA_TRACE_START(trace_id, SOURCEMETA_STRINGIFY(instruction_type)); \ const auto &instruction_category{std::get(instruction)}; \ @@ -99,21 +88,18 @@ auto evaluate_fast_instruction( evaluate_fast_instruction(child, target, std::cref(name), depth + 1, context) #define SOURCEMETA_EVALUATOR_FAST -#include "dispatch.inc.h" -#undef SOURCEMETA_EVALUATOR_FAST -#undef EVALUATE_BEGIN -#undef EVALUATE_BEGIN_IF_STRING -#undef EVALUATE_BEGIN_TRY_TARGET -#undef EVALUATE_BEGIN_NO_PRECONDITION -#undef EVALUATE_BEGIN_NO_PRECONDITION_AND_NO_PUSH -#undef EVALUATE_BEGIN_PASS_THROUGH -#undef EVALUATE_END -#undef EVALUATE_END_NO_POP -#undef EVALUATE_END_PASS_THROUGH -#undef EVALUATE_ANNOTATION -#undef EVALUATE_RECURSE -#undef EVALUATE_RECURSE_ON_PROPERTY_NAME +namespace sourcemeta::blaze { + +auto evaluate_fast_instruction( + const sourcemeta::blaze::Instruction &instruction, + const sourcemeta::jsontoolkit::JSON &instance, + const std::optional< + std::reference_wrapper> + &property_target, + const std::uint64_t depth, sourcemeta::blaze::EvaluationContext &context) + -> bool { +#include "dispatch.inc.h" } inline auto evaluate_fast(const sourcemeta::jsontoolkit::JSON &instance, @@ -133,4 +119,19 @@ inline auto evaluate_fast(const sourcemeta::jsontoolkit::JSON &instance, } // namespace sourcemeta::blaze +#undef SOURCEMETA_EVALUATOR_FAST + +#undef EVALUATE_BEGIN +#undef EVALUATE_BEGIN_IF_STRING +#undef EVALUATE_BEGIN_TRY_TARGET +#undef EVALUATE_BEGIN_NO_PRECONDITION +#undef EVALUATE_BEGIN_NO_PRECONDITION_AND_NO_PUSH +#undef EVALUATE_BEGIN_PASS_THROUGH +#undef EVALUATE_END +#undef EVALUATE_END_NO_POP +#undef EVALUATE_END_PASS_THROUGH +#undef EVALUATE_ANNOTATION +#undef EVALUATE_RECURSE +#undef EVALUATE_RECURSE_ON_PROPERTY_NAME + #endif