about summary refs log blame commit diff stats
path: root/arc/.traces/scheduler-wakeup
blob: ef6db682d2687ffc42d881dcb9302909352b5625 (plain) (tree)
1
                      
schedule: waking up f2
l?h=hlt&id=9570363aec35e187e2395b1760a4b94e71580ac9'>9570363a ^
672e3e50 ^

65361948 ^
9570363a ^
65361948 ^
672e3e50 ^
65361948 ^

672e3e50 ^



76755b28 ^


67db19a0 ^
76755b28 ^
672e3e50 ^
90560d71 ^
d5d908dd ^
76755b28 ^


67db19a0 ^



76755b28 ^
65361948 ^

90560d71 ^

67db19a0 ^


83fcebf3 ^
65361948 ^
762b099e ^
e8b1d3ff ^
672e3e50 ^




9570363a ^
672e3e50 ^

65361948 ^
9570363a ^
76755b28 ^
672e3e50 ^
672e3e50 ^



67db19a0 ^





















































76755b28 ^












672e3e50 ^


83fcebf3 ^
65361948 ^
672e3e50 ^
76755b28 ^
672e3e50 ^
9570363a ^
65361948 ^
672e3e50 ^

76755b28 ^

672e3e50 ^
76755b28 ^
672e3e50 ^
d5d908dd ^
76755b28 ^
d5d908dd ^
9570363a ^
d5d908dd ^


76755b28 ^

d5d908dd ^
76755b28 ^
d5d908dd ^
672e3e50 ^
90560d71 ^

672e3e50 ^
90560d71 ^
76755b28 ^
672e3e50 ^


90560d71 ^

76755b28 ^
672e3e50 ^



65361948 ^







90560d71 ^
9570363a ^
65361948 ^


90560d71 ^
65361948 ^



90560d71 ^
9570363a ^
65361948 ^


76755b28 ^
65361948 ^




90560d71 ^

76755b28 ^
9542bb11 ^






76755b28 ^
9542bb11 ^


90560d71 ^
76755b28 ^
9542bb11 ^
dbe12410 ^





90560d71 ^

76755b28 ^
9542bb11 ^






76755b28 ^
9542bb11 ^


90560d71 ^
76755b28 ^
9542bb11 ^
65361948 ^
672e3e50 ^



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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
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



                                                                                          
                                    






                                                                                         

                                                                                                 
                        
                                  
                             

                              
                            
                               
                             














                                                                                 
                                                                                                                                                                                     

          
                                                                                            
                                                                                                                                                                                        
                                                                                                                       
 

                                                                 



                                                                                      


                                                                                                                                                                                                        
                                                                                                                                                      
                                
                                                                                         
                                            
                                                


                                                                                                                                                                                                                                                                                                                                                                                                                



                                                                                                                                                                                                            
                                  

                                                                                                                                     

                                                                                                                                                                                                                                                                      


                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
                                                                         
                                                                                                                                                                                                                                                                                                                                                                            
                                           
                                                                                                                                                         




                                                                                                                
                                                                                                            

          
                                                                                            
                                                                                                                  
                                                                               
 



                                                                 





















































                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             












                                                                                                                                                        


                                                                                                         
                                                                                                               
 
                                                                   
                                                  
             
                                                                                                                
                                                                                                                                                                                                   

              

                                                                                                  
 
                                                                                                                              
 
                                                                         
                                                    
             
                                                                                                                


                                                                                                                                                              

                                                                                                  
 
                                       
 
                                      

                                                                                                                                                                                                                                  
                                                   
                                                                                                                                                                                                               
                                                                                                                                                                                                   


                                                                                                                   

                                                                                                                                                                                                                                                                           
                                                                                                                                                                     



                                                                                                                 







                                                                                 
                                                                                                           
                                        


                                                                 
                                                     



                                                                                 
                                                                                                           
                                        


                                                                 
                                                                                                 




                                                                  

                                                                                                                                                                                                  
                                                                                                                                                                                                                            






                                                                                                                                                                                                                                                                                                                                                                                                                                                    
                                                                                                                                                                                                                      


                                                                                                                                                               
                                       
                                                                                                                                                                                                                    
                                  





                                                                      

                                                                                                                                                                                                      
                                                                                                                                                                                                                        






                                                                                                                                                                                                                                                                                                                                                                                                                                                    
                                                                                                                                                                                                                      


                                                                                                                                                               
                                       
                                                                                                                                                                                                                        
                                  
                                



                                     
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Mu - 036call_reply.cc</title>
<meta name="Generator" content="Vim/7.4">
<meta name="plugin-version" content="vim7.4_v1">
<meta name="syntax" content="cpp">
<meta name="settings" content="use_css,pre_wrap,no_foldcolumn,expand_tabs,prevent_copy=">
<meta name="colorscheme" content="minimal">
<style type="text/css">
<!--
pre { white-space: pre-wrap; font-family: monospace; color: #eeeeee; background-color: #080808; }
body { font-family: monospace; color: #eeeeee; background-color: #080808; }
* { font-size: 1.05em; }
.traceContains { color: #008000; }
.cSpecial { color: #008000; }
.Comment { color: #9090ff; }
.Delimiter { color: #a04060; }
.Special { color: #ff6060; }
.Identifier { color: #804000; }
.Constant { color: #00a0a0; }
-->
</style>

<script type='text/javascript'>
<!--

-->
</script>
</head>
<body>
<pre id='vimCodeElement'>
<span class="Comment">//: Calls can also generate products, using 'reply'.</span>

<span class="Delimiter">:(scenario reply)</span>
recipe main [
  <span class="Constant">1</span>:number<span class="Delimiter">,</span> <span class="Constant">2</span>:number<span class="Special"> &lt;- </span>f <span class="Constant">34</span>
]
recipe f [
  <span class="Constant">12</span>:number<span class="Special"> &lt;- </span>next-ingredient
  <span class="Constant">13</span>:number<span class="Special"> &lt;- </span>add <span class="Constant">1</span><span class="Delimiter">,</span> <span class="Constant">12</span>:number
  reply <span class="Constant">12</span>:number<span class="Delimiter">,</span> <span class="Constant">13</span>:number
]
<span class="traceContains">+mem: storing 34 in location 1</span>
<span class="traceContains">+mem: storing 35 in location 2</span>

<span class="Delimiter">:(before &quot;End Primitive Recipe Declarations&quot;)</span>
REPLY<span class="Delimiter">,</span>
<span class="Delimiter">:(before &quot;End Primitive Recipe Numbers&quot;)</span>
put<span class="Delimiter">(</span>Recipe_ordinal<span class="Delimiter">,</span> <span class="Constant">&quot;reply&quot;</span><span class="Delimiter">,</span> REPLY<span class="Delimiter">);</span>
<span class="Delimiter">:(before &quot;End Primitive Recipe Checks&quot;)</span>
case REPLY: <span class="Delimiter">{</span>
  <span class="Identifier">break</span><span class="Delimiter">;</span>  <span class="Comment">// checks will be performed by a transform below</span>
<span class="Delimiter">}</span>
<span class="Delimiter">:(before &quot;End Primitive Recipe Implementations&quot;)</span>
case REPLY: <span class="Delimiter">{</span>
  <span class="Comment">// Starting Reply</span>
  if <span class="Delimiter">(</span>Trace_stream<span class="Delimiter">)</span> <span class="Delimiter">{</span>
    trace<span class="Delimiter">(</span><span class="Constant">9999</span><span class="Delimiter">,</span> <span class="Constant">&quot;trace&quot;</span><span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot;reply: decrementing callstack depth from &quot;</span> &lt;&lt; Trace_stream<span class="Delimiter">-&gt;</span>callstack_depth &lt;&lt; end<span class="Delimiter">();</span>
    --Trace_stream<span class="Delimiter">-&gt;</span>callstack_depth<span class="Delimiter">;</span>
    if <span class="Delimiter">(</span>Trace_stream<span class="Delimiter">-&gt;</span>callstack_depth &lt; <span class="Constant">0</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
      Current_routine<span class="Delimiter">-&gt;</span>calls<span class="Delimiter">.</span>clear<span class="Delimiter">();</span>
      <span class="Identifier">goto</span> stop_running_current_routine<span class="Delimiter">;</span>
    <span class="Delimiter">}</span>
  <span class="Delimiter">}</span>
  Current_routine<span class="Delimiter">-&gt;</span>calls<span class="Delimiter">.</span>pop_front<span class="Delimiter">();</span>
  <span class="Comment">// just in case 'main' returns a value, drop it for now</span>
  if <span class="Delimiter">(</span>Current_routine<span class="Delimiter">-&gt;</span>calls<span class="Delimiter">.</span>empty<span class="Delimiter">())</span> <span class="Identifier">goto</span> stop_running_current_routine<span class="Delimiter">;</span>
  const instruction&amp; caller_instruction = current_instruction<span class="Delimiter">();</span>
  for <span class="Delimiter">(</span>long long int i = <span class="Constant">0</span><span class="Delimiter">;</span> i &lt; SIZE<span class="Delimiter">(</span>caller_instruction<span class="Delimiter">.</span>products<span class="Delimiter">);</span> ++i<span class="Delimiter">)</span>
    trace<span class="Delimiter">(</span><span class="Constant">9998</span><span class="Delimiter">,</span> <span class="Constant">&quot;run&quot;</span><span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot;result &quot;</span> &lt;&lt; i &lt;&lt; <span class="Constant">&quot; is &quot;</span> &lt;&lt; to_string<span class="Delimiter">(</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">))</span> &lt;&lt; end<span class="Delimiter">();</span>

  <span class="Comment">// make reply products available to caller</span>
  copy<span class="Delimiter">(</span>ingredients<span class="Delimiter">.</span>begin<span class="Delimiter">(),</span> ingredients<span class="Delimiter">.</span>end<span class="Delimiter">(),</span> inserter<span class="Delimiter">(</span>products<span class="Delimiter">,</span> products<span class="Delimiter">.</span>begin<span class="Delimiter">()));</span>
  <span class="Comment">// End Reply</span>
  <span class="Identifier">break</span><span class="Delimiter">;</span>  <span class="Comment">// continue to process rest of *caller* instruction</span>
<span class="Delimiter">}</span>

<span class="Comment">//: Products can include containers and exclusive containers, addresses and arrays.</span>
<span class="Delimiter">:(scenario reply_container)</span>
recipe main [
  <span class="Constant">3</span>:point<span class="Special"> &lt;- </span>f <span class="Constant">2</span>
]
recipe f [
  <span class="Constant">12</span>:number<span class="Special"> &lt;- </span>next-ingredient
  <span class="Constant">13</span>:number<span class="Special"> &lt;- </span>copy <span class="Constant">35</span>
  reply <span class="Constant">12</span>:point/<span class="Special">raw</span>
]
<span class="traceContains">+run: result 0 is [2, 35]</span>
<span class="traceContains">+mem: storing 2 in location 3</span>
<span class="traceContains">+mem: storing 35 in location 4</span>

<span class="Comment">//: Types in reply instructions are checked ahead of time.</span>

<span class="Delimiter">:(before &quot;End Checks&quot;)</span>
Transform<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>check_types_of_reply_instructions<span class="Delimiter">);</span>
<span class="Delimiter">:(code)</span>
void check_types_of_reply_instructions<span class="Delimiter">(</span>recipe_ordinal r<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  const recipe&amp; caller = get<span class="Delimiter">(</span>Recipe<span class="Delimiter">,</span> r<span class="Delimiter">);</span>
  for <span class="Delimiter">(</span>long long int i = <span class="Constant">0</span><span class="Delimiter">;</span> i &lt; SIZE<span class="Delimiter">(</span>caller<span class="Delimiter">.</span>steps<span class="Delimiter">);</span> ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span>
    const instruction&amp; caller_instruction = caller<span class="Delimiter">.</span>steps<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">);</span>
    if <span class="Delimiter">(</span>caller_instruction<span class="Delimiter">.</span>is_label<span class="Delimiter">)</span> <span class="Identifier">continue</span><span class="Delimiter">;</span>
    if <span class="Delimiter">(</span>caller_instruction<span class="Delimiter">.</span>products<span class="Delimiter">.</span>empty<span class="Delimiter">())</span> <span class="Identifier">continue</span><span class="Delimiter">;</span>
    if <span class="Delimiter">(</span>caller_instruction<span class="Delimiter">.</span>operation &lt; MAX_PRIMITIVE_RECIPES<span class="Delimiter">)</span> <span class="Identifier">continue</span><span class="Delimiter">;</span>
    const recipe&amp; callee = get<span class="Delimiter">(</span>Recipe<span class="Delimiter">,</span> caller_instruction<span class="Delimiter">.</span>operation<span class="Delimiter">);</span>
    for <span class="Delimiter">(</span>long long int i = <span class="Constant">0</span><span class="Delimiter">;</span> i &lt; SIZE<span class="Delimiter">(</span>callee<span class="Delimiter">.</span>steps<span class="Delimiter">);</span> ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span>
      const instruction&amp; reply_inst = callee<span class="Delimiter">.</span>steps<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">);</span>
      if <span class="Delimiter">(</span>reply_inst<span class="Delimiter">.</span>operation != REPLY<span class="Delimiter">)</span> <span class="Identifier">continue</span><span class="Delimiter">;</span>
      <span class="Comment">// check types with the caller</span>
      if <span class="Delimiter">(</span>SIZE<span class="Delimiter">(</span>caller_instruction<span class="Delimiter">.</span>products<span class="Delimiter">)</span> &gt; SIZE<span class="Delimiter">(</span>reply_inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">))</span> <span class="Delimiter">{</span>
        raise_error &lt;&lt; maybe<span class="Delimiter">(</span>caller<span class="Delimiter">.</span>name<span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot;too few values replied from &quot;</span> &lt;&lt; callee<span class="Delimiter">.</span>name &lt;&lt; <span class="cSpecial">'\n'</span> &lt;&lt; end<span class="Delimiter">();</span>
        <span class="Identifier">break</span><span class="Delimiter">;</span>
      <span class="Delimiter">}</span>
      for <span class="Delimiter">(</span>long long int i = <span class="Constant">0</span><span class="Delimiter">;</span> i &lt; SIZE<span class="Delimiter">(</span>caller_instruction<span class="Delimiter">.</span>products<span class="Delimiter">);</span> ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span>
        if <span class="Delimiter">(</span>!types_coercible<span class="Delimiter">(</span>caller_instruction<span class="Delimiter">.</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">),</span> reply_inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">)))</span> <span class="Delimiter">{</span>
          raise_error &lt;&lt; maybe<span class="Delimiter">(</span>callee<span class="Delimiter">.</span>name<span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot;reply ingredient &quot;</span> &lt;&lt; reply_inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">).</span>original_string &lt;&lt; <span class="Constant">&quot; can't be saved in &quot;</span> &lt;&lt; caller_instruction<span class="Delimiter">.</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">).</span>original_string &lt;&lt; <span class="cSpecial">'\n'</span> &lt;&lt; end<span class="Delimiter">();</span>
          reagent lhs = reply_inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">);</span>
          canonize_type<span class="Delimiter">(</span>lhs<span class="Delimiter">);</span>
          reagent rhs = caller_instruction<span class="Delimiter">.</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">);</span>
          canonize_type<span class="Delimiter">(</span>rhs<span class="Delimiter">);</span>
          raise_error &lt;&lt; debug_string<span class="Delimiter">(</span>lhs<span class="Delimiter">.</span>type<span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot; vs &quot;</span> &lt;&lt; debug_string<span class="Delimiter">(</span>rhs<span class="Delimiter">.</span>type<span class="Delimiter">)</span> &lt;&lt; <span class="cSpecial">'\n'</span> &lt;&lt; end<span class="Delimiter">();</span>
          <span class="Comment">// End reply Type Mismatch Error</span>
          <span class="Identifier">goto</span> finish_reply_check<span class="Delimiter">;</span>
        <span class="Delimiter">}</span>
      <span class="Delimiter">}</span>
      <span class="Comment">// check that any reply ingredients with /same-as-ingredient connect up</span>
      <span class="Comment">// the corresponding ingredient and product in the caller.</span>
      for <span class="Delimiter">(</span>long long int i = <span class="Constant">0</span><span class="Delimiter">;</span> i &lt; SIZE<span class="Delimiter">(</span>caller_instruction<span class="Delimiter">.</span>products<span class="Delimiter">);</span> ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span>
        if <span class="Delimiter">(</span>has_property<span class="Delimiter">(</span>reply_inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">),</span> <span class="Constant">&quot;same-as-ingredient&quot;</span><span class="Delimiter">))</span> <span class="Delimiter">{</span>
          string_tree* tmp = property<span class="Delimiter">(</span>reply_inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">),</span> <span class="Constant">&quot;same-as-ingredient&quot;</span><span class="Delimiter">);</span>
          if <span class="Delimiter">(</span>!tmp || tmp<span class="Delimiter">-&gt;</span>right<span class="Delimiter">)</span> <span class="Delimiter">{</span>
            raise_error &lt;&lt; maybe<span class="Delimiter">(</span>caller<span class="Delimiter">.</span>name<span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot;'same-as-ingredient' metadata should take exactly one value in &quot;</span> &lt;&lt; reply_inst<span class="Delimiter">.</span>to_string<span class="Delimiter">()</span> &lt;&lt; <span class="cSpecial">'\n'</span> &lt;&lt; end<span class="Delimiter">();</span>
            <span class="Identifier">goto</span> finish_reply_check<span class="Delimiter">;</span>
          <span class="Delimiter">}</span>
          long long int ingredient_index = to_integer<span class="Delimiter">(</span>tmp<span class="Delimiter">-&gt;</span>value<span class="Delimiter">);</span>
          if <span class="Delimiter">(</span>ingredient_index &gt;= SIZE<span class="Delimiter">(</span>caller_instruction<span class="Delimiter">.</span>ingredients<span class="Delimiter">))</span>
            raise_error &lt;&lt; maybe<span class="Delimiter">(</span>caller<span class="Delimiter">.</span>name<span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot;'same-as-ingredient' metadata overflows ingredients in: &quot;</span> &lt;&lt; caller_instruction<span class="Delimiter">.</span>to_string<span class="Delimiter">()</span> &lt;&lt; <span class="cSpecial">'\n'</span> &lt;&lt; end<span class="Delimiter">();</span>
          if <span class="Delimiter">(</span>!is_dummy<span class="Delimiter">(</span>caller_instruction<span class="Delimiter">.</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">))</span> &amp;&amp; caller_instruction<span class="Delimiter">.</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">).</span>name != caller_instruction<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span>ingredient_index<span class="Delimiter">).</span>name<span class="Delimiter">)</span>
            raise_error &lt;&lt; maybe<span class="Delimiter">(</span>caller<span class="Delimiter">.</span>name<span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot;'&quot;</span> &lt;&lt; caller_instruction<span class="Delimiter">.</span>to_string<span class="Delimiter">()</span> &lt;&lt; <span class="Constant">&quot;' should write to &quot;</span> &lt;&lt; caller_instruction<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span>ingredient_index<span class="Delimiter">).</span>original_string &lt;&lt; <span class="Constant">&quot; rather than &quot;</span> &lt;&lt; caller_instruction<span class="Delimiter">.</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">).</span>original_string &lt;&lt; <span class="cSpecial">'\n'</span> &lt;&lt; end<span class="Delimiter">();</span>
        <span class="Delimiter">}</span>
      <span class="Delimiter">}</span>
      finish_reply_check:<span class="Delimiter">;</span>
    <span class="Delimiter">}</span>
  <span class="Delimiter">}</span>
<span class="Delimiter">}</span>

<span class="Delimiter">:(scenario reply_type_mismatch)</span>
<span class="Special">% Hide_errors = true;</span>
recipe main [
  <span class="Constant">3</span>:number<span class="Special"> &lt;- </span>f <span class="Constant">2</span>
]
recipe f [
  <span class="Constant">12</span>:number<span class="Special"> &lt;- </span>next-ingredient
  <span class="Constant">13</span>:number<span class="Special"> &lt;- </span>copy <span class="Constant">35</span>
  <span class="Constant">14</span>:point<span class="Special"> &lt;- </span>copy <span class="Constant">12</span>:point/<span class="Special">raw</span>
  reply <span class="Constant">14</span>:point
]
<span class="traceContains">+error: f: reply ingredient 14:point can't be saved in 3:number</span>

<span class="Comment">//: In mu we'd like to assume that any instruction doesn't modify its</span>
<span class="Comment">//: ingredients unless they're also products. The /same-as-ingredient inside</span>
<span class="Comment">//: the recipe's 'reply' will help catch accidental misuse of such</span>
<span class="Comment">//: 'ingredient-products' (sometimes called in-out parameters in other languages).</span>

<span class="Delimiter">:(scenario reply_same_as_ingredient)</span>
<span class="Special">% Hide_errors = true;</span>
recipe main [
  <span class="Constant">1</span>:number<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>
  <span class="Constant">2</span>:number<span class="Special"> &lt;- </span>test1 <span class="Constant">1</span>:number  <span class="Comment"># call with different ingredient and product</span>
]
recipe test1 [
  <span class="Constant">10</span>:number<span class="Special"> &lt;- </span>next-ingredient
  reply <span class="Constant">10</span>:number/same-as-ingredient:<span class="Constant">0</span>
]
<span class="traceContains">+error: main: '2:number &lt;- test1 1:number' should write to 1:number rather than 2:number</span>

<span class="Delimiter">:(scenario reply_same_as_ingredient_dummy)</span>
<span class="Comment"># % Hide_errors = true;</span>
recipe main [
  <span class="Constant">1</span>:number<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>
  _<span class="Special"> &lt;- </span>test1 <span class="Constant">1</span>:number  <span class="Comment"># call with different ingredient and product</span>
]
recipe test1 [
  <span class="Constant">10</span>:number<span class="Special"> &lt;- </span>next-ingredient
  reply <span class="Constant">10</span>:number/same-as-ingredient:<span class="Constant">0</span>
]
$error: <span class="Constant">0</span>

<span class="Delimiter">:(code)</span>
string to_string<span class="Delimiter">(</span>const vector&lt;double&gt;&amp; in<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  if <span class="Delimiter">(</span>in<span class="Delimiter">.</span>empty<span class="Delimiter">())</span> <span class="Identifier">return</span> <span class="Constant">&quot;[]&quot;</span><span class="Delimiter">;</span>
  ostringstream out<span class="Delimiter">;</span>
  if <span class="Delimiter">(</span>SIZE<span class="Delimiter">(</span>in<span class="Delimiter">)</span> == <span class="Constant">1</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
    out &lt;&lt; no_scientific<span class="Delimiter">(</span>in<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">));</span>
    <span class="Identifier">return</span> out<span class="Delimiter">.</span>str<span class="Delimiter">();</span>
  <span class="Delimiter">}</span>
  out &lt;&lt; <span class="Constant">&quot;[&quot;</span><span class="Delimiter">;</span>
  for <span class="Delimiter">(</span>long long int i = <span class="Constant">0</span><span class="Delimiter">;</span> i &lt; SIZE<span class="Delimiter">(</span>in<span class="Delimiter">);</span> ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span>
    if <span class="Delimiter">(</span>i &gt; <span class="Constant">0</span><span class="Delimiter">)</span> out &lt;&lt; <span class="Constant">&quot;, &quot;</span><span class="Delimiter">;</span>
    out &lt;&lt; no_scientific<span class="Delimiter">(</span>in<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">));</span>
  <span class="Delimiter">}</span>
  out &lt;&lt; <span class="Constant">&quot;]&quot;</span><span class="Delimiter">;</span>
  <span class="Identifier">return</span> out<span class="Delimiter">.</span>str<span class="Delimiter">();</span>
<span class="Delimiter">}</span>

<span class="Comment">//: Conditional reply.</span>

<span class="Delimiter">:(scenario reply_if)</span>
recipe main [
  <span class="Constant">1</span>:number<span class="Special"> &lt;- </span>test1
]
recipe test1 [
  reply-if <span class="Constant">0</span><span class="Delimiter">,</span> <span class="Constant">34</span>
  reply <span class="Constant">35</span>
]
<span class="traceContains">+mem: storing 35 in location 1</span>

<span class="Delimiter">:(scenario reply_if_2)</span>
recipe main [
  <span class="Constant">1</span>:number<span class="Special"> &lt;- </span>test1
]
recipe test1 [
  reply-if <span class="Constant">1</span><span class="Delimiter">,</span> <span class="Constant">34</span>
  reply <span class="Constant">35</span>
]
<span class="traceContains">+mem: storing 34 in location 1</span>

<span class="Delimiter">:(before &quot;End Rewrite Instruction(curr, recipe result)&quot;)</span>
<span class="Comment">// rewrite `reply-if a, b, c, ...` to</span>
<span class="Comment">//   ```</span>
<span class="Comment">//   jump-unless a, 1:offset</span>
<span class="Comment">//   reply b, c, ...</span>
<span class="Comment">//   ```</span>
if <span class="Delimiter">(</span>curr<span class="Delimiter">.</span>name == <span class="Constant">&quot;reply-if&quot;</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
  if <span class="Delimiter">(</span>curr<span class="Delimiter">.</span>products<span class="Delimiter">.</span>empty<span class="Delimiter">())</span> <span class="Delimiter">{</span>
    curr<span class="Delimiter">.</span>operation = get<span class="Delimiter">(</span>Recipe_ordinal<span class="Delimiter">,</span> <span class="Constant">&quot;jump-unless&quot;</span><span class="Delimiter">);</span>
    curr<span class="Delimiter">.</span>name = <span class="Constant">&quot;jump-unless&quot;</span><span class="Delimiter">;</span>
    vector&lt;reagent&gt; results<span class="Delimiter">;</span>
    copy<span class="Delimiter">(</span>++curr<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>begin<span class="Delimiter">(),</span> curr<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>end<span class="Delimiter">(),</span> inserter<span class="Delimiter">(</span>results<span class="Delimiter">,</span> results<span class="Delimiter">.</span>end<span class="Delimiter">()));</span>
    curr<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>resize<span class="Delimiter">(</span><span class="Constant">1</span><span class="Delimiter">);</span>
    curr<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>reagent<span class="Delimiter">(</span><span class="Constant">&quot;1:offset&quot;</span><span class="Delimiter">));</span>
    result<span class="Delimiter">.</span>steps<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>curr<span class="Delimiter">);</span>
    curr<span class="Delimiter">.</span>clear<span class="Delimiter">();</span>
    curr<span class="Delimiter">.</span>operation = get<span class="Delimiter">(</span>Recipe_ordinal<span class="Delimiter">,</span> <span class="Constant">&quot;reply&quot;</span><span class="Delimiter">);</span>
    curr<span class="Delimiter">.</span>name = <span class="Constant">&quot;reply&quot;</span><span class="Delimiter">;</span>
    curr<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>swap<span class="Delimiter">(</span>results<span class="Delimiter">);</span>
  <span class="Delimiter">}</span>
  else <span class="Delimiter">{</span>
    raise_error &lt;&lt; <span class="Constant">&quot;'reply-if' never yields any products</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span> &lt;&lt; end<span class="Delimiter">();</span>
  <span class="Delimiter">}</span>
<span class="Delimiter">}</span>
<span class="Comment">// rewrite `reply-unless a, b, c, ...` to</span>
<span class="Comment">//   ```</span>
<span class="Comment">//   jump-if a, 1:offset</span>
<span class="Comment">//   reply b, c, ...</span>
<span class="Comment">//   ```</span>
if <span class="Delimiter">(</span>curr<span class="Delimiter">.</span>name == <span class="Constant">&quot;reply-unless&quot;</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
  if <span class="Delimiter">(</span>curr<span class="Delimiter">.</span>products<span class="Delimiter">.</span>empty<span class="Delimiter">())</span> <span class="Delimiter">{</span>
    curr<span class="Delimiter">.</span>operation = get<span class="Delimiter">(</span>Recipe_ordinal<span class="Delimiter">,</span> <span class="Constant">&quot;jump-if&quot;</span><span class="Delimiter">);</span>
    curr<span class="Delimiter">.</span>name = <span class="Constant">&quot;jump-if&quot;</span><span class="Delimiter">;</span>
    vector&lt;reagent&gt; results<span class="Delimiter">;</span>
    copy<span class="Delimiter">(</span>++curr<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>begin<span class="Delimiter">(),</span> curr<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>end<span class="Delimiter">(),</span> inserter<span class="Delimiter">(</span>results<span class="Delimiter">,</span> results<span class="Delimiter">.</span>end<span class="Delimiter">()));</span>
    curr<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>resize<span class="Delimiter">(</span><span class="Constant">1</span><span class="Delimiter">);</span>
    curr<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>reagent<span class="Delimiter">(</span><span class="Constant">&quot;1:offset&quot;</span><span class="Delimiter">));</span>
    result<span class="Delimiter">.</span>steps<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>curr<span class="Delimiter">);</span>
    curr<span class="Delimiter">.</span>clear<span class="Delimiter">();</span>
    curr<span class="Delimiter">.</span>operation = get<span class="Delimiter">(</span>Recipe_ordinal<span class="Delimiter">,</span> <span class="Constant">&quot;reply&quot;</span><span class="Delimiter">);</span>
    curr<span class="Delimiter">.</span>name = <span class="Constant">&quot;reply&quot;</span><span class="Delimiter">;</span>
    curr<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>swap<span class="Delimiter">(</span>results<span class="Delimiter">);</span>
  <span class="Delimiter">}</span>
  else <span class="Delimiter">{</span>
    raise_error &lt;&lt; <span class="Constant">&quot;'reply-unless' never yields any products</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span> &lt;&lt; end<span class="Delimiter">();</span>
  <span class="Delimiter">}</span>
<span class="Delimiter">}</span>
</pre>
</body>
</html>
<!-- vim: set foldmethod=manual : -->