cons_list.h 847 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #ifndef EXECUTABLE_SEMANTICS_INTERPRETER_CONS_LIST_H_
  5. #define EXECUTABLE_SEMANTICS_INTERPRETER_CONS_LIST_H_
  6. namespace Carbon {
  7. template <class T>
  8. struct Cons {
  9. Cons(T e, Cons* n) : curr(e), next(n) {}
  10. T curr;
  11. Cons* next;
  12. };
  13. template <class T>
  14. auto MakeCons(const T& x) -> Cons<T>* {
  15. return new Cons<T>(x, nullptr);
  16. }
  17. template <class T>
  18. auto MakeCons(const T& x, Cons<T>* ls) -> Cons<T>* {
  19. return new Cons<T>(x, ls);
  20. }
  21. template <class T>
  22. auto Length(Cons<T>* ls) -> unsigned int {
  23. if (ls) {
  24. return 1 + Length(ls->next);
  25. } else {
  26. return 0;
  27. }
  28. }
  29. } // namespace Carbon
  30. #endif // EXECUTABLE_SEMANTICS_INTERPRETER_CONS_LIST_H_