1 //: So far the recipes we define can't run each other. Let's fix that.
3 void test_calling_recipe() {
13 "mem: storing 4 in location 3\n"
17 void test_return_on_fallthrough() {
32 "run: {4: \"number\"} <- copy {0: \"literal\"}\n"
33 "run: {5: \"number\"} <- copy {0: \"literal\"}\n"
34 "run: {1: \"number\"} <- copy {0: \"literal\"}\n"
35 "run: {2: \"number\"} <- copy {0: \"literal\"}\n"
36 "run: {3: \"number\"} <- copy {0: \"literal\"}\n"
40 :(before
"struct routine {")
41 // Everytime a recipe runs another, we interrupt it and start running the new
42 // recipe. When that finishes, we continue this one where we left off.
43 // This requires maintaining a 'stack' of interrupted recipes or 'calls'.
45 recipe_ordinal running_recipe
;
46 int running_step_index
;
48 call(recipe_ordinal r
) { clear(r
, 0); }
49 call(recipe_ordinal r
, int index
) { clear(r
, index
); }
50 void clear(recipe_ordinal r
, int index
) {
52 running_step_index
= index
;
53 // End call Constructor
56 // End call Destructor
59 typedef list
<call
> call_stack
;
61 :(replace
{} "struct routine")
65 routine(recipe_ordinal r
);
66 bool completed() const;
67 const vector
<instruction
>& steps() const;
70 routine::routine(recipe_ordinal r
) {
72 trace(Callstack_depth
+1, "trace") << "new routine; incrementing callstack depth to " << Callstack_depth
<< end();
73 assert(Callstack_depth
< Max_depth
);
74 calls
.push_front(call(r
));
75 // End routine Constructor
78 //:: now update routine's helpers
80 //: macro versions for a slight speedup
82 :(delete{} "int& current_step_index()")
83 :(delete{} "recipe_ordinal currently_running_recipe()")
84 :(delete{} "const string& current_recipe_name()")
85 :(delete{} "const recipe& current_recipe()")
86 :(delete{} "const instruction& current_instruction()")
88 :(before
"End Includes")
89 #define current_call() Current_routine->calls.front()
90 #define current_step_index() current_call().running_step_index
91 #define currently_running_recipe() current_call().running_recipe
92 #define current_recipe() get(Recipe, currently_running_recipe())
93 #define current_recipe_name() current_recipe().name
94 #define to_instruction(call) get(Recipe, (call).running_recipe).steps.at((call).running_step_index)
95 #define current_instruction() to_instruction(current_call())
97 //: function versions for debugging
100 //? :(before "End Globals")
101 //? bool Foo2 = false;
103 //? call& current_call() {
104 //? if (Foo2) cerr << __FUNCTION__ << '\n';
105 //? return Current_routine->calls.front();
107 //? :(replace{} "int& current_step_index()")
108 //? int& current_step_index() {
109 //? assert(!Current_routine->calls.empty());
110 //? if (Foo2) cerr << __FUNCTION__ << '\n';
111 //? return current_call().running_step_index;
113 //? :(replace{} "recipe_ordinal currently_running_recipe()")
114 //? recipe_ordinal currently_running_recipe() {
115 //? assert(!Current_routine->calls.empty());
116 //? if (Foo2) cerr << __FUNCTION__ << '\n';
117 //? return current_call().running_recipe;
119 //? :(replace{} "const string& current_recipe_name()")
120 //? const string& current_recipe_name() {
121 //? assert(!Current_routine->calls.empty());
122 //? if (Foo2) cerr << __FUNCTION__ << '\n';
123 //? return get(Recipe, current_call().running_recipe).name;
125 //? :(replace{} "const recipe& current_recipe()")
126 //? const recipe& current_recipe() {
127 //? assert(!Current_routine->calls.empty());
128 //? if (Foo2) cerr << __FUNCTION__ << '\n';
129 //? return get(Recipe, current_call().running_recipe);
131 //? :(replace{} "const instruction& current_instruction()")
132 //? const instruction& current_instruction() {
133 //? assert(!Current_routine->calls.empty());
134 //? if (Foo2) cerr << __FUNCTION__ << '\n';
135 //? return to_instruction(current_call());
138 //? const instruction& to_instruction(const call& call) {
139 //? return get(Recipe, call.running_recipe).steps.at(call.running_step_index);
143 void dump_callstack() {
144 if (!Current_routine
) return;
145 if (Current_routine
->calls
.size() <= 1) return;
146 for (call_stack::const_iterator p
= ++Current_routine
->calls
.begin(); p
!= Current_routine
->calls
.end(); ++p
)
147 raise
<< " called from " << get(Recipe
, p
->running_recipe
).name
<< ": " << to_original_string(to_instruction(*p
)) << '\n' << end();
150 :(after
"Defined Recipe Checks")
151 // not a primitive; check that it's present in the book of recipes
152 if (!contains_key(Recipe
, inst
.operation
)) {
153 raise
<< maybe(get(Recipe
, r
).name
) << "undefined operation in '" << to_original_string(inst
) << "'\n" << end();
156 :(replace
{} "default:" following
"End Primitive Recipe Implementations")
158 if (contains_key(Recipe
, current_instruction().operation
)) { // error already raised in Checks above
159 // not a primitive; look up the book of recipes
161 trace(Callstack_depth
+1, "trace") << "incrementing callstack depth to " << Callstack_depth
<< end();
162 assert(Callstack_depth
< Max_depth
);
163 const call
& caller_frame
= current_call();
164 Current_routine
->calls
.push_front(call(to_instruction(caller_frame
).operation
));
165 finish_call_housekeeping(to_instruction(caller_frame
), ingredients
);
166 // not done with caller
167 write_products
= false;
168 fall_through_to_next_instruction
= false;
169 // End Non-primitive Call(caller_frame)
173 void finish_call_housekeeping(const instruction
& call_instruction
, const vector
<vector
<double> >& ingredients
) {
174 // End Call Housekeeping
177 void test_calling_undefined_recipe_fails() {
184 CHECK_TRACE_CONTENTS(
185 "error: main: undefined operation in 'foo'\n"
189 void test_calling_undefined_recipe_handles_missing_result() {
196 CHECK_TRACE_CONTENTS(
197 "error: main: undefined operation in 'x:num <- foo'\n"
201 //:: finally, we need to fix the termination conditions for the run loop
203 :(replace
{} "bool routine::completed() const")
204 bool routine::completed() const {
205 return calls
.empty();
208 :(replace
{} "const vector<instruction>& routine::steps() const")
209 const vector
<instruction
>& routine::steps() const {
210 assert(!calls
.empty());
211 return get(Recipe
, calls
.front().running_recipe
).steps
;
214 :(after
"Running One Instruction")
215 // when we reach the end of one call, we may reach the end of the one below
216 // it, and the one below that, and so on
217 while (current_step_index() >= SIZE(Current_routine
->steps())) {
218 // Falling Through End Of Recipe
219 trace(Callstack_depth
+1, "trace") << "fall-through: exiting " << current_recipe_name() << "; decrementing callstack depth from " << Callstack_depth
<< end();
221 assert(Callstack_depth
>= 0);
222 Current_routine
->calls
.pop_front();
223 if (Current_routine
->calls
.empty()) goto stop_running_current_routine
;
224 // Complete Call Fallthrough
225 // todo: fail if no products returned
226 ++current_step_index();
229 :(before
"End Primitive Recipe Declarations")
231 :(before
"End Primitive Recipe Numbers")
232 put(Recipe_ordinal
, "$dump-call-stack", _DUMP_CALL_STACK
);
233 :(before
"End Primitive Recipe Checks")
234 case _DUMP_CALL_STACK
: {
237 :(before
"End Primitive Recipe Implementations")
238 case _DUMP_CALL_STACK
: {
239 dump(Current_routine
->calls
);
243 void dump(const call_stack
& calls
) {
244 for (call_stack::const_reverse_iterator p
= calls
.rbegin(); p
!= calls
.rend(); ++p
)
245 cerr
<< get(Recipe
, p
->running_recipe
).name
<< ":" << p
->running_step_index
<< " -- " << to_string(to_instruction(*p
)) << '\n';