Add support for return; (#678)

This creates a ReturnExpression. A separate change should enforce that an implicit return of `()` is only allowed in functions that have an implicit return type of `()`, and I think the structure taken here should ease that.
This commit is contained in:
Jon Meow
2021-08-02 16:30:08 -07:00
committed by GitHub
parent b1993a6cd0
commit 6ae9cc3cf8
16 changed files with 114 additions and 33 deletions
+12 -4
View File
@@ -117,11 +117,15 @@ auto Statement::MakeContinue(int line_num) -> const Statement* {
return s;
}
auto Statement::MakeReturn(int line_num, const Expression* e)
-> const Statement* {
auto Statement::MakeReturn(int line_num, const Expression* exp,
bool is_omitted_exp) -> const Statement* {
auto* s = global_arena->New<Statement>();
s->line_num = line_num;
s->value = Return({.exp = e});
if (exp == nullptr) {
CHECK(is_omitted_exp);
exp = Expression::MakeTupleLiteral(line_num, {});
}
s->value = Return({.exp = exp, .is_omitted_exp = is_omitted_exp});
return s;
}
@@ -230,7 +234,11 @@ void Statement::PrintDepth(int depth, llvm::raw_ostream& out) const {
}
break;
case StatementKind::Return:
out << "return " << *GetReturn().exp << ";";
if (GetReturn().is_omitted_exp) {
out << "return;";
} else {
out << "return " << *GetReturn().exp << ";";
}
break;
case StatementKind::Sequence:
GetSequence().stmt->PrintDepth(depth, out);