interpreter.h 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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/assoc_list.h"
  12. #include "executable_semantics/interpreter/stack.h"
  13. #include "executable_semantics/interpreter/value.h"
  14. namespace Carbon {
  15. using Env = AssocList<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<Value*> heap;
  33. };
  34. extern State* state;
  35. void PrintEnv(Env* env);
  36. auto AllocateValue(Value* v) -> Address;
  37. auto CopyVal(Value* val, int line_num) -> Value*;
  38. auto ToInteger(Value* v) -> int;
  39. /***** Interpreters *****/
  40. auto InterpProgram(std::list<Declaration>* fs) -> int;
  41. auto InterpExp(Env* env, Expression* e) -> Value*;
  42. } // namespace Carbon
  43. #endif // EXECUTABLE_SEMANTICS_INTERPRETER_INTERPRETER_H_