interpreter.h 2.7 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 <optional>
  7. #include <utility>
  8. #include <vector>
  9. #include "common/ostream.h"
  10. #include "executable_semantics/ast/ast.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/action.h"
  15. #include "executable_semantics/interpreter/heap.h"
  16. #include "executable_semantics/interpreter/value.h"
  17. #include "llvm/ADT/ArrayRef.h"
  18. namespace Carbon {
  19. // Interprets the program defined by `ast`, allocating values on `arena` and
  20. // printing traces if `trace` is true.
  21. auto InterpProgram(const AST& ast, Nonnull<Arena*> arena,
  22. std::optional<Nonnull<llvm::raw_ostream*>> trace_stream)
  23. -> ErrorOr<int>;
  24. // Interprets `e` at compile-time, allocating values on `arena` and
  25. // printing traces if `trace` is true. The caller must ensure that all the
  26. // code this evaluates has been typechecked.
  27. auto InterpExp(Nonnull<const Expression*> e, Nonnull<Arena*> arena,
  28. std::optional<Nonnull<llvm::raw_ostream*>> trace_stream)
  29. -> ErrorOr<Nonnull<const Value*>>;
  30. // Interprets `p` at compile-time, allocating values on `arena` and
  31. // printing traces if `trace` is true. The caller must ensure that all the
  32. // code this evaluates has been typechecked.
  33. auto InterpPattern(Nonnull<const Pattern*> p, Nonnull<Arena*> arena,
  34. std::optional<Nonnull<llvm::raw_ostream*>> trace_stream)
  35. -> ErrorOr<Nonnull<const Value*>>;
  36. // Attempts to match `v` against the pattern `p`, returning whether matching
  37. // is successful. If it is, populates **bindings with the variables bound by
  38. // the match; `bindings` should only be nullopt in contexts where `p`
  39. // is not permitted to bind variables. **bindings may be modified even if the
  40. // match is unsuccessful, so it should typically be created for the
  41. // PatternMatch call and then merged into an existing scope on success.
  42. // The matches for generic variables in the pattern are output in
  43. // `generic_args`.
  44. // TODO: consider moving this to a separate header.
  45. [[nodiscard]] auto PatternMatch(
  46. Nonnull<const Value*> p, Nonnull<const Value*> v, SourceLocation source_loc,
  47. std::optional<Nonnull<RuntimeScope*>> bindings, BindingMap& generic_args,
  48. std::optional<Nonnull<llvm::raw_ostream*>> trace_stream) -> bool;
  49. } // namespace Carbon
  50. #endif // EXECUTABLE_SEMANTICS_INTERPRETER_INTERPRETER_H_