about summary refs log tree commit diff stats
path: root/026call.cc
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2016-04-23 22:11:48 -0700
committerKartik K. Agaram <vc@akkartik.com>2016-04-24 00:35:50 -0700
commitc9a5a7badacd8dba9b5d7cf683301c8ead4b1f12 (patch)
tree3062a12c4fda9757ef633e56f0abc8b8efbe594b /026call.cc
parent7bf9212fd47e505711cc0873d30456e3d793034d (diff)
downloadmu-c9a5a7badacd8dba9b5d7cf683301c8ead4b1f12.tar.gz
2862
Layers 0-29 are now a complete rudimentary platform except for pointers
and indirection.
Diffstat (limited to '026call.cc')
-rw-r--r--026call.cc168
1 files changed, 168 insertions, 0 deletions
diff --git a/026call.cc b/026call.cc
new file mode 100644
index 00000000..dd240936
--- /dev/null
+++ b/026call.cc
@@ -0,0 +1,168 @@
+//: So far the recipes we define can't run each other. Let's fix that.
+
+:(scenario calling_recipe)
+def main [
+  f
+]
+def f [
+  3:number <- add 2, 2
+]
++mem: storing 4 in location 3
+
+:(scenario return_on_fallthrough)
+def main [
+  f
+  1:number <- copy 0
+  2:number <- copy 0
+  3:number <- copy 0
+]
+def f [
+  4:number <- copy 0
+  5:number <- copy 0
+]
++run: f
+# running f
++run: {4: "number"} <- copy {0: "literal"}
++run: {5: "number"} <- copy {0: "literal"}
+# back out to main
++run: {1: "number"} <- copy {0: "literal"}
++run: {2: "number"} <- copy {0: "literal"}
++run: {3: "number"} <- copy {0: "literal"}
+
+:(before "struct routine {")
+// Everytime a recipe runs another, we interrupt it and start running the new
+// recipe. When that finishes, we continue this one where we left off.
+// This requires maintaining a 'stack' of interrupted recipes or 'calls'.
+struct call {
+  recipe_ordinal running_recipe;
+  int running_step_index;
+  // End call Fields
+  call(recipe_ordinal r) {
+    running_recipe = r;
+    running_step_index = 0;
+    // End call Constructor
+  }
+  ~call() {
+    // End call Destructor
+  }
+};
+typedef list<call> call_stack;
+
+:(replace{} "struct routine")
+struct routine {
+  call_stack calls;
+  // End routine Fields
+  routine(recipe_ordinal r);
+  bool completed() const;
+  const vector<instruction>& steps() const;
+};
+:(code)
+routine::routine(recipe_ordinal r) {
+  if (Trace_stream) {
+    ++Trace_stream->callstack_depth;
+    trace(9999, "trace") << "new routine; incrementing callstack depth to " << Trace_stream->callstack_depth << end();
+    assert(Trace_stream->callstack_depth < 9000);  // 9998-101 plus cushion
+  }
+  calls.push_front(call(r));
+  // End routine Constructor
+}
+
+:(code)
+inline call& current_call() {
+  return Current_routine->calls.front();
+}
+
+//:: now update routine's helpers
+
+:(replace{} "inline int& current_step_index()")
+inline int& current_step_index() {
+  assert(!Current_routine->calls.empty());
+  return current_call().running_step_index;
+}
+:(replace{} "inline const string& current_recipe_name()")
+inline const string& current_recipe_name() {
+  assert(!Current_routine->calls.empty());
+  return get(Recipe, current_call().running_recipe).name;
+}
+:(replace{} "inline const instruction& current_instruction()")
+inline const instruction& current_instruction() {
+  assert(!Current_routine->calls.empty());
+  return to_instruction(current_call());
+}
+:(code)
+inline const instruction& to_instruction(const call& call) {
+  return get(Recipe, call.running_recipe).steps.at(call.running_step_index);
+}
+
+:(after "Defined Recipe Checks")
+// not a primitive; check that it's present in the book of recipes
+if (!contains_key(Recipe, inst.operation)) {
+  raise << maybe(get(Recipe, r).name) << "undefined operation in '" << to_original_string(inst) << "'\n" << end();
+  break;
+}
+:(replace{} "default:" following "End Primitive Recipe Implementations")
+default: {
+  const instruction& call_instruction = current_instruction();
+  if (Recipe.find(current_instruction().operation) == Recipe.end()) {  // duplicate from Checks
+    // stop running this instruction immediately
+    ++current_step_index();
+    continue;
+  }
+  // not a primitive; look up the book of recipes
+  if (Trace_stream) {
+    ++Trace_stream->callstack_depth;
+    trace(9999, "trace") << "incrementing callstack depth to " << Trace_stream->callstack_depth << end();
+    assert(Trace_stream->callstack_depth < 9000);  // 9998-101 plus cushion
+  }
+  Current_routine->calls.push_front(call(current_instruction().operation));
+  finish_call_housekeeping(call_instruction, ingredients);
+  continue;  // not done with caller; don't increment step_index of caller
+}
+:(code)
+void finish_call_housekeeping(const instruction& call_instruction, const vector<vector<double> >& ingredients) {
+  // End Call Housekeeping
+}
+
+:(scenario calling_undefined_recipe_fails)
+% Hide_errors = true;
+def main [
+  foo
+]
++error: main: undefined operation in 'foo '
+
+:(scenario calling_undefined_recipe_handles_missing_result)
+% Hide_errors = true;
+def main [
+  x:number <- foo
+]
++error: main: undefined operation in 'x:number <- foo '
+
+//:: finally, we need to fix the termination conditions for the run loop
+
+:(replace{} "inline bool routine::completed() const")
+inline bool routine::completed() const {
+  return calls.empty();
+}
+
+:(replace{} "inline const vector<instruction>& routine::steps() const")
+inline const vector<instruction>& routine::steps() const {
+  assert(!calls.empty());
+  return get(Recipe, calls.front().running_recipe).steps;
+}
+
+:(before "Running One Instruction")
+// when we reach the end of one call, we may reach the end of the one below
+// it, and the one below that, and so on
+while (current_step_index() >= SIZE(Current_routine->steps())) {
+  // Falling Through End Of Recipe
+  if (Trace_stream) {
+    trace(9999, "trace") << "fall-through: exiting " << current_recipe_name() << "; decrementing callstack depth from " << Trace_stream->callstack_depth << end();
+    --Trace_stream->callstack_depth;
+    assert(Trace_stream->callstack_depth >= 0);
+  }
+  Current_routine->calls.pop_front();
+  if (Current_routine->calls.empty()) return;
+  // Complete Call Fallthrough
+  // todo: fail if no products returned
+  ++current_step_index();
+}
/a>
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634