// Part of the Carbon Language project, under the Apache License v2.0 with LLVM // Exceptions. See /LICENSE for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception #ifndef EXECUTABLE_SEMANTICS_INTERPRETER_INTERPRETER_H_ #define EXECUTABLE_SEMANTICS_INTERPRETER_INTERPRETER_H_ #include #include #include #include "common/ostream.h" #include "executable_semantics/ast/declaration.h" #include "executable_semantics/ast/expression.h" #include "executable_semantics/ast/pattern.h" #include "executable_semantics/interpreter/action.h" #include "executable_semantics/interpreter/action_stack.h" #include "executable_semantics/interpreter/heap.h" #include "executable_semantics/interpreter/value.h" #include "llvm/ADT/ArrayRef.h" namespace Carbon { class Interpreter { public: explicit Interpreter(Nonnull arena, bool trace) : arena_(arena), globals_(arena), heap_(arena), trace_(trace) {} // Interpret the whole program. auto InterpProgram(llvm::ArrayRef> fs, Nonnull call_main) -> int; // Interpret an expression at compile-time. auto InterpExp(Env values, Nonnull e) -> Nonnull; // Interpret a pattern at compile-time. auto InterpPattern(Env values, Nonnull p) -> Nonnull; // Attempts to match `v` against the pattern `p`. If matching succeeds, // returns the bindings of pattern variables to their matched values. auto PatternMatch(Nonnull p, Nonnull v, SourceLocation source_loc) -> std::optional; // Support TypeChecker allocating values on the heap. auto AllocateValue(Nonnull v) -> AllocationId { return heap_.AllocateValue(v); } void InitEnv(const Declaration& d, Env* env); void PrintEnv(Env values, llvm::raw_ostream& out); private: void Step(); // State transitions for expressions. void StepExp(); // State transitions for lvalues. void StepLvalue(); // State transitions for patterns. void StepPattern(); // State transition for statements. void StepStmt(); void InitGlobals(llvm::ArrayRef> fs); auto CurrentEnv() -> Env; auto GetFromEnv(SourceLocation source_loc, const std::string& name) -> Address; auto CreateStruct(const std::vector& fields, const std::vector>& values) -> Nonnull; auto EvalPrim(Operator op, const std::vector>& args, SourceLocation source_loc) -> Nonnull; // Returns the result of converting `value` to type `destination_type`. auto Convert(Nonnull value, Nonnull destination_type) const -> Nonnull; void PrintState(llvm::raw_ostream& out); // Runs `action` in a scope consisting of `values`, and returns the result. // `action` must produce a result. In other words, it must not be a // StatementAction or ScopeAction. // // TODO: consider whether to use this->trace_ rather than a separate // trace_steps parameter. auto ExecuteAction(std::unique_ptr action, Env values, bool trace_steps) -> Nonnull; Nonnull arena_; // Globally-defined entities, such as functions, structs, or choices. Env globals_; ActionStack todo_; Heap heap_; // The underlying states of continuation values. All StackFragments created // during execution are tracked here, in order to safely deallocate the // contents of any non-completed continuations at the end of execution. std::vector> stack_fragments_; bool trace_; }; } // namespace Carbon #endif // EXECUTABLE_SEMANTICS_INTERPRETER_INTERPRETER_H_