mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-28 20:44:53 +01:00
Add support for _ placeholder. (#661)
Co-authored-by: Jon Meow <46229924+jonmeow@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
co-authored by
Jon Meow
parent
c3c89a77a4
commit
def98d1182
@@ -105,7 +105,8 @@ auto Expression::MakeIdentifierExpression(int line_num, std::string var)
|
||||
return v;
|
||||
}
|
||||
|
||||
auto Expression::MakeBindingExpression(int line_num, std::string var,
|
||||
auto Expression::MakeBindingExpression(int line_num,
|
||||
std::optional<std::string> var,
|
||||
const Expression* type)
|
||||
-> const Expression* {
|
||||
auto* v = new Expression();
|
||||
@@ -280,11 +281,17 @@ void PrintExp(const Expression* e) {
|
||||
case ExpressionKind::IdentifierExpression:
|
||||
std::cout << e->GetIdentifierExpression().name;
|
||||
break;
|
||||
case ExpressionKind::BindingExpression:
|
||||
PrintExp(e->GetBindingExpression().type);
|
||||
case ExpressionKind::BindingExpression: {
|
||||
const BindingExpression& binding = e->GetBindingExpression();
|
||||
if (binding.name.has_value()) {
|
||||
std::cout << *binding.name;
|
||||
} else {
|
||||
std::cout << "_";
|
||||
}
|
||||
std::cout << ": ";
|
||||
std::cout << e->GetBindingExpression().name;
|
||||
PrintExp(e->GetBindingExpression().type);
|
||||
break;
|
||||
}
|
||||
case ExpressionKind::CallExpression:
|
||||
PrintExp(e->GetCallExpression().function);
|
||||
if (e->GetCallExpression().argument->tag() ==
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#ifndef EXECUTABLE_SEMANTICS_AST_EXPRESSION_H_
|
||||
#define EXECUTABLE_SEMANTICS_AST_EXPRESSION_H_
|
||||
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <variant>
|
||||
#include <vector>
|
||||
@@ -74,7 +75,8 @@ struct IndexExpression {
|
||||
|
||||
struct BindingExpression {
|
||||
static constexpr ExpressionKind Kind = ExpressionKind::BindingExpression;
|
||||
std::string name;
|
||||
// nullopt represents the `_` placeholder.
|
||||
std::optional<std::string> name;
|
||||
const Expression* type;
|
||||
};
|
||||
|
||||
@@ -136,7 +138,8 @@ struct TypeTypeLiteral {
|
||||
struct Expression {
|
||||
static auto MakeIdentifierExpression(int line_num, std::string var)
|
||||
-> const Expression*;
|
||||
static auto MakeBindingExpression(int line_num, std::string var,
|
||||
static auto MakeBindingExpression(int line_num,
|
||||
std::optional<std::string> var,
|
||||
const Expression* type)
|
||||
-> const Expression*;
|
||||
static auto MakeIntLiteral(int line_num, int i) -> const Expression*;
|
||||
|
||||
Reference in New Issue
Block a user