frame.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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_FRAME_H_
  5. #define EXECUTABLE_SEMANTICS_INTERPRETER_FRAME_H_
  6. #include <list>
  7. #include <string>
  8. #include "common/ostream.h"
  9. #include "executable_semantics/interpreter/action.h"
  10. #include "executable_semantics/interpreter/address.h"
  11. #include "executable_semantics/interpreter/dictionary.h"
  12. #include "executable_semantics/interpreter/stack.h"
  13. namespace Carbon {
  14. using Env = Dictionary<std::string, Address>;
  15. struct Scope {
  16. Scope(Env values, std::list<std::string> l)
  17. : values(values), locals(std::move(l)) {}
  18. Env values;
  19. std::list<std::string> locals;
  20. };
  21. // A frame represents either a function call or a delimited continuation.
  22. struct Frame {
  23. // The name of the function.
  24. std::string name;
  25. // If the frame represents a function call, the bottom scope
  26. // contains the parameter-argument bindings for this function
  27. // call. The rest of the scopes contain local variables defined by
  28. // blocks within the function. The scope at the top of the stack is
  29. // the current scope and its environment is the one used for looking
  30. // up the value associated with a variable.
  31. Stack<Scope*> scopes;
  32. // The actions that need to be executed in the future of the
  33. // current function call. The top of the stack is the action
  34. // that is executed first.
  35. Stack<Action*> todo;
  36. // If this frame is the bottom frame of a continuation, then it stores
  37. // the address of the continuation.
  38. std::optional<Address> continuation;
  39. Frame(const Frame&) = delete;
  40. Frame& operator=(const Frame&) = delete;
  41. Frame(std::string n, Stack<Scope*> s, Stack<Action*> c)
  42. : name(std::move(std::move(n))), scopes(s), todo(c), continuation() {}
  43. void Print(llvm::raw_ostream& out) const;
  44. };
  45. } // namespace Carbon
  46. #endif // EXECUTABLE_SEMANTICS_INTERPRETER_FRAME_H_