interpreter.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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_INTERPRETER_H_
  5. #define EXECUTABLE_SEMANTICS_INTERPRETER_INTERPRETER_H_
  6. #include <list>
  7. #include <utility>
  8. #include <vector>
  9. #include "executable_semantics/ast/declaration.h"
  10. #include "executable_semantics/interpreter/action.h"
  11. #include "executable_semantics/interpreter/dictionary.h"
  12. #include "executable_semantics/interpreter/stack.h"
  13. #include "executable_semantics/interpreter/value.h"
  14. namespace Carbon {
  15. using Env = Dictionary<std::string, Address>;
  16. /***** Scopes *****/
  17. struct Scope {
  18. Scope(Env values, std::list<std::string> l)
  19. : values(values), locals(std::move(l)) {}
  20. Env values;
  21. std::list<std::string> locals;
  22. };
  23. /***** Frames and State *****/
  24. // A frame represents either a function call or a delimited continuation.
  25. struct Frame {
  26. // The name of the function.
  27. std::string name;
  28. // If the frame represents a function call, the bottom scope
  29. // contains the parameter-argument bindings for this function
  30. // call. The rest of the scopes contain local variables defined by
  31. // blocks within the function. The scope at the top of the stack is
  32. // the current scope and its environment is the one used for looking
  33. // up the value associated with a variable.
  34. Stack<Scope*> scopes;
  35. // The actions that need to be executed in the future of the
  36. // current function call. The top of the stack is the action
  37. // that is executed first.
  38. Stack<Action*> todo;
  39. // If this frame is the bottom frame of a continuation, then it stores
  40. // the address of the continuation.
  41. std::optional<Address> continuation;
  42. Frame(std::string n, Stack<Scope*> s, Stack<Action*> c)
  43. : name(std::move(std::move(n))), scopes(s), todo(c), continuation() {}
  44. };
  45. // A Heap represents the abstract machine's dynamically allocated memory.
  46. class Heap {
  47. public:
  48. // Constructs an empty Heap.
  49. Heap() = default;
  50. Heap(const Heap&) = delete;
  51. Heap& operator=(const Heap&) = delete;
  52. // Returns the value at the given address in the heap after
  53. // checking that it is alive.
  54. auto Read(const Address& a, int line_num) -> const Value*;
  55. // Writes the given value at the address in the heap after
  56. // checking that the address is alive.
  57. auto Write(const Address& a, const Value* v, int line_num) -> void;
  58. // Put the given value on the heap and mark it as alive.
  59. auto AllocateValue(const Value* v) -> Address;
  60. // Marks the object at this address, and all of its sub-objects, as dead.
  61. auto Deallocate(const Address& address) -> void;
  62. // Print the value at the given address to the stream `out`.
  63. auto PrintAddress(const Address& a, std::ostream& out) -> void;
  64. // Print all the values on the heap to the stream `out`.
  65. auto PrintHeap(std::ostream& out) -> void;
  66. private:
  67. // Signal an error if the address is no longer alive.
  68. void CheckAlive(const Address& address, int line_num);
  69. std::vector<const Value*> values_;
  70. std::vector<bool> alive_;
  71. };
  72. struct State {
  73. Stack<Frame*> stack;
  74. Heap heap;
  75. };
  76. extern State* state;
  77. void InitEnv(const Declaration& d, Env* env);
  78. void PrintFrame(Frame* frame, std::ostream& out);
  79. void PrintStack(Stack<Frame*> ls, std::ostream& out);
  80. void PrintEnv(Env values);
  81. auto CopyVal(const Value* val, int line_num) -> const Value*;
  82. auto ToInteger(const Value* v) -> int;
  83. /***** Interpreters *****/
  84. auto InterpProgram(std::list<Declaration>* fs) -> int;
  85. auto InterpExp(Env values, const Expression* e) -> const Value*;
  86. } // namespace Carbon
  87. #endif // EXECUTABLE_SEMANTICS_INTERPRETER_INTERPRETER_H_