Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions eval/eval/evaluator_core.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <cstddef>
#include <utility>

#include "absl/base/attributes.h"
#include "absl/base/nullability.h"
#include "absl/base/optimization.h"
#include "absl/log/absl_check.h"
Expand All @@ -39,6 +40,23 @@ void FlatExpressionEvaluatorState::Reset() {
}

const ExpressionStep* ExecutionFrame::Next() {
if (ABSL_PREDICT_TRUE(pc_ < execution_path_.size())) {
const ExpressionStep* step = &execution_path_[pc_++];
ABSL_ASSUME(step != nullptr);
return step;
}
// Normal completion of the top-level program. Handled inline since every
// evaluation ends here.
if (ABSL_PREDICT_TRUE(pc_ == execution_path_.size() && call_stack_.empty())) {
return nullptr;
}
return ReturnFromSubexpression();
}

// Kept out of line so that `Next()` stays small enough to inline into the
// `Evaluate` loop.
ABSL_ATTRIBUTE_NOINLINE const ExpressionStep*
ExecutionFrame::ReturnFromSubexpression() {
while (true) {
const size_t end_pos = execution_path_.size();

Expand Down
6 changes: 6 additions & 0 deletions eval/eval/evaluator_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,12 @@ class ExecutionFrame : public ExecutionFrameBase {
size_t expected_stack_size;
};

// Called by `Next()` when `pc_` reaches the end of the current execution
// path. If a subexpression is being evaluated, returns to the caller (storing
// the result in the caller's comprehension slot) and returns the caller's
// next step. Returns nullptr once the top-level expression is complete.
const ExpressionStep* ReturnFromSubexpression();

size_t pc_; // pc_ - Program Counter. Current position on execution path.
ExecutionPathView execution_path_;
EvaluatorStack* absl_nonnull const value_stack_;
Expand Down
Loading