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:
Jeremy G. Siek
2021-03-26 11:22:48 -04:00
committed by GitHub
co-authored by Dave Abrahams Geoff Romer
parent 6df0d51516
commit 3fa72d2984
35 changed files with 674 additions and 31 deletions
@@ -103,6 +103,15 @@ auto MakeAltCons(std::string alt_name, std::string choice_name)
return v;
}
// Return a first-class continuation represented a fragment
// of the stack.
auto MakeContinuation(std::vector<Frame*> stack) -> Value* {
auto* v = new Value();
v->tag = ValKind::ContinuationV;
v->u.continuation.stack = new std::vector<Frame*>(stack);
return v;
}
auto MakeVarPatVal(std::string name, const Value* type) -> const Value* {
auto* v = new Value();
v->tag = ValKind::VarPatV;
@@ -136,6 +145,13 @@ auto MakeTypeTypeVal() -> const Value* {
return v;
}
// Return a Continuation type.
auto MakeContinuationTypeVal() -> const Value* {
auto* v = new Value();
v->tag = ValKind::ContinuationTV;
return v;
}
auto MakeAutoTypeVal() -> const Value* {
auto* v = new Value();
v->tag = ValKind::AutoTV;
@@ -255,6 +271,9 @@ void PrintValue(const Value* val, std::ostream& out) {
case ValKind::AutoTV:
out << "auto";
break;
case ValKind::ContinuationTV:
out << "Continuation";
break;
case ValKind::PointerTV:
out << "Ptr(";
PrintValue(val->u.ptr_type.type, out);
@@ -291,6 +310,14 @@ void PrintValue(const Value* val, std::ostream& out) {
case ValKind::ChoiceTV:
out << "choice " << *val->u.choice_type.name;
break;
case ValKind::ContinuationV:
out << "continuation[[";
for (Frame* frame : *val->u.continuation.stack) {
PrintFrame(frame, out);
out << " :: ";
}
out << "]]";
break;
}
}
@@ -314,6 +341,7 @@ auto TypeEqual(const Value* t1, const Value* t2) -> bool {
return FieldsEqual(t1->u.tuple_type.fields, t2->u.tuple_type.fields);
case ValKind::IntTV:
case ValKind::BoolTV:
case ValKind::ContinuationTV:
return true;
default:
return false;