Support return statement with no expression
This commit is contained in:
parent
1f3c495acf
commit
bc7d24ad62
4 changed files with 5 additions and 2 deletions
|
|
@ -28,6 +28,7 @@ namespace pslang::ast
|
|||
|
||||
struct return_statement
|
||||
{
|
||||
// can be null, which means "return unit"
|
||||
expression_ptr value;
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -267,7 +267,8 @@ namespace pslang::ast
|
|||
put_indent(out, options);
|
||||
out << "return";
|
||||
newline(out);
|
||||
print(out, node.value, child(options));
|
||||
if (node.value)
|
||||
print(out, node.value, child(options));
|
||||
}
|
||||
|
||||
void print(std::ostream & out, statement_ptr const & node, print_options const & options)
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue