interpreter.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. // Otherwise the `continuation` field is the sentinel UINT_MAX.
  42. Address continuation;
  43. // Returns whether this frame is the bottom frame of a continuation.
  44. auto IsContinuation() -> bool { return continuation != UINT_MAX; }
  45. Frame(std::string n, Stack<Scope*> s, Stack<Action*> c)
  46. : name(std::move(std::move(n))),
  47. scopes(s),
  48. todo(c),
  49. continuation(UINT_MAX) {}
  50. };
  51. struct State {
  52. Stack<Frame*> stack;
  53. std::vector<const Value*> heap;
  54. std::vector<bool> alive;
  55. };
  56. extern State* state;
  57. auto PrintFrame(Frame* frame, std::ostream& out) -> void;
  58. void PrintStack(Stack<Frame*> ls, std::ostream& out);
  59. void PrintEnv(Env values);
  60. auto AllocateValue(const Value* v) -> Address;
  61. auto CopyVal(const Value* val, int line_num) -> const Value*;
  62. auto ToInteger(const Value* v) -> int;
  63. /***** Interpreters *****/
  64. auto InterpProgram(std::list<Declaration>* fs) -> int;
  65. auto InterpExp(Env values, const Expression* e) -> const Value*;
  66. } // namespace Carbon
  67. #endif // EXECUTABLE_SEMANTICS_INTERPRETER_INTERPRETER_H_