interpreter.h 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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 "common/ostream.h"
  10. #include "executable_semantics/ast/declaration.h"
  11. #include "executable_semantics/ast/expression.h"
  12. #include "executable_semantics/ast/pattern.h"
  13. #include "executable_semantics/interpreter/frame.h"
  14. #include "executable_semantics/interpreter/heap.h"
  15. #include "executable_semantics/interpreter/stack.h"
  16. #include "executable_semantics/interpreter/value.h"
  17. namespace Carbon {
  18. using Env = Dictionary<std::string, Address>;
  19. struct State {
  20. Stack<Ptr<Frame>> stack;
  21. Heap heap;
  22. };
  23. extern State* state;
  24. void InitEnv(const Declaration& d, Env* env);
  25. void PrintStack(const Stack<Frame*>& ls, llvm::raw_ostream& out);
  26. void PrintEnv(Env values, llvm::raw_ostream& out);
  27. /***** Interpreters *****/
  28. // Attempts to match `v` against the pattern `p`. If matching succeeds, returns
  29. // the bindings of pattern variables to their matched values.
  30. auto PatternMatch(const Value* p, const Value* v, int line_num)
  31. -> std::optional<Env>;
  32. auto InterpProgram(const std::list<Ptr<const Declaration>>& fs) -> int;
  33. auto InterpExp(Env values, const Expression* e) -> const Value*;
  34. auto InterpPattern(Env values, const Pattern* p) -> const Value*;
  35. } // namespace Carbon
  36. #endif // EXECUTABLE_SEMANTICS_INTERPRETER_INTERPRETER_H_