mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 09:11:05 +01:00
Use llvm::ListSeparator for simpler separators (#671)
I was looking for something like this, I think it's a straightforward simplification for code. Internally, it handles skipping the separator on the first print. See the bottom of: https://llvm.org/doxygen/StringExtras_8h_source.html
This commit is contained in:
@@ -5,6 +5,7 @@
|
||||
#include "executable_semantics/ast/expression.h"
|
||||
|
||||
#include "executable_semantics/common/error.h"
|
||||
#include "llvm/ADT/StringExtras.h"
|
||||
|
||||
namespace Carbon {
|
||||
|
||||
@@ -221,12 +222,9 @@ static void PrintOp(llvm::raw_ostream& out, Operator op) {
|
||||
|
||||
static void PrintFields(llvm::raw_ostream& out,
|
||||
const std::vector<FieldInitializer>& fields) {
|
||||
int i = 0;
|
||||
for (auto iter = fields.begin(); iter != fields.end(); ++iter, ++i) {
|
||||
if (i != 0) {
|
||||
out << ", ";
|
||||
}
|
||||
out << iter->name << " = " << *iter->expression;
|
||||
llvm::ListSeparator sep;
|
||||
for (const auto& field : fields) {
|
||||
out << sep << field.name << " = " << field.expression;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
#include "executable_semantics/ast/expression.h"
|
||||
#include "executable_semantics/ast/function_definition.h"
|
||||
#include "executable_semantics/interpreter/stack.h"
|
||||
#include "llvm/ADT/StringExtras.h"
|
||||
|
||||
namespace Carbon {
|
||||
|
||||
@@ -74,24 +75,21 @@ void Action::Print(llvm::raw_ostream& out) const {
|
||||
out << "<" << pos << ">";
|
||||
if (results.size() > 0) {
|
||||
out << "(";
|
||||
llvm::ListSeparator sep;
|
||||
for (auto& result : results) {
|
||||
out << sep;
|
||||
if (result) {
|
||||
out << *result;
|
||||
}
|
||||
out << ",";
|
||||
}
|
||||
out << ")";
|
||||
}
|
||||
}
|
||||
|
||||
void Action::PrintList(const Stack<Action*>& ls, llvm::raw_ostream& out) {
|
||||
auto it = ls.begin();
|
||||
while (it != ls.end()) {
|
||||
out << **it;
|
||||
++it;
|
||||
if (it != ls.end()) {
|
||||
out << " :: ";
|
||||
}
|
||||
llvm::ListSeparator sep(" :: ");
|
||||
for (const auto& action : ls) {
|
||||
out << sep << *action;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,6 +4,8 @@
|
||||
|
||||
#include "executable_semantics/interpreter/heap.h"
|
||||
|
||||
#include "llvm/ADT/StringExtras.h"
|
||||
|
||||
namespace Carbon {
|
||||
|
||||
auto Heap::AllocateValue(const Value* v) -> Address {
|
||||
@@ -48,9 +50,10 @@ void Heap::Deallocate(const Address& address) {
|
||||
}
|
||||
|
||||
void Heap::Print(llvm::raw_ostream& out) const {
|
||||
llvm::ListSeparator sep;
|
||||
for (size_t i = 0; i < values_.size(); ++i) {
|
||||
out << sep;
|
||||
PrintAddress(Address(i), out);
|
||||
out << ", ";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
#include "executable_semantics/interpreter/action.h"
|
||||
#include "executable_semantics/interpreter/frame.h"
|
||||
#include "executable_semantics/interpreter/stack.h"
|
||||
#include "llvm/ADT/StringExtras.h"
|
||||
|
||||
namespace Carbon {
|
||||
|
||||
@@ -31,10 +32,10 @@ void Step();
|
||||
//
|
||||
|
||||
void PrintEnv(Env values, llvm::raw_ostream& out) {
|
||||
llvm::ListSeparator sep;
|
||||
for (const auto& [name, address] : values) {
|
||||
out << name << ": ";
|
||||
out << sep << name << ": ";
|
||||
state->heap.PrintAddress(address, out);
|
||||
out << ", ";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,13 +44,9 @@ void PrintEnv(Env values, llvm::raw_ostream& out) {
|
||||
//
|
||||
|
||||
void PrintStack(const Stack<Frame*>& ls, llvm::raw_ostream& out) {
|
||||
auto it = ls.begin();
|
||||
while (it != ls.end()) {
|
||||
out << **it;
|
||||
++it;
|
||||
if (it != ls.end()) {
|
||||
out << " :: ";
|
||||
}
|
||||
llvm::ListSeparator sep(" :: ");
|
||||
for (const auto& frame : ls) {
|
||||
out << sep << *frame;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
#include <algorithm>
|
||||
|
||||
#include "common/check.h"
|
||||
#include "llvm/ADT/StringExtras.h"
|
||||
|
||||
namespace Carbon {
|
||||
|
||||
@@ -373,15 +374,9 @@ void Value::Print(llvm::raw_ostream& out) const {
|
||||
}
|
||||
case ValKind::TupleValue: {
|
||||
out << "(";
|
||||
bool add_commas = false;
|
||||
llvm::ListSeparator sep;
|
||||
for (const TupleElement& element : GetTupleValue().elements) {
|
||||
if (add_commas) {
|
||||
out << ", ";
|
||||
} else {
|
||||
add_commas = true;
|
||||
}
|
||||
|
||||
out << element.name << " = " << *element.value;
|
||||
out << sep << element.name << " = " << *element.value;
|
||||
}
|
||||
out << ")";
|
||||
break;
|
||||
|
||||
Reference in New Issue
Block a user