mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-06 07:34:45 +01:00
Unify Action and Scope stacks, and eliminate Frame (#880)
Co-authored-by: Jon Meow <46229924+jonmeow@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
co-authored by
Jon Meow
parent
44154c8663
commit
3bec7f8dc0
@@ -13,7 +13,7 @@
|
||||
#include "executable_semantics/ast/declaration.h"
|
||||
#include "executable_semantics/ast/expression.h"
|
||||
#include "executable_semantics/ast/pattern.h"
|
||||
#include "executable_semantics/interpreter/frame.h"
|
||||
#include "executable_semantics/interpreter/action.h"
|
||||
#include "executable_semantics/interpreter/heap.h"
|
||||
#include "executable_semantics/interpreter/stack.h"
|
||||
#include "executable_semantics/interpreter/value.h"
|
||||
@@ -21,8 +21,6 @@
|
||||
|
||||
namespace Carbon {
|
||||
|
||||
using Env = Dictionary<std::string, Address>;
|
||||
|
||||
class Interpreter {
|
||||
public:
|
||||
explicit Interpreter(Nonnull<Arena*> arena, bool trace)
|
||||
@@ -85,16 +83,19 @@ class Interpreter {
|
||||
// and increments its position counter.
|
||||
struct RunAgain {};
|
||||
|
||||
// Transition type which unwinds the `todo` and `scopes` stacks until it
|
||||
// reaches a specified Action lower in the stack.
|
||||
// Transition type which unwinds the `todo` stack until it reaches the
|
||||
// StatementAction associated with `ast_node`. Execution then resumes with
|
||||
// that StatementAction.
|
||||
struct UnwindTo {
|
||||
const Nonnull<Action*> new_top;
|
||||
Nonnull<const Statement*> ast_node;
|
||||
};
|
||||
|
||||
// Transition type which unwinds the entire current stack frame, and returns
|
||||
// a specified value to the caller.
|
||||
struct UnwindFunctionCall {
|
||||
Nonnull<const Value*> return_val;
|
||||
// Transition type which unwinds the `todo` stack down to and including the
|
||||
// StatementAction associated with `ast_node`. If `result` is set, it will be
|
||||
// treated as the result of that StatementAction.
|
||||
struct UnwindPast {
|
||||
Nonnull<const Statement*> ast_node;
|
||||
std::optional<Nonnull<const Value*>> result;
|
||||
};
|
||||
|
||||
// Transition type which removes the current action from the top of the todo
|
||||
@@ -112,12 +113,12 @@ class Interpreter {
|
||||
// uses of this type should be replaced with meaningful transitions.
|
||||
struct ManualTransition {};
|
||||
|
||||
using Transition =
|
||||
std::variant<Done, Spawn, Delegate, RunAgain, UnwindTo,
|
||||
UnwindFunctionCall, CallFunction, ManualTransition>;
|
||||
using Transition = std::variant<Done, Spawn, Delegate, RunAgain, UnwindTo,
|
||||
UnwindPast, CallFunction, ManualTransition>;
|
||||
|
||||
// Visitor which implements the behavior associated with each transition type.
|
||||
class DoTransition;
|
||||
friend class DoTransition;
|
||||
|
||||
void Step();
|
||||
|
||||
@@ -131,12 +132,12 @@ class Interpreter {
|
||||
auto StepStmt() -> Transition;
|
||||
|
||||
void InitGlobals(llvm::ArrayRef<Nonnull<Declaration*>> fs);
|
||||
auto CurrentScope() -> Scope&;
|
||||
auto CurrentEnv() -> Env;
|
||||
auto GetFromEnv(SourceLocation source_loc, const std::string& name)
|
||||
-> Address;
|
||||
|
||||
void DeallocateScope(Nonnull<Scope*> scope);
|
||||
void DeallocateLocals(Nonnull<Frame*> frame);
|
||||
void DeallocateScope(Scope& scope);
|
||||
|
||||
auto CreateTuple(Nonnull<Action*> act, Nonnull<const Expression*> exp)
|
||||
-> Nonnull<const Value*>;
|
||||
@@ -157,14 +158,22 @@ class Interpreter {
|
||||
|
||||
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(Nonnull<Action*> action, Env values, bool trace_steps)
|
||||
-> Nonnull<const Value*>;
|
||||
|
||||
Nonnull<Arena*> arena_;
|
||||
|
||||
// Globally-defined entities, such as functions, structs, or choices.
|
||||
Env globals_;
|
||||
|
||||
Stack<Nonnull<Frame*>> stack_;
|
||||
Stack<Nonnull<Action*>> todo_;
|
||||
Heap heap_;
|
||||
std::optional<Nonnull<const Value*>> program_value_;
|
||||
|
||||
bool trace_;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user