Files
carbon-lang/executable_semantics/ast/pattern.cpp
T
Jon MeowandGeoff Romer 250ce4ab00 Add string parsing and a print builtin (#721)
It was in my mind to add String in order to support libraries in `package`.  `print` is added in order to have a String go to stdout. I've tried to do `print` in a way that won't be too hard to add other printable types, but it's probably also somewhat optional here -- that is, if desired, I could remove it. But it was a lot easier to doublecheck `\n` behavior with it, and I suspect it'll be helpful in other tests if it supports more value types.

On the side, this also fixes dereferencing in Pattern/Expression Print() calls, which I was noticing printing pointers instead of values. This may be another argument for moving away from passing pointers, since this seems to be a difficult-to-catch error.

Co-authored-by: Geoff Romer <gromer@google.com>
2021-08-11 13:14:05 -07:00

110 lines
3.6 KiB
C++

// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#include "executable_semantics/ast/pattern.h"
#include <string>
#include "common/ostream.h"
#include "executable_semantics/ast/expression.h"
#include "executable_semantics/common/arena.h"
#include "executable_semantics/common/error.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/Support/Casting.h"
namespace Carbon {
using llvm::cast;
void Pattern::Print(llvm::raw_ostream& out) const {
switch (Tag()) {
case Kind::AutoPattern:
out << "auto";
break;
case Kind::BindingPattern: {
const auto& binding = cast<BindingPattern>(*this);
if (binding.Name().has_value()) {
out << *binding.Name();
} else {
out << "_";
}
out << ": " << *binding.Type();
break;
}
case Kind::TuplePattern: {
const auto& tuple = cast<TuplePattern>(*this);
out << "(";
llvm::ListSeparator sep;
for (const TuplePattern::Field& field : tuple.Fields()) {
out << sep << field.name << " = " << *field.pattern;
}
out << ")";
break;
}
case Kind::AlternativePattern: {
const auto& alternative = cast<AlternativePattern>(*this);
out << *alternative.ChoiceType() << "." << alternative.AlternativeName()
<< *alternative.Arguments();
break;
}
case Kind::ExpressionPattern:
out << *cast<ExpressionPattern>(*this).Expression();
break;
}
}
TuplePattern::TuplePattern(const Expression* tuple_literal)
: Pattern(Kind::TuplePattern, tuple_literal->LineNumber()) {
const auto& tuple = cast<TupleLiteral>(*tuple_literal);
for (const FieldInitializer& init : tuple.Fields()) {
fields.push_back(Field(
init.name, global_arena->New<ExpressionPattern>(init.expression)));
}
}
auto PatternFromParenContents(int line_num,
const ParenContents<Pattern>& paren_contents)
-> const Pattern* {
std::optional<const Pattern*> single_term = paren_contents.SingleTerm();
if (single_term.has_value()) {
return *single_term;
} else {
return TuplePatternFromParenContents(line_num, paren_contents);
}
}
auto TuplePatternFromParenContents(int line_num,
const ParenContents<Pattern>& paren_contents)
-> const TuplePattern* {
return global_arena->New<TuplePattern>(
line_num, paren_contents.TupleElements<TuplePattern::Field>(line_num));
}
AlternativePattern::AlternativePattern(int line_num,
const Expression* alternative,
const TuplePattern* arguments)
: Pattern(Kind::AlternativePattern, line_num), arguments(arguments) {
if (alternative->Tag() != Expression::Kind::FieldAccessExpression) {
FATAL_PROGRAM_ERROR(alternative->LineNumber())
<< "Alternative pattern must have the form of a field access.";
}
const auto& field_access = cast<FieldAccessExpression>(*alternative);
choice_type = field_access.Aggregate();
alternative_name = field_access.Field();
}
auto ParenExpressionToParenPattern(const ParenContents<Expression>& contents)
-> ParenContents<Pattern> {
ParenContents<Pattern> result = {
.elements = {}, .has_trailing_comma = contents.has_trailing_comma};
for (const auto& element : contents.elements) {
result.elements.push_back(
{.name = element.name,
.term = global_arena->New<ExpressionPattern>(element.term)});
}
return result;
}
} // namespace Carbon