From 35435ac62618bd1ade3a5dd0dbc8f14b8771ce99 Mon Sep 17 00:00:00 2001 From: lisyarus Date: Mon, 22 Dec 2025 18:52:57 +0300 Subject: [PATCH] Rewrite get_location(statement/expression) using visitors --- libs/ast/source/expression.cpp | 67 +++++++++++++++++++++-------- libs/ast/source/statement.cpp | 77 ++++++++++++++++++++++++++++------ 2 files changed, 113 insertions(+), 31 deletions(-) diff --git a/libs/ast/source/expression.cpp b/libs/ast/source/expression.cpp index e5b7e90..9418539 100644 --- a/libs/ast/source/expression.cpp +++ b/libs/ast/source/expression.cpp @@ -1,4 +1,5 @@ #include +#include namespace pslang::ast { @@ -6,33 +7,63 @@ namespace pslang::ast namespace { - template - location get_location_impl(numeric_literal_base const & expression) + struct get_location_visitor + : const_expression_visitor { - return expression.location; - } + using const_expression_visitor::apply; - location get_location_impl(literal const & expression) - { - return std::visit([](auto const & expression){ return get_location_impl(expression); }, expression); - } + template + location apply(numeric_literal_base const & node) + { + return node.location; + } - template - location get_location_impl(Expression const & expression) - { - return expression.location; - } + location apply(identifier const & node) + { + return node.location; + } - location get_location_impl(expression const & expression) - { - return std::visit([](auto const & expression){ return get_location_impl(expression); }, expression); - } + location apply(unary_operation const & node) + { + return node.location; + } + + location apply(binary_operation const & node) + { + return node.location; + } + + location apply(cast_operation const & node) + { + return node.location; + } + + location apply(function_call const & node) + { + return node.location; + } + + location apply(array const & node) + { + return node.location; + } + + location apply(array_access const & node) + { + return node.location; + } + + location apply(field_access const & node) + { + return node.location; + } + }; } location get_location(expression const & expression) { - return get_location_impl(expression); + return get_location_visitor{}.apply(expression); } } diff --git a/libs/ast/source/statement.cpp b/libs/ast/source/statement.cpp index 5cd0740..9835a4b 100644 --- a/libs/ast/source/statement.cpp +++ b/libs/ast/source/statement.cpp @@ -1,4 +1,5 @@ #include +#include namespace pslang::ast { @@ -6,27 +7,77 @@ namespace pslang::ast namespace { - location get_location_impl(expression_ptr const & statement) + struct get_location_visitor + : const_statement_visitor { - return get_location(*statement); - } + using const_statement_visitor::apply; - template - location get_location_impl(Statement const & statement) - { - return statement.location; - } + location apply(expression_ptr const & node) + { + return get_location(*node); + } - location get_location_impl(statement const & statement) - { - return std::visit([](auto const & statement){ return get_location_impl(statement); }, statement); - } + location apply(assignment const & node) + { + return node.location; + } + + location apply(variable_declaration const & node) + { + return node.location; + } + + location apply(if_block const & node) + { + return node.location; + } + + location apply(else_block const & node) + { + return node.location; + } + + location apply(else_if_block const & node) + { + return node.location; + } + + location apply(if_chain const & node) + { + return node.location; + } + + location apply(while_block const & node) + { + return node.location; + } + + location apply(function_definition const & node) + { + return node.location; + } + + location apply(return_statement const & node) + { + return node.location; + } + + location apply(field_definition const & node) + { + return node.location; + } + + location apply(struct_definition const & node) + { + return node.location; + } + }; } location get_location(statement const & statement) { - return get_location_impl(statement); + return get_location_visitor{}.apply(statement); } }