Add support for compound assignment and increment (#2526)

Add support for user-defined assignment, as well as compound assignment and increment, following the design direction in pending proposal #2511.

Some of this isn't fully testable yet: because explorer doesn't properly support `impl` specialization, the blanket `impl`s in the prelude prevent types from customizing assignment.
This commit is contained in:
Richard Smith
2023-01-20 18:48:13 -08:00
committed by GitHub
parent f1a0645551
commit 2fd7e2b65d
32 changed files with 975 additions and 215 deletions
+16
View File
@@ -1937,6 +1937,13 @@ auto Interpreter::StepStmt() -> ErrorOr<Success> {
}
case StatementKind::Assign: {
const auto& assign = cast<Assign>(stmt);
if (auto rewrite = assign.rewritten_form()) {
if (act.pos() == 0) {
return todo_.Spawn(std::make_unique<ExpressionAction>(*rewrite));
} else {
return todo_.FinishAction();
}
}
if (act.pos() == 0) {
// { {(lv = e) :: C, E, F} :: S, H}
// -> { {lv :: ([] = e) :: C, E, F} :: S, H}
@@ -1958,6 +1965,15 @@ auto Interpreter::StepStmt() -> ErrorOr<Success> {
return todo_.FinishAction();
}
}
case StatementKind::IncrementDecrement: {
const auto& inc_dec = cast<IncrementDecrement>(stmt);
if (act.pos() == 0) {
return todo_.Spawn(
std::make_unique<ExpressionAction>(*inc_dec.rewritten_form()));
} else {
return todo_.FinishAction();
}
}
case StatementKind::If:
if (act.pos() == 0) {
// { {(if (e) then_stmt else else_stmt) :: C, E, F} :: S, H}