mu/034call.cc

168 lines
4.9 KiB
C++
Raw Normal View History

2015-04-17 18:22:59 +00:00
//: So far the recipes we define can't run each other. Let's fix that.
2015-04-24 17:19:03 +00:00
:(scenario calling_recipe)
2015-03-14 00:39:32 +00:00
recipe main [
f
]
recipe f [
3:number <- add 2, 2
2015-03-14 00:39:32 +00:00
]
2015-03-24 06:59:59 +00:00
+mem: storing 4 in location 3
2015-04-24 17:19:03 +00:00
:(scenario return_on_fallthrough)
recipe main [
f
1:number <- copy 0
2:number <- copy 0
3:number <- copy 0
]
recipe f [
4:number <- copy 0
5:number <- copy 0
]
+run: f
# running f
+run: 4:number <- copy 0
+run: 5:number <- copy 0
# back out to main
+run: 1:number <- copy 0
+run: 2:number <- copy 0
+run: 3:number <- copy 0
:(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;
2015-05-17 09:22:41 +00:00
long long int running_step_index;
2015-04-13 03:56:45 +00:00
// End call Fields
call(recipe_ordinal r) {
2015-05-24 00:58:10 +00:00
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;
2015-04-13 03:56:45 +00:00
// End routine Fields
routine(recipe_ordinal r);
2015-04-25 03:00:56 +00:00
bool completed() const;
2015-04-25 03:52:06 +00:00
const vector<instruction>& steps() const;
2015-03-27 17:51:27 +00:00
};
:(code)
routine::routine(recipe_ordinal r) {
if (Trace_stream) {
++Trace_stream->callstack_depth;
2015-10-29 18:56:10 +00:00
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));
2015-04-27 01:17:39 +00:00
// End routine Constructor
2015-04-25 05:24:39 +00:00
}
2015-10-28 13:27:01 +00:00
:(code)
inline call& current_call() {
return Current_routine->calls.front();
}
//:: now update routine's helpers
2015-05-17 09:22:41 +00:00
:(replace{} "inline long long int& current_step_index()")
inline long long int& current_step_index() {
assert(!Current_routine->calls.empty());
2015-10-28 13:27:01 +00:00
return current_call().running_step_index;
}
2015-04-25 03:25:33 +00:00
:(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);
}
2015-03-15 05:25:06 +00:00
:(after "Defined Recipe Checks")
// not a primitive; check that it's present in the book of recipes
if (Recipe.find(inst.operation) == Recipe.end()) {
raise_error << maybe(get(Recipe, r).name) << "undefined operation in '" << inst.to_string() << "'\n" << end();
break;
}
2015-04-13 03:47:49 +00:00
:(replace{} "default:" following "End Primitive Recipe Implementations")
2015-03-15 00:10:33 +00:00
default: {
const instruction& call_instruction = current_instruction();
2015-10-05 04:29:58 +00:00
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;
2015-10-29 18:56:10 +00:00
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));
// End Call Housekeeping
continue; // not done with caller; don't increment current_step_index()
}
2015-03-15 00:10:33 +00:00
:(scenario calling_undefined_recipe_fails)
% Hide_errors = true;
recipe main [
foo
]
+error: main: undefined operation in 'foo '
:(scenario calling_undefined_recipe_handles_missing_result)
% Hide_errors = true;
recipe main [
x:number <- foo
]
+error: main: undefined operation in 'x:number <- foo '
//:: finally, we need to fix the termination conditions for the run loop
2015-03-17 03:41:12 +00:00
2015-04-25 03:00:56 +00:00
:(replace{} "inline bool routine::completed() const")
inline bool routine::completed() const {
return calls.empty();
}
2015-03-14 08:09:20 +00:00
2015-04-25 03:52:06 +00:00
inline const vector<instruction>& routine::steps() const {
assert(!calls.empty());
return get(Recipe, calls.front().running_recipe).steps;
2015-04-25 03:52:06 +00:00
}
:(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
2015-05-17 09:22:41 +00:00
while (current_step_index() >= SIZE(Current_routine->steps())) {
// Falling Through End Of Recipe
if (Trace_stream) {
2015-10-29 18:56:10 +00:00
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
2015-04-25 03:23:34 +00:00
++current_step_index();
2015-03-14 08:09:20 +00:00
}
2015-04-06 19:21:18 +00:00
:(before "End Includes")
#include <stack>
using std::stack;