From 693a1be718281e426e6c537a4389b3fc04dcb1a3 Mon Sep 17 00:00:00 2001 From: "Mike D. Lowis" Date: Mon, 19 Mar 2012 16:53:14 -0400 Subject: [PATCH] Added prototype of continuations in C++ --- lang_design/cont_gc/main.cpp | 113 +++++++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 lang_design/cont_gc/main.cpp diff --git a/lang_design/cont_gc/main.cpp b/lang_design/cont_gc/main.cpp new file mode 100644 index 0000000..a7754d4 --- /dev/null +++ b/lang_design/cont_gc/main.cpp @@ -0,0 +1,113 @@ +#include +#include +#include + +extern long* Stack_Bottom; +extern long* Return_Value; + +class Continuation +{ + public: + int n; + long* stack; + jmp_buf registers; + public: + Continuation() : n(0), stack(0) {} + Continuation(long* pbos, long*ptos) + { + n = pbos-ptos; + stack = new long[n]; + for (int i = 0; i Continuations; + +int save_context(Continuation* c) +{ + Continuations.push_back(c); + return setjmp(c->registers); +} + +void restore_context(long* ret_val) +{ + Continuation* c = Continuations.back(); + Continuations.pop_back(); + printf("Jumping back to main\n"); + (*c)(ret_val); // Call the continuation + printf("Should not print\n"); +} + +#define CALL_CC(a) if(! save_context( new Continuation() ) ) { a; } +#define RETURN(a) restore_context( (long*)a ) + +void max(int a,int b) +{ + printf("beginning of max\n"); + int* ret = new int; + *ret = (a > b) ? a : b; + printf("Before return\n"); + RETURN( ret ); + printf("This should never print\n"); +} + +int main(int argc, char** argv) +{ + long bottom; + Stack_Bottom = ⊥ + printf("%d\n", Stack_Bottom); + printf("Calling max\n"); + CALL_CC( max(1,2); ); + printf("Done calling max\n"); + printf("%d\n",*Return_Value); +} -- 2.49.0