Store named constant values in the AST (#1011)

This enables us to stop using `Env` in the typechecker. As a byproduct, this commit also restructures the interpreter to handle run-time global initialization as part of ordinary execution, using the Action stack.
This commit is contained in:
Geoff Romer
2022-01-13 11:07:28 -08:00
committed by GitHub
parent d88f95ad31
commit 461e178273
15 changed files with 309 additions and 242 deletions
@@ -19,14 +19,17 @@ namespace Carbon {
class ActionStack {
public:
// Constructs an empty ActionStack
ActionStack() = default;
explicit ActionStack(Scope globals) : globals_(std::move(globals)) {}
void Print(llvm::raw_ostream& out) const;
LLVM_DUMP_METHOD void Dump() const { Print(llvm::errs()); }
// Starts execution with `action` at the top of the stack, in the given scope.
// `action` must be an `ExpressionAction` or `PatternAction`.
void Start(std::unique_ptr<Action> action, Scope scope);
// Returns an Env containing the currently-defined global variables.
auto GlobalEnv() const -> Env { return globals_.values(); }
// Starts execution with `action` at the top of the stack. Cannot be called
// when IsEmpty() is false.
void Start(std::unique_ptr<Action> action);
// True if the stack is empty.
auto IsEmpty() const -> bool { return todo_.IsEmpty(); }
@@ -97,6 +100,7 @@ class ActionStack {
// TODO: consider defining a non-nullable unique_ptr-like type to use here.
Stack<std::unique_ptr<Action>> todo_;
std::optional<Nonnull<const Value*>> result_;
mutable Scope globals_;
};
} // namespace Carbon