From 13a9ba24f2ba6da4e55e6fc05200c2f900333c7b Mon Sep 17 00:00:00 2001 From: lisyarus Date: Thu, 12 Mar 2026 21:45:21 +0300 Subject: [PATCH] Refactor: split function definition & declaration in ast --- libs/ast/include/pslang/ast/function.hpp | 9 +++++++-- libs/parser/rules/pslang.y | 24 ++++++++++++------------ 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/libs/ast/include/pslang/ast/function.hpp b/libs/ast/include/pslang/ast/function.hpp index 3482c7d..c6c49f5 100644 --- a/libs/ast/include/pslang/ast/function.hpp +++ b/libs/ast/include/pslang/ast/function.hpp @@ -12,7 +12,7 @@ namespace pslang::ast { - struct function_definition + struct function_declaration { struct argument { @@ -24,10 +24,15 @@ namespace pslang::ast std::string name; std::vector arguments; type_ptr return_type; - statement_list_ptr statements; ast::location location; }; + struct function_definition + : function_declaration + { + statement_list_ptr statements; + }; + struct return_statement { // can be null, which means "return unit" diff --git a/libs/parser/rules/pslang.y b/libs/parser/rules/pslang.y index a384611..61da3b2 100644 --- a/libs/parser/rules/pslang.y +++ b/libs/parser/rules/pslang.y @@ -164,9 +164,9 @@ template %type indented_statement_list %type indentation %type statement -%type > function_definition_argument_list -%type > nonempty_function_definition_argument_list -%type function_definition_single_argument +%type > function_declaration_argument_list +%type > nonempty_function_declaration_argument_list +%type function_declaration_single_argument %type function_return_type %type variable_declaration %type variable_keyword @@ -211,25 +211,25 @@ statement | else colon { $$ = ast::else_block{@$}; } | else if expression colon { $$ = ast::else_if_block{std::make_unique($3), @$}; } | while expression colon { $$ = ast::while_block{std::make_unique($2), {}, @$}; } -| func name lparen function_definition_argument_list rparen function_return_type colon { $$ = ast::function_definition{$2, $4, $6, {}, @$}; } +| func name lparen function_declaration_argument_list rparen function_return_type colon { $$ = ast::function_definition{{$2, $4, $6, @$}, {}}; } | return expression { $$ = ast::return_statement{std::make_unique($2), @$}; } | return { $$ = ast::return_statement{nullptr, @$}; } | struct name colon { $$ = ast::struct_definition{$2, {}, @$}; } | name colon type_expression { $$ = ast::field_definition{$1, std::make_unique($3), @$}; } ; -function_definition_argument_list -: %empty { std::vector tmp; $$ = std::move(tmp); } -| nonempty_function_definition_argument_list { $$ = $1; } +function_declaration_argument_list +: %empty { std::vector tmp; $$ = std::move(tmp); } +| nonempty_function_declaration_argument_list { $$ = $1; } ; -nonempty_function_definition_argument_list -: function_definition_single_argument { std::vector tmp; tmp.push_back($1); $$ = std::move(tmp); } -| nonempty_function_definition_argument_list comma function_definition_single_argument { auto tmp = $1; tmp.push_back($3); $$ = std::move(tmp); } +nonempty_function_declaration_argument_list +: function_declaration_single_argument { std::vector tmp; tmp.push_back($1); $$ = std::move(tmp); } +| nonempty_function_declaration_argument_list comma function_declaration_single_argument { auto tmp = $1; tmp.push_back($3); $$ = std::move(tmp); } ; -function_definition_single_argument -: name colon type_expression { $$ = ast::function_definition::argument{$1, std::make_unique($3), @$}; } +function_declaration_single_argument +: name colon type_expression { $$ = ast::function_declaration::argument{$1, std::make_unique($3), @$}; } ; function_return_type