assoc_list.h 1009 B

1234567891011121314151617181920212223242526272829303132333435363738
  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_ASSOC_LIST_H_
  5. #define EXECUTABLE_SEMANTICS_INTERPRETER_ASSOC_LIST_H_
  6. #include <iostream>
  7. #include <list>
  8. #include <string>
  9. namespace Carbon {
  10. template <class K, class V>
  11. struct AssocList {
  12. AssocList(K k, V v, AssocList* n) : key(k), value(v), next(n) {}
  13. K key;
  14. V value;
  15. AssocList* next;
  16. };
  17. template <class K, class V>
  18. auto Lookup(int line_num, AssocList<K, V>* alist, const K& key,
  19. void (*print_key)(const K&)) -> V {
  20. if (alist == NULL) {
  21. std::cerr << line_num << ": could not find `" << key << "`" << std::endl;
  22. exit(-1);
  23. } else if (alist->key == key) {
  24. return alist->value;
  25. } else {
  26. return Lookup(line_num, alist->next, key, print_key);
  27. }
  28. }
  29. } // namespace Carbon
  30. #endif // EXECUTABLE_SEMANTICS_INTERPRETER_ASSOC_LIST_H_