https://github.com/akkartik/mu/blob/master/076continuation.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54 :(before "End Mu Types Initialization")
55 type_ordinal continuation = Type_ordinal["continuation"] = Next_type_ordinal++;
56 Type[continuation].name = "continuation";
57
58
59 :(before "End is_mu_recipe Atom Cases(r)")
60 if (r.type->name == "continuation") return true;
61
62
63
64 :(after "Begin Reagent->Recipe(r, recipe_header)")
65 if (r.type->atom && r.type->name == "continuation") {
66 result_header.has_header = false;
67 return result_header;
68 }
69
70 :(scenario delimited_continuation)
71 recipe main [
72 1:continuation <- call-with-continuation-mark 233/mark, f, 77
73 2:num <- copy 5
74 {
75 2:num <- call 1:continuation, 2:num
76 3:bool <- greater-or-equal 2:num, 8
77 break-if 3:bool
78 loop
79 }
80 ]
81 recipe f [
82 11:num <- next-ingredient
83 12:num <- g 11:num
84 return 12:num
85 ]
86 recipe g [
87 21:num <- next-ingredient
88 22:num <- return-continuation-until-mark 233/mark
89 23:num <- add 22:num, 1
90 return 23:num
91 ]
92
93 +mem: storing 77 in location 21
94 +run: {2: "number"} <- copy {5: "literal"}
95 +mem: storing 5 in location 2
96
97 +run: {2: "number"} <- call {1: "continuation"}, {2: "number"}
98 +mem: storing 5 in location 22
99 +mem: storing 6 in location 2
100 +run: {2: "number"} <- call {1: "continuation"}, {2: "number"}
101 +mem: storing 6 in location 22
102 +mem: storing 7 in location 2
103 +run: {2: "number"} <- call {1: "continuation"}, {2: "number"}
104 +mem: storing 7 in location 22
105 +mem: storing 8 in location 2
106
107 -mem: storing 77 in location 22
108
109 -mem: storing 5 in location 21
110 -mem: storing 6 in location 21
111 -mem: storing 7 in location 21
112
113 -mem: storing 9 in location 2
114
115 :(before "End call Fields")
116 int continuation_mark_tag;
117 :(before "End call Constructor")
118 continuation_mark_tag = 0;
119
120 :(before "End Primitive Recipe Declarations")
121 CALL_WITH_CONTINUATION_MARK,
122 :(before "End Primitive Recipe Numbers")
123 Recipe_ordinal["call-with-continuation-mark"] = CALL_WITH_CONTINUATION_MARK;
124 :(before "End Primitive Recipe Checks")
125 case CALL_WITH_CONTINUATION_MARK: {
126 if (SIZE(inst.ingredients) < 2) {
127 raise << maybe(get(Recipe, r).name) << "'" << to_original_string(inst) << "' requires at least two ingredients: a mark number and a recipe to call\n" << end();
128 }
129 break;
130 }
131 :(before "End Primitive Recipe Implementations")
132 case CALL_WITH_CONTINUATION_MARK: {
133
134
135 if (Trace_stream) {
136 ++Trace_stream->callstack_depth;
137 trace("trace") << "delimited continuation; incrementing callstack depth to " << Trace_stream->callstack_depth << end();
138 assert(Trace_stream->callstack_depth < 9000);
139 }
140 instruction caller_instruction = current_instruction();
141 Current_routine->calls.front().continuation_mark_tag = current_instruction().ingredients.at(0).value;
142 Current_routine->calls.push_front(call(ingredients.at(1).at(0)));
143
144 caller_instruction.ingredients.erase(caller_instruction.ingredients.begin());
145 ingredients.erase(ingredients.begin());
146
147 caller_instruction.ingredients.erase(caller_instruction.ingredients.begin());
148 ingredients.erase(ingredients.begin());
149 finish_call_housekeeping(caller_instruction, ingredients);
150 continue;
151 }
152
153 :(scenario next_ingredient_inside_continuation)
154 recipe main [
155 call-with-continuation-mark 233/mark, f, true
156 ]
157 recipe f [
158 10:bool <- next-input
159 ]
160 +mem: storing 1 in location 10
161
162 :(scenario delimited_continuation_out_of_recipe_variable)
163 recipe main [
164 x:recipe <- copy f
165 call-with-continuation-mark 233/mark, x, true
166 ]
167 recipe f [
168 10:bool <- next-input
169 ]
170 +mem: storing 1 in location 10
171
172
173
174
175 :(before "End Types")
176 struct delimited_continuation {
177 call_stack frames;
178 int nrefs;
179 delimited_continuation(call_stack::iterator begin, call_stack::iterator end) :frames(call_stack(begin, end)), nrefs(0) {}
180 };
181 :(before "End Globals")
182 map<long long int, delimited_continuation> Delimited_continuation;
183 long long int Next_delimited_continuation_id = 1;
184 :(before "End Reset")
185 Delimited_continuation.clear();
186 Next_delimited_continuation_id = 1;
187
188 :(before "End Primitive Recipe Declarations")
189 RETURN_CONTINUATION_UNTIL_MARK,
190 :(before "End Primitive Recipe Numbers")
191 Recipe_ordinal["return-continuation-until-mark"] = RETURN_CONTINUATION_UNTIL_MARK;
192 :(before "End Primitive Recipe Checks")
193 case RETURN_CONTINUATION_UNTIL_MARK: {
194 if (inst.ingredients.empty()) {
195 raise << maybe(get(Recipe, r).name) << "'" << to_original_string(inst) << "' requires at least one ingredient: a mark tag (number)\n" << end();
196 }
197 break;
198 }
199 :(before "End Primitive Recipe Implementations")
200 case RETURN_CONTINUATION_UNTIL_MARK: {
201
202
203
204 Current_routine->calls.front().ingredient_atoms.clear();
205 Current_routine->calls.front().next_ingredient_to_process = 0;
206
207 call_stack::iterator find_base_of_continuation(call_stack&, int);
208 call_stack::iterator base = find_base_of_continuation(Current_routine->calls, current_instruction().ingredients.at(0).value);
209 if (base == Current_routine->calls.end()) {
210 raise << maybe(current_recipe_name()) << "couldn't find a 'call-with-continuation-mark' to return to with tag " << current_instruction().ingredients.at(0).original_string << '\n' << end();
211 raise << maybe(current_recipe_name()) << "call stack:\n" << end();
212 for (call_stack::iterator p = Current_routine->calls.begin(); p != Current_routine->calls.end(); ++p)
213 raise << maybe(current_recipe_name()) << " " << get(Recipe, p->running_recipe).name << '\n' << end();
214 break;
215 }
216 trace("run") << "creating continuation " << Next_delimited_continuation_id << end();
217 put(Delimited_continuation, Next_delimited_continuation_id, delimited_continuation(Current_routine->calls.begin(), base));
218 while (Current_routine->calls.begin() != base) {
219 if (Trace_stream) {
220 --Trace_stream->callstack_depth;
221 assert(Trace_stream->callstack_depth >= 0);
222 }
223 Current_routine->calls.pop_front();
224 }
225
226 products.resize(1);
227 products.at(0).push_back(Next_delimited_continuation_id);
228
229 copy(++ingredients.begin(), ingredients.end(), inserter(products, products.end()));
230 ++Next_delimited_continuation_id;
231 break;
232 }
233
234 :(code)
235 call_stack::iterator find_base_of_continuation(call_stack& c, int mark_tag) {
236 for (call_stack::iterator p = c.begin(); p != c.end(); ++p)
237 if (p->continuation_mark_tag == mark_tag) return p;
238 return c.end();
239 }
240
241
242 :(after "Begin Call")
243 if (is_mu_continuation(current_instruction().ingredients.at(0))) {
244
245 assert(scalar(ingredients.at(0)));
246 trace("run") << "calling continuation " << ingredients.at(0).at(0) << end();
247 if (!contains_key(Delimited_continuation, ingredients.at(0).at(0)))
248 raise << maybe(current_recipe_name()) << "no such delimited continuation " << current_instruction().ingredients.at(0).original_string << '\n' << end();
249 const call_stack& new_frames = get(Delimited_continuation, ingredients.at(0).at(0)).frames;
250 for (call_stack::const_reverse_iterator p = new_frames.rbegin(); p != new_frames.rend(); ++p)
251 Current_routine->calls.push_front(*p);
252 if (Trace_stream) {
253 Trace_stream->callstack_depth += SIZE(new_frames);
254 trace("trace") << "calling delimited continuation; growing callstack depth to " << Trace_stream->callstack_depth << end();
255 assert(Trace_stream->callstack_depth < 9000);
256 }
257
258 copy(++ingredients.begin(), ingredients.end(), inserter(products, products.begin()));
259 break;
260 }
261
262 :(scenario continuations_can_return_values)
263 def main [
264 local-scope
265 k:continuation, 1:num/raw <- call-with-continuation-mark 233/mark, f
266 ]
267 def f [
268 local-scope
269 g
270 ]
271 def g [
272 local-scope
273 return-continuation-until-mark 233/mark, 34
274 stash [continuation called]
275 ]
276 +mem: storing 34 in location 1
277
278 :(scenario continuations_continue_to_matching_mark)
279 def main [
280 local-scope
281 k:continuation, 1:num/raw <- call-with-continuation-mark 233/mark, f
282 add 1, 1
283 ]
284 def f [
285 local-scope
286 k2:continuation <- call-with-continuation-mark 234/mark, g
287 add 2, 2
288 ]
289 def g [
290 local-scope
291 return-continuation-until-mark 233/mark, 34
292 stash [continuation called]
293 ]
294 +run: add {1: "literal"}, {1: "literal"}
295 -run: add {2: "literal"}, {2: "literal"}
296
297
298
299 :(scenario call_shape_shifting_recipe_with_continuation_mark)
300 def main [
301 1:num <- call-with-continuation-mark 233/mark, f, 34
302 ]
303 def f x:_elem -> y:_elem [
304 local-scope
305 load-ingredients
306 y <- copy x
307 ]
308 +mem: storing 34 in location 1
309
310 :(before "End resolve_ambiguous_call(r, index, inst, caller_recipe) Special-cases")
311 if (inst.name == "call-with-continuation-mark") {
312 if (SIZE(inst.ingredients) > 1 && is_recipe_literal(inst.ingredients.at(1))) {
313 resolve_indirect_continuation_call(r, index, inst, caller_recipe);
314 return;
315 }
316 }
317 :(code)
318 void resolve_indirect_continuation_call(const recipe_ordinal r, int index, instruction& inst, const recipe& caller_recipe) {
319 instruction inst2;
320 inst2.name = inst.ingredients.at(1).name;
321 for (int i = 2; i < SIZE(inst.ingredients); ++i)
322 inst2.ingredients.push_back(inst.ingredients.at(i));
323 for (int i = 1; i < SIZE(inst.products); ++i)
324 inst2.products.push_back(inst.products.at(i));
325 resolve_ambiguous_call(r, index, inst2, caller_recipe);
326 inst.ingredients.at(1).name = inst2.name;
327 inst.ingredients.at(1).set_value(get(Recipe_ordinal, inst2.name));
328 }
329
330 :(scenario call_shape_shifting_recipe_with_continuation_mark_and_no_outputs)
331 def main [
332 1:continuation <- call-with-continuation-mark 233/mark, f, 34
333 ]
334 def f x:_elem [
335 local-scope
336 load-ingredients
337 return-continuation-until-mark 233/mark
338 ]
339 $error: 0
340
341 :(scenario continuation1)
342 def main [
343 local-scope
344 k:continuation <- call-with-continuation-mark 233/mark, create-yielder
345 10:num/raw <- call k
346 ]
347 def create-yielder -> n:num [
348 local-scope
349 load-inputs
350 return-continuation-until-mark 233/mark
351 return 1
352 ]
353 +mem: storing 1 in location 10
354 $error: 0
355
356 :(code)
357 bool is_mu_continuation(reagent x) {
358 canonize_type(x);
359 return x.type && x.type->atom && x.type->value == get(Type_ordinal, "continuation");
360 }
361
362
363 void dump(const int continuation_id) {
364 if (!contains_key(Delimited_continuation, continuation_id)) {
365 raise << "missing delimited continuation: " << continuation_id << '\n' << end();
366 return;
367 }
368 delimited_continuation& curr = get(Delimited_continuation, continuation_id);
369 dump(curr.frames);
370 }