Files
carbon-lang/explorer/interpreter/stack.h
T
Jon Meow 309ec35f95 Rename executable_semantics to explorer (#1188)
Change generated with:

```
#!/usr/bin/bash -eux

# Helper script for renaming pending work.
# Run from the repo root.

# Rename executable_semantics in code.
sed -i 's/executable_semantics/explorer/g' \
  $(git grep -l 'executable_semantics' . | grep -v proposals)
sed -i 's/executable semantics/explorer/g' \
  $(git grep -l 'executable semantics' . | grep -v proposals)
sed -i 's/Executable semantics/Explorer/g' \
  $(git grep -l 'Executable semantics' . |  grep -v proposals)
sed -i 's/Executable Semantics/Explorer/g' \
  $(git grep -l 'Executable Semantics' . |  grep -v proposals)
sed -i 's/EXECUTABLE_SEMANTICS/EXPLORER/g' \
  $(git grep -l 'EXECUTABLE_SEMANTICS' . | grep -v proposals)
sed -i 's/ExecutableSemantics/Explorer/g' \
  $(git grep -l 'ExecutableSemantics' . | grep -v proposals)
sed -i 's/executable-semantics/explorer/g' \
  $(git grep -l 'executable-semantics' . | grep -v proposals)

# This is only needed for the initial move.
mv executable_semantics explorer
mv explorer/fuzzing/executable_semantics_fuzzer.cpp explorer/fuzzing/explorer_fuzzer.cpp
```

Verified with `bazel test ...`
2022-04-29 13:20:25 -07:00

76 lines
2.1 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
#ifndef EXPLORER_INTERPRETER_STACK_H_
#define EXPLORER_INTERPRETER_STACK_H_
#include <cstddef>
#include <iterator>
#include <vector>
#include "common/check.h"
namespace Carbon {
// A stack data structure.
template <class T>
struct Stack {
// NOLINTNEXTLINE(readability-identifier-naming)
using const_iterator = typename std::vector<T>::const_reverse_iterator;
// Creates an empty instance.
Stack() = default;
// Creates an instance containing just `x`.
explicit Stack(T x) : Stack() { Push(std::move(x)); }
// Pushes `x` onto the top of the stack.
void Push(T x) { elements_.push_back(std::move(x)); }
// Removes and returns the top element of the stack.
//
// - Requires: !this->IsEmpty()
auto Pop() -> T {
CHECK(!IsEmpty()) << "Can't pop from empty stack.";
auto r = std::move(elements_.back());
elements_.pop_back();
return r;
}
// Removes the top `n` elements of the stack.
//
// - Requires: n >= 0 && n <= Count()
void Pop(int n) {
CHECK(n >= 0) << "Negative pop count disallowed.";
CHECK(static_cast<size_t>(n) <= elements_.size())
<< "Can only pop as many elements as stack has.";
elements_.erase(elements_.end() - n, elements_.end());
}
// Returns the top element of the stack.
//
// - Requires: !this->IsEmpty()
auto Top() const -> const T& {
CHECK(!IsEmpty()) << "Empty stack has no Top().";
return elements_.back();
}
// Returns `true` iff `Count() > 0`.
auto IsEmpty() const -> bool { return elements_.empty(); }
// Returns the number of elements in `*this`.
auto Count() const -> int { return elements_.size(); }
// Iterates over the Stack from top to bottom.
auto begin() const -> const_iterator { return elements_.crbegin(); }
auto end() const -> const_iterator { return elements_.crend(); }
private:
std::vector<T> elements_;
};
} // namespace Carbon
#endif // EXPLORER_INTERPRETER_CONS_LIST_H_