Files
carbon-lang/executable_semantics/interpreter/cons_list.h
T
Dave Abrahams 29a59944fa Executable Semantics: 1st-class stacks (#296)
Replaces low-level uses of `Cons` with a first-class `Stack` data structure.  Also removes an unused algorithm.
Co-authored-by: Jeremy Siek <jsiek@indiana.edu>

Possible next step: use `std::stack` instead.
2021-02-26 09:46:32 -08:00

41 lines
895 B
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 EXECUTABLE_SEMANTICS_INTERPRETER_CONS_LIST_H_
#define EXECUTABLE_SEMANTICS_INTERPRETER_CONS_LIST_H_
namespace Carbon {
template <class T>
struct Stack;
template <class T>
struct Cons {
friend struct Stack<T>;
private:
Cons(T e, Cons* n) : curr(e), next(n) {}
const T curr;
Cons* const next;
// Cons cells are part of a "persistent data structure" and are thus
// immutable.
Cons& operator=(const Cons&) = delete;
Cons& operator=(Cons&&) = delete;
};
template <class T>
auto Length(Cons<T>* ls) -> unsigned int {
if (ls) {
return 1 + Length(ls->next);
} else {
return 0;
}
}
} // namespace Carbon
#endif // EXECUTABLE_SEMANTICS_INTERPRETER_CONS_LIST_H_