Refactor: split function definition & declaration in ast

This commit is contained in:
Nikita Lisitsa 2026-03-12 21:45:21 +03:00
parent 927368aa80
commit 13a9ba24f2
2 changed files with 19 additions and 14 deletions

View file

@ -12,7 +12,7 @@
namespace pslang::ast namespace pslang::ast
{ {
struct function_definition struct function_declaration
{ {
struct argument struct argument
{ {
@ -24,10 +24,15 @@ namespace pslang::ast
std::string name; std::string name;
std::vector<argument> arguments; std::vector<argument> arguments;
type_ptr return_type; type_ptr return_type;
statement_list_ptr statements;
ast::location location; ast::location location;
}; };
struct function_definition
: function_declaration
{
statement_list_ptr statements;
};
struct return_statement struct return_statement
{ {
// can be null, which means "return unit" // can be null, which means "return unit"

View file

@ -164,9 +164,9 @@ template <typename T>
%type <indented_statement_list> indented_statement_list %type <indented_statement_list> indented_statement_list
%type <std::size_t> indentation %type <std::size_t> indentation
%type <ast::statement> statement %type <ast::statement> statement
%type <std::vector<ast::function_definition::argument>> function_definition_argument_list %type <std::vector<ast::function_declaration::argument>> function_declaration_argument_list
%type <std::vector<ast::function_definition::argument>> nonempty_function_definition_argument_list %type <std::vector<ast::function_declaration::argument>> nonempty_function_declaration_argument_list
%type <ast::function_definition::argument> function_definition_single_argument %type <ast::function_declaration::argument> function_declaration_single_argument
%type <ast::type_ptr> function_return_type %type <ast::type_ptr> function_return_type
%type <ast::variable_declaration> variable_declaration %type <ast::variable_declaration> variable_declaration
%type <ast::value_category> variable_keyword %type <ast::value_category> variable_keyword
@ -211,25 +211,25 @@ statement
| else colon { $$ = ast::else_block{@$}; } | else colon { $$ = ast::else_block{@$}; }
| else if expression colon { $$ = ast::else_if_block{std::make_unique<ast::expression>($3), @$}; } | else if expression colon { $$ = ast::else_if_block{std::make_unique<ast::expression>($3), @$}; }
| while expression colon { $$ = ast::while_block{std::make_unique<ast::expression>($2), {}, @$}; } | while expression colon { $$ = ast::while_block{std::make_unique<ast::expression>($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<ast::expression>($2), @$}; } | return expression { $$ = ast::return_statement{std::make_unique<ast::expression>($2), @$}; }
| return { $$ = ast::return_statement{nullptr, @$}; } | return { $$ = ast::return_statement{nullptr, @$}; }
| struct name colon { $$ = ast::struct_definition{$2, {}, @$}; } | struct name colon { $$ = ast::struct_definition{$2, {}, @$}; }
| name colon type_expression { $$ = ast::field_definition{$1, std::make_unique<ast::type>($3), @$}; } | name colon type_expression { $$ = ast::field_definition{$1, std::make_unique<ast::type>($3), @$}; }
; ;
function_definition_argument_list function_declaration_argument_list
: %empty { std::vector<ast::function_definition::argument> tmp; $$ = std::move(tmp); } : %empty { std::vector<ast::function_declaration::argument> tmp; $$ = std::move(tmp); }
| nonempty_function_definition_argument_list { $$ = $1; } | nonempty_function_declaration_argument_list { $$ = $1; }
; ;
nonempty_function_definition_argument_list nonempty_function_declaration_argument_list
: function_definition_single_argument { std::vector<ast::function_definition::argument> tmp; tmp.push_back($1); $$ = std::move(tmp); } : function_declaration_single_argument { std::vector<ast::function_declaration::argument> 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 comma function_declaration_single_argument { auto tmp = $1; tmp.push_back($3); $$ = std::move(tmp); }
; ;
function_definition_single_argument function_declaration_single_argument
: name colon type_expression { $$ = ast::function_definition::argument{$1, std::make_unique<ast::type>($3), @$}; } : name colon type_expression { $$ = ast::function_declaration::argument{$1, std::make_unique<ast::type>($3), @$}; }
; ;
function_return_type function_return_type