Initial implementation of returned var. (#1348)

This PR implements the `returned var` for the explorer.
- Enabled `returned var` and `return var` syntax in lexer and parser.
- Split `Return` statement into `ReturnVar` and `ReturnExpression` to distinguish the two types of returns.
- Added logic in name resolution, type checking and interpretation to process `returned var` definition and `ReturnVar` statement.
This commit is contained in:
SlaterLatiao
2022-07-06 13:35:41 -07:00
committed by GitHub
parent a23f15e901
commit 9106e9239e
29 changed files with 544 additions and 61 deletions
+28
View File
@@ -4,6 +4,8 @@
#include "explorer/ast/static_scope.h"
#include <optional>
#include "explorer/common/error_builders.h"
#include "llvm/Support/Error.h"
@@ -71,4 +73,30 @@ auto StaticScope::TryResolve(const std::string& name,
return result;
}
auto StaticScope::AddReturnedVar(ValueNodeView returned_var_def_view)
-> ErrorOr<Success> {
std::optional<ValueNodeView> resolved_returned_var = ResolveReturned();
if (resolved_returned_var.has_value()) {
return CompilationError(returned_var_def_view.base().source_loc())
<< "Duplicate definition of returned var also found at "
<< resolved_returned_var->base().source_loc();
}
returned_var_def_view_ = std::move(returned_var_def_view);
return Success();
}
auto StaticScope::ResolveReturned() const -> std::optional<ValueNodeView> {
if (returned_var_def_view_.has_value()) {
return returned_var_def_view_;
}
for (Nonnull<const StaticScope*> parent : parent_scopes_) {
std::optional<ValueNodeView> parent_returned_var =
parent->ResolveReturned();
if (parent_returned_var.has_value()) {
return parent_returned_var;
}
}
return std::nullopt;
}
} // namespace Carbon