interpreter.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 e, std::list<std::string> l) : env(e), locals(std::move(l)) {}
  19. Env env;
  20. std::list<std::string> locals;
  21. };
  22. /***** Frames and State *****/
  23. // A frame represents either a function call or a delimited continuation.
  24. struct Frame {
  25. // The name of the function.
  26. std::string name;
  27. // If the frame represents a function call, the bottom scope
  28. // contains the parameter-argument bindings for this function
  29. // call. The rest of the scopes contain local variables defined by
  30. // blocks within the function. The scope at the top of the stack is
  31. // the current scope and its environment is the one used for looking
  32. // up the value associated with a variable.
  33. Stack<Scope*> scopes;
  34. // The actions that need to be executed in the future of the
  35. // current function call. The top of the stack is the action
  36. // that is executed first.
  37. Stack<Action*> todo;
  38. // If this frame is the bottom frame of a continuation, then it stores
  39. // the address of the continuation.
  40. // Otherwise the `continuation` field is the sentinel UINT_MAX.
  41. Address continuation;
  42. // Returns whether this frame is the bottom frame of a continuation.
  43. auto IsContinuation() -> bool { return continuation != UINT_MAX; }
  44. Frame(std::string n, Stack<Scope*> s, Stack<Action*> c)
  45. : name(std::move(std::move(n))),
  46. scopes(s),
  47. todo(c),
  48. continuation(UINT_MAX) {}
  49. };
  50. struct State {
  51. Stack<Frame*> stack;
  52. std::vector<const Value*> heap;
  53. std::vector<bool> alive;
  54. };
  55. extern State* state;
  56. auto PrintFrame(Frame* frame, std::ostream& out) -> void;
  57. void PrintStack(Stack<Frame*> ls, std::ostream& out);
  58. void PrintEnv(Env env);
  59. auto AllocateValue(const Value* v) -> Address;
  60. auto CopyVal(const Value* val, int line_num) -> const Value*;
  61. auto ToInteger(const Value* v) -> int;
  62. /***** Interpreters *****/
  63. auto InterpProgram(std::list<Declaration>* fs) -> int;
  64. auto InterpExp(Env env, Expression* e) -> const Value*;
  65. } // namespace Carbon
  66. #endif // EXECUTABLE_SEMANTICS_INTERPRETER_INTERPRETER_H_