interpreter.h 1.6 KB

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