Support return statement with no expression

This commit is contained in:
Nikita Lisitsa 2025-12-17 14:22:20 +03:00
parent 1f3c495acf
commit bc7d24ad62
4 changed files with 5 additions and 2 deletions

View file

@ -28,6 +28,7 @@ namespace pslang::ast
struct return_statement
{
// can be null, which means "return unit"
expression_ptr value;
};

View file

@ -267,6 +267,7 @@ namespace pslang::ast
put_indent(out, options);
out << "return";
newline(out);
if (node.value)
print(out, node.value, child(options));
}

View file

@ -180,7 +180,7 @@ namespace pslang::interpreter
void execute_impl(context & context, ast::return_statement const & return_statement)
{
auto value = eval(context, return_statement.value);
auto value = return_statement.value ? eval(context, return_statement.value) : unit_value{};
for (auto it = context.scope_stack.rbegin(); it != context.scope_stack.rend(); ++it)
{
if (it->is_function_scope)

View file

@ -188,6 +188,7 @@ statement
| 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, {}}; }
| return expression { $$ = ast::return_statement{std::make_unique<ast::expression>($2)}; }
| return { $$ = ast::return_statement{nullptr}; }
;
function_definition_argument_list