mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-04 22:02:52 +01:00
Experimental control-flow operator (#368)
* AST and syntax for delimited control * stashing for later * a little more progress * progress on delimited continuations * delimit, suspend, and resume implemented (draft) * example that generates the natural numbers * fixes * tinkering * changed demo to experimental * comments and name changes * describe delimited continuations in the README * renamed Snapshot to Continuation, edits to comments * Update executable_semantics/ast/statement.h improve comment for MakeDelimitStmt Co-authored-by: Dave Abrahams <dabrahams@google.com> * Update executable_semantics/interpreter/interpreter.cpp remove snake_case Co-authored-by: Dave Abrahams <dabrahams@google.com> * edits to comments, change name of variable * updates to handle review edits * trailing whitespace * fixes to delimited continuations, added more tests, also fixed assignment to do a copy * improvements from Geoffrey * new test from Geoffrey, fix for empty blocks * more suggestions from Geoffrey * more tests for delimited continuations, renaming some of them * renamed test files * improve a comment * sketch of creating continuation * initial implementation of shift/reset style continuations * more documentation * fix some camel case * implemented deep copy of continuations, added a test case for it * fixed a bug and got the recursive test case working * removed __delimit, polished up __continuation * back to shallow copy for continuations * suggestions from Geoffrey * removed structured binding (for now) * Update executable_semantics/ast/expression.cpp Co-authored-by: Geoff Romer <gromer@google.com> * responses to Geoffrey Co-authored-by: Dave Abrahams <dabrahams@google.com> Co-authored-by: Geoff Romer <gromer@google.com>
This commit is contained in:
committed by
GitHub
co-authored by
Dave Abrahams
Geoff Romer
parent
6df0d51516
commit
3fa72d2984
@@ -0,0 +1,15 @@
|
||||
// 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
|
||||
|
||||
// Test that assignment performs a copy and does not create an alias.
|
||||
|
||||
fn main() -> Int {
|
||||
var Int: x = -1;
|
||||
{
|
||||
var Int: y = 0;
|
||||
x = y;
|
||||
// y dies here
|
||||
}
|
||||
return x;
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
result: 0
|
||||
@@ -0,0 +1,12 @@
|
||||
// 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
|
||||
|
||||
// Test that assignment performs a copy and does not create an alias.
|
||||
|
||||
fn main() -> Int {
|
||||
var Int: x = 0;
|
||||
var Int: y = x;
|
||||
x = 1;
|
||||
return y;
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
result: 0
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
// 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
|
||||
|
||||
fn main() -> Int {
|
||||
var Int: x = 0;
|
||||
{
|
||||
// empty block
|
||||
}
|
||||
return x;
|
||||
}
|
||||
+1
@@ -0,0 +1 @@
|
||||
result: 0
|
||||
@@ -0,0 +1,13 @@
|
||||
// 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
|
||||
|
||||
// Test that creating a continuation doesn't do anything.
|
||||
|
||||
fn main() -> Int {
|
||||
var Int: x = 0;
|
||||
__continuation k {
|
||||
x = x + 1;
|
||||
}
|
||||
return x;
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
result: 0
|
||||
@@ -0,0 +1,14 @@
|
||||
// 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
|
||||
|
||||
// Test creating and running a continuation.
|
||||
|
||||
fn main() -> Int {
|
||||
var Int: x = 0;
|
||||
__continuation k {
|
||||
x = x + 1;
|
||||
}
|
||||
__run k;
|
||||
return x;
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
result: 1
|
||||
@@ -0,0 +1,18 @@
|
||||
// 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
|
||||
|
||||
// Test pausing a continuation with `__await` and restarting it with
|
||||
// `__run`.
|
||||
|
||||
fn main() -> Int {
|
||||
var Int: x = 0;
|
||||
__continuation k {
|
||||
x = x + 1;
|
||||
__await;
|
||||
x = x + 2;
|
||||
}
|
||||
__run k;
|
||||
__run k;
|
||||
return x;
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
result: 3
|
||||
@@ -0,0 +1,19 @@
|
||||
// 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
|
||||
|
||||
// Assignment for continuations is shallow, so `k2` refers to the same
|
||||
// continuation as `k1`.
|
||||
|
||||
fn main() -> Int {
|
||||
var Int: x = 0;
|
||||
__continuation k1 {
|
||||
x = x + 1;
|
||||
__await;
|
||||
x = x + 2;
|
||||
}
|
||||
var __Continuation: k2 = k1;
|
||||
__run k1;
|
||||
__run k2;
|
||||
return x;
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
result: 3
|
||||
@@ -0,0 +1,19 @@
|
||||
// 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
|
||||
|
||||
// Test access to block-scoped variables upon resuming a continuation.
|
||||
|
||||
fn main() -> Int {
|
||||
var Int: y = 0;
|
||||
__continuation k {
|
||||
var Int: x = 0;
|
||||
x = x + 1;
|
||||
__await;
|
||||
x = x + 2;
|
||||
y = x;
|
||||
}
|
||||
__run k;
|
||||
__run k;
|
||||
return y;
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
result: 3
|
||||
@@ -0,0 +1,33 @@
|
||||
// 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
|
||||
|
||||
// Test recursive functions inside continuations.
|
||||
|
||||
var Int: current = 0;
|
||||
|
||||
fn CountUpTo(Int: x) -> Int {
|
||||
if (x == 0) {
|
||||
current = 0;
|
||||
__await;
|
||||
return 0;
|
||||
} else {
|
||||
current = 1 + CountUpTo(x - 1);
|
||||
__await;
|
||||
return current;
|
||||
}
|
||||
}
|
||||
|
||||
fn main() -> Int {
|
||||
__continuation k {
|
||||
CountUpTo(5);
|
||||
}
|
||||
var Int: sum = 0;
|
||||
var Int: count = 5;
|
||||
while (not (count == 0)) {
|
||||
__run k;
|
||||
sum = sum + current;
|
||||
count = count - 1;
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
result: 10
|
||||
@@ -0,0 +1,23 @@
|
||||
// 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
|
||||
|
||||
// Test the way in which copying of continuations interacts with data
|
||||
// on the stack such as the variable `x`. In this example the copy
|
||||
// happens after the variable `x` is created.
|
||||
|
||||
var Int: y = 0;
|
||||
|
||||
fn main() -> Int {
|
||||
__continuation k1 {
|
||||
var Int: x = 0;
|
||||
x = x + 1;
|
||||
__await;
|
||||
x = x + 2;
|
||||
y = x;
|
||||
}
|
||||
__run k1;
|
||||
var __Continuation: k2 = k1;
|
||||
__run k2;
|
||||
return y;
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
result: 3
|
||||
@@ -0,0 +1,25 @@
|
||||
// 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
|
||||
|
||||
// Test the way in which copying of continuations interacts with data
|
||||
// on the stack such as the variable `x`. In this example the copy
|
||||
// happens before the variable `x` is created, so each continuation
|
||||
// creates a different `x`.
|
||||
|
||||
var Int: y = 0;
|
||||
|
||||
fn main() -> Int {
|
||||
__continuation k1 {
|
||||
var Int: x = 0;
|
||||
x = x + 1;
|
||||
__await;
|
||||
y = x;
|
||||
}
|
||||
var __Continuation: k2 = k1;
|
||||
__run k1;
|
||||
__run k2;
|
||||
__run k1;
|
||||
__run k2;
|
||||
return y;
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
result: 1
|
||||
Reference in New Issue
Block a user