interpreter.h 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. struct Frame {
  24. std::string name;
  25. Stack<Scope*> scopes;
  26. Stack<Action*> todo;
  27. Frame(std::string n, Stack<Scope*> s, Stack<Action*> c)
  28. : name(std::move(std::move(n))), scopes(s), todo(c) {}
  29. };
  30. struct State {
  31. Stack<Frame*> stack;
  32. std::vector<const Value*> heap;
  33. std::vector<bool> alive;
  34. };
  35. extern State* state;
  36. void PrintEnv(Env env);
  37. auto AllocateValue(const Value* v) -> Address;
  38. auto CopyVal(const Value* val, int line_num) -> const Value*;
  39. auto ToInteger(const Value* v) -> int;
  40. /***** Interpreters *****/
  41. auto InterpProgram(std::list<Declaration>* fs) -> int;
  42. auto InterpExp(Env env, Expression* e) -> const Value*;
  43. } // namespace Carbon
  44. #endif // EXECUTABLE_SEMANTICS_INTERPRETER_INTERPRETER_H_