interpreter.h 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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 CARBON_EXPLORER_INTERPRETER_INTERPRETER_H_
  5. #define CARBON_EXPLORER_INTERPRETER_INTERPRETER_H_
  6. #include <optional>
  7. #include <utility>
  8. #include <vector>
  9. #include "common/ostream.h"
  10. #include "explorer/ast/ast.h"
  11. #include "explorer/ast/declaration.h"
  12. #include "explorer/ast/expression.h"
  13. #include "explorer/ast/pattern.h"
  14. #include "explorer/interpreter/action.h"
  15. #include "explorer/interpreter/heap.h"
  16. #include "explorer/interpreter/trace_stream.h"
  17. #include "explorer/interpreter/value.h"
  18. #include "llvm/ADT/ArrayRef.h"
  19. namespace Carbon {
  20. // Interprets the program defined by `ast`, allocating values on `arena` and
  21. // printing traces if `trace` is true.
  22. auto InterpProgram(const AST& ast, Nonnull<Arena*> arena,
  23. Nonnull<TraceStream*> trace_stream) -> 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. Nonnull<TraceStream*> trace_stream)
  29. -> ErrorOr<Nonnull<const Value*>>;
  30. // Attempts to match `v` against the pattern `p`, returning whether matching
  31. // is successful. If it is, populates **bindings with the variables bound by
  32. // the match; `bindings` should only be nullopt in contexts where `p`
  33. // is not permitted to bind variables. **bindings may be modified even if the
  34. // match is unsuccessful, so it should typically be created for the
  35. // PatternMatch call and then merged into an existing scope on success.
  36. // The matches for generic variables in the pattern are output in
  37. // `generic_args`.
  38. // TODO: consider moving this to a separate header.
  39. [[nodiscard]] auto PatternMatch(
  40. Nonnull<const Value*> p, Nonnull<const Value*> v, SourceLocation source_loc,
  41. std::optional<Nonnull<RuntimeScope*>> bindings, BindingMap& generic_args,
  42. Nonnull<TraceStream*> trace_stream, Nonnull<Arena*> arena) -> bool;
  43. } // namespace Carbon
  44. #endif // CARBON_EXPLORER_INTERPRETER_INTERPRETER_H_