about summary refs log tree commit diff stats
path: root/event.c
Commit message (Expand)AuthorAgeFilesLines
* applied Sanders max_and_focus.patchAnselm R. Garbe2006-09-041-4/+4
* simplified buttonpressAnselm R. Garbe2006-09-011-23/+12
* applied sanders patchAnselm R. Garbe2006-09-011-5/+8
* implemented Button2 press on tags for toggletag on the focused clientAnselm R. Garbe2006-08-311-2/+9
* simplified configurerequestAnselm R. Garbe2006-08-291-25/+22
* now dwm enforces max screen size also in tiled mode on non-floating clients w...Anselm R. Garbe2006-08-291-13/+14
* fixedAnselm R. Garbe2006-08-291-1/+3
* configurenotify remembers max geom now, and restores this if necessary, howev...Anselm R. Garbe2006-08-291-1/+11
* applied sanders focus_ patchesAnselm R. Garbe2006-08-281-1/+1
* applied sanders somepatches.patchAnselm R. Garbe2006-08-281-2/+4
* trying a different configurationAnselm R. Garbe2006-08-251-13/+11
* changed symbols for float/tiled mode, added mouse-driven mode toggle to butto...Anselm R. Garbe2006-08-251-10/+12
* new color stuff/new rendering stuffAnselm R. Garbe2006-08-251-1/+3
* applied Sanders focus_* patches, removed the unnecessary clean-prefix from th...Anselm R.Garbe2006-08-211-0/+2
* changed main event loopAnselm R.Garbe2006-08-151-0/+13
* removed NUMLOCKMASK, added dynamically calculated numlockmask insteadAnselm R.Garbe2006-08-141-3/+3
* applied sanders man page patch, removed button2 from bar clickAnselm R.Garbe2006-08-141-3/+3
* fixedAnselm R.Garbe2006-08-141-1/+1
* remove unnecessary XSyncAnselm R.Garbe2006-08-141-1/+0
* added mappingnotify event for kb refreshesAnselm R.Garbe2006-08-141-0/+12
* removed unneecessary crapAnselm R.Garbe2006-08-141-6/+1
* pplied Sanders appendtag patchAnselm R.Garbe2006-08-141-0/+6
* some other small fixesAnselm R.Garbe2006-08-141-2/+4
* removed ungrabkeys again (because of sander's mail)Anselm R.Garbe2006-08-141-16/+0
* implemented ungrabkeys() which is called in cleanup()Anselm R.Garbe2006-08-141-0/+16
* implemented restack behavior (floats are on top in tiled mode)Anselm R.Garbe2006-08-141-3/+6
* applied Sanders LD and resize patchesAnselm R.Garbe2006-08-141-2/+5
* removed viewnext/viewprevAnselm R.Garbe2006-08-141-23/+9
* made shortcuts like proposed by Sander, renamed viewextend to toggleview (mor...Anselm R.Garbe2006-08-131-1/+1
* implemented viewextend and added M-S-C-n shortcuts for extending the current ...Anselm R.Garbe2006-08-111-5/+2
* implement multi-tag selection through button3 click on the specific tagAnselm R.Garbe2006-08-111-1/+7
* realized that client focussing through the bar is pretty useless, better is s...Anselm R.Garbe2006-08-111-5/+5
* implemented focusprev on button1 press in the bar, and focusnext on button3 p...Anselm R.Garbe2006-08-111-2/+6
* applied Sanders zoom_update patchAnselm R.Garbe2006-08-101-2/+1
* applied sumik's multihead patchAnselm R.Garbe2006-08-101-5/+10
* applied sander's patchAnselm R.Garbe2006-08-101-11/+8
* disallow zoom on maximized clientsAnselm R.Garbe2006-08-101-9/+8
* added zoom on Mod1-Button1 on managed clients only (there is no moving possible)Anselm R.Garbe2006-08-091-0/+2
* removed NET_ACTIVE_WINDOW handlingAnselm R.Garbe2006-08-081-13/+0
* implemented NET_ACTIVE_WINDOW supportAnselm R.Garbe2006-08-081-0/+13
* applied grabbing-- and shell_minimalarg@10ksloc.org2006-08-071-8/+0
* small performance tweak ;)arg@10ksloc.org2006-08-041-2/+4
* fixed xterm font change (all other related apps should work fine with this fi...arg@10ksloc.org2006-08-041-14/+12
* removed TLast tag enum, now tags is simple defined as char *[] array, the res...arg@10ksloc.org2006-08-031-1/+1
* made fullscreen apps working fine in floating mode (there is no sane way to m...arg@10ksloc.org2006-08-021-19/+46
* new stuffarg@10ksloc.org2006-08-021-5/+2
* implemented focus on enterwindow on titlebarsarg@10ksloc.org2006-08-021-1/+1
* applied Sanders patches (numlock2)arg@10ksloc.org2006-08-021-5/+12
* reverting to old resize policyarg@10ksloc.org2006-08-021-10/+13
* new resize stuff (using XConfigureWindow instead of XSendEvent)arg@10ksloc.org2006-08-021-13/+10
previous revision' href='/akkartik/mu/blame/040brace.cc?h=main&id=e5998f74ac29bb4bf2aedfdd6fbea801ffdb08f6'>^
4c879fa1 ^
c062697c ^
e85cb85b ^
95f2fe96 ^
e85cb85b ^
e62aa7e3 ^
e85cb85b ^


4c879fa1 ^
95f2fe96 ^

e6692482 ^
436b2b2e ^

95f2fe96 ^
e6692482 ^
436b2b2e ^


e6692482 ^
436b2b2e ^

4c879fa1 ^
95f2fe96 ^
4c879fa1 ^
ab6ed192 ^
c442a5ad ^
4c879fa1 ^
32f6079f ^

decaddb4 ^
4c879fa1 ^

c442a5ad ^
4c879fa1 ^

decaddb4 ^
4c879fa1 ^
090cad5c ^
4c879fa1 ^

e62aa7e3 ^
95f2fe96 ^
4c879fa1 ^

4ce90b39 ^
4c879fa1 ^

e6692482 ^
c442a5ad ^
e6692482 ^
c442a5ad ^
decaddb4 ^


6f8f9fb5 ^

85173b62 ^
1fba5d5d ^
85173b62 ^
7ac221e4 ^
decaddb4 ^
7ac221e4 ^
decaddb4 ^
1b76245c ^
795f5244 ^
decaddb4 ^

4a943d4e ^


















3d45bf38 ^
4a943d4e ^














3d45bf38 ^
4a943d4e ^


















3d45bf38 ^
4a943d4e ^






















c062697c ^
4a943d4e ^






















c062697c ^
4a943d4e ^

















c062697c ^
4a943d4e ^
















c062697c ^
4a943d4e ^















c062697c ^
4a943d4e ^













32f6079f ^
4a943d4e ^


















32f6079f ^
4a943d4e ^


















15ab959d ^
4a943d4e ^


















15ab959d ^
4a943d4e ^













15ab959d ^

4a943d4e ^


















4c879fa1 ^
4a943d4e ^










e85cb85b ^
4a943d4e ^












436b2b2e ^
5109e78f ^
e765f9e7 ^
4a943d4e ^













e765f9e7 ^
4a943d4e ^













e765f9e7 ^

7d07cd1d ^
e765f9e7 ^


5109e78f ^
e765f9e7 ^

5109e78f ^
e765f9e7 ^
c8f2ff13 ^
e765f9e7 ^





7d07cd1d ^
e765f9e7 ^


5109e78f ^
e765f9e7 ^

5109e78f ^
e765f9e7 ^
c8f2ff13 ^
e765f9e7 ^







c8f2ff13 ^

3473c63a ^
5109e78f ^

e765f9e7 ^







95f2fe96 ^
e765f9e7 ^


5109e78f ^




c8f2ff13 ^
5109e78f ^
e765f9e7 ^





436b2b2e ^











795f5244 ^





436b2b2e ^






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
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

                          
                                                                    









                                                                         
                                                                       

                                                                            




                                                                              














                                                         
 
                                                
                                                     

       
                                               
                                        
                                                                              
                                                      
                                                                                                
                                                                      
                                                             
                            
                                                                                                       
                                                    

                            
                                                                          
                                                     

     
                                 
                                                                      
                                                       


                              
     
                            
                                
                                                                           

               

                        
     
                                





                                          
                                                              
               
     
                       
                                                                                             
                                     
                                                                                                                           


                 
                                   

                                                
                            

                               
                                                         
                                


                                   
                         

                            
                                            
                                                                                             
                                         
                                                                             
                                                                                                         
                 

       
          

                                                                            
                                                                                                

                 
     
                                  
                                            

                            
                                                                                                  
                                                   

                                                
                                                                                 

                                       
                            
                                                                                              
        
                                                                                                                                              


   

                                                                         
                                                                                       
                    
                                                                                             
                                    
                                     
                                         
   
                                                                     
                                                             

 


















                                                         
 














                                                         
 


















                                                         
 






















                                                         
 






















                                                         
 

















                                  
 
















                                     
 















                                       
 













                                     
 


















                                                         
 


















                                                         
 


















                                                         
 













                                                         

                                


















                                                  
 










                                                 
 












                                                                          
 
                                                         
 













                                       
 













                                       

                                                        
                                      


                     
                       

        
                                                          
                              
                                                    





                                                                          
                                          


                 
                       

        
                                                                  
                              
                                                







                                                                          

                                                                                           
                                                

                                                                                                         







                                                                             
                                  


                                              




                                                        
                                                     
                                   





                                                                                











                                                                            





                                                  






                                       
//: Structured programming
//:
//: Our jump recipes are quite inconvenient to use, so Mu provides a
//: lightweight tool called 'transform_braces' to work in a slightly more
//: convenient format with nested braces:
//:
//:   {
//:     some instructions
//:     {
//:       more instructions
//:     }
//:   }
//:
//: Braces are just labels, they require no special parsing. The pseudo
//: instructions 'loop' and 'break' jump to just after the enclosing '{' and
//: '}' respectively.
//:
//: Conditional and unconditional 'loop' and 'break' should give us 80% of the
//: benefits of the control-flow primitives we're used to in other languages,
//: like 'if', 'while', 'for', etc.

void test_brace_conversion() {
  transform(
      "def main [\n"
      "  {\n"
      "    break\n"
      "    1:num <- copy 0\n"
      "  }\n"
      "]\n"
  );
  CHECK_TRACE_CONTENTS(
      "transform: --- transform braces for recipe main\n"
      "transform: jump 1:offset\n"
      "transform: copy ...\n"
  );
}

:(before "End Instruction Modifying Transforms")
Transform.push_back(transform_braces);  // idempotent

:(code)
void transform_braces(const recipe_ordinal r) {
  const bool OPEN = false, CLOSE = true;
  // use signed integer for step index because we'll be doing arithmetic on it
  list<pair<bool/*OPEN/CLOSE*/, /*step*/int> > braces;
  trace(101, "transform") << "--- transform braces for recipe " << get(Recipe, r).name << end();
  for (int index = 0;  index < SIZE(get(Recipe, r).steps);  ++index) {
    const instruction& inst = get(Recipe, r).steps.at(index);
    if (inst.label == "{") {
      trace(103, "transform") << maybe(get(Recipe, r).name) << "push (open, " << index << ")" << end();
      braces.push_back(pair<bool,int>(OPEN, index));
    }
    if (inst.label == "}") {
      trace(103, "transform") << "push (close, " << index << ")" << end();
      braces.push_back(pair<bool,int>(CLOSE, index));
    }
  }
  stack</*step*/int> open_braces;
  for (int index = 0;  index < SIZE(get(Recipe, r).steps);  ++index) {
    instruction& inst = get(Recipe, r).steps.at(index);
    if (inst.label == "{") {
      open_braces.push(index);
      continue;
    }
    if (inst.label == "}") {
      if (open_braces.empty()) {
        raise << maybe(get(Recipe, r).name) << "unbalanced '}'\n" << end();
        return;
      }
      open_braces.pop();
      continue;
    }
    if (inst.is_label) continue;
    if (inst.name != "loop"
         && inst.name != "loop-if"
         && inst.name != "loop-unless"
         && inst.name != "break"
         && inst.name != "break-if"
         && inst.name != "break-unless") {
      trace(102, "transform") << inst.name << " ..." << end();
      continue;
    }
    // check for errors
    if (inst.name.find("-if") != string::npos || inst.name.find("-unless") != string::npos) {
      if (inst.ingredients.empty()) {
        raise << maybe(get(Recipe, r).name) << "'" << inst.name << "' expects 1 or 2 ingredients, but got none\n" << end();
        continue;
      }
    }
    // update instruction operation
    string old_name = inst.name;  // save a copy
    if (inst.name.find("-if") != string::npos) {
      inst.name = "jump-if";
      inst.operation = JUMP_IF;
    }
    else if (inst.name.find("-unless") != string::npos) {
      inst.name = "jump-unless";
      inst.operation = JUMP_UNLESS;
    }
    else {
      inst.name = "jump";
      inst.operation = JUMP;
    }
    // check for explicitly provided targets
    if (inst.name.find("-if") != string::npos || inst.name.find("-unless") != string::npos) {
      // conditional branches check arg 1
      if (SIZE(inst.ingredients) > 1 && is_literal(inst.ingredients.at(1))) {
        trace(102, "transform") << inst.name << ' ' << inst.ingredients.at(1).name << ":offset" << end();
        continue;
      }
    }
    else {
      // unconditional branches check arg 0
      if (!inst.ingredients.empty() && is_literal(inst.ingredients.at(0))) {
        trace(102, "transform") << "jump " << inst.ingredients.at(0).name << ":offset" << end();
        continue;
      }
    }
    // if implicit, compute target
    reagent target(new type_tree("offset"));
    target.set_value(0);
    if (open_braces.empty())
      raise << maybe(get(Recipe, r).name) << "'" << old_name << "' needs a '{' before\n" << end();
    else if (old_name.find("loop") != string::npos)
      target.set_value(open_braces.top()-index);
    else  // break instruction
      target.set_value(matching_brace(open_braces.top(), braces, r) - index - 1);
    inst.ingredients.push_back(target);
    // log computed target
    if (inst.name == "jump")
      trace(102, "transform") << "jump " << no_scientific(target.value) << ":offset" << end();
    else
      trace(102, "transform") << inst.name << ' ' << inst.ingredients.at(0).name << ", " << no_scientific(target.value) << ":offset" << end();
  }
}

// returns a signed integer not just so that we can return -1 but also to
// enable future signed arithmetic
int matching_brace(int index, const list<pair<bool, int> >& braces, recipe_ordinal r) {
  int stacksize = 0;
  for (list<pair<bool, int> >::const_iterator p = braces.begin();  p != braces.end();  ++p) {
    if (p->second < index) continue;
    stacksize += (p->first ? 1 : -1);
    if (stacksize == 0) return p->second;
  }
  raise << maybe(get(Recipe, r).name) << "unbalanced '{'\n" << end();
  return SIZE(get(Recipe, r).steps);  // exit current routine
}

void test_loop() {
  transform(
      "def main [\n"
      "  1:num <- copy 0\n"
      "  2:num <- copy 0\n"
      "  {\n"
      "    3:num <- copy 0\n"
      "    loop\n"
      "  }\n"
      "]\n"
  );
  CHECK_TRACE_CONTENTS(
      "transform: --- transform braces for recipe main\n"
      "transform: copy ...\n"
      "transform: copy ...\n"
      "transform: copy ...\n"
      "transform: jump -2:offset\n"
  );
}

void test_break_empty_block() {
  transform(
      "def main [\n"
      "  1:num <- copy 0\n"
      "  {\n"
      "    break\n"
      "  }\n"
      "]\n"
  );
  CHECK_TRACE_CONTENTS(
      "transform: --- transform braces for recipe main\n"
      "transform: copy ...\n"
      "transform: jump 0:offset\n"
  );
}

void test_break_cascading() {
  transform(
      "def main [\n"
      "  1:num <- copy 0\n"
      "  {\n"
      "    break\n"
      "  }\n"
      "  {\n"
      "    break\n"
      "  }\n"
      "]\n"
  );
  CHECK_TRACE_CONTENTS(
      "transform: --- transform braces for recipe main\n"
      "transform: copy ...\n"
      "transform: jump 0:offset\n"
      "transform: jump 0:offset\n"
  );
}

void test_break_cascading_2() {
  transform(
      "def main [\n"
      "  1:num <- copy 0\n"
      "  2:num <- copy 0\n"
      "  {\n"
      "    break\n"
      "    3:num <- copy 0\n"
      "  }\n"
      "  {\n"
      "    break\n"
      "  }\n"
      "]\n"
  );
  CHECK_TRACE_CONTENTS(
      "transform: --- transform braces for recipe main\n"
      "transform: copy ...\n"
      "transform: copy ...\n"
      "transform: jump 1:offset\n"
      "transform: copy ...\n"
      "transform: jump 0:offset\n"
  );
}

void test_break_if() {
  transform(
      "def main [\n"
      "  1:num <- copy 0\n"
      "  2:num <- copy 0\n"
      "  {\n"
      "    break-if 2:num\n"
      "    3:num <- copy 0\n"
      "  }\n"
      "  {\n"
      "    break\n"
      "  }\n"
      "]\n"
  );
  CHECK_TRACE_CONTENTS(
      "transform: --- transform braces for recipe main\n"
      "transform: copy ...\n"
      "transform: copy ...\n"
      "transform: jump-if 2, 1:offset\n"
      "transform: copy ...\n"
      "transform: jump 0:offset\n"
  );
}

void test_break_nested() {
  transform(
      "def main [\n"
      "  1:num <- copy 0\n"
      "  {\n"
      "    2:num <- copy 0\n"
      "    break\n"
      "    {\n"
      "      3:num <- copy 0\n"
      "    }\n"
      "    4:num <- copy 0\n"
      "  }\n"
      "]\n"
  );
  CHECK_TRACE_CONTENTS(
      "transform: jump 4:offset\n"
  );
}

void test_break_nested_degenerate() {
  transform(
      "def main [\n"
      "  1:num <- copy 0\n"
      "  {\n"
      "    2:num <- copy 0\n"
      "    break\n"
      "    {\n"
      "    }\n"
      "    4:num <- copy 0\n"
      "  }\n"
      "]\n"
  );
  CHECK_TRACE_CONTENTS(
      "transform: jump 3:offset\n"
  );
}

void test_break_nested_degenerate_2() {
  transform(
      "def main [\n"
      "  1:num <- copy 0\n"
      "  {\n"
      "    2:num <- copy 0\n"
      "    break\n"
      "    {\n"
      "    }\n"
      "  }\n"
      "]\n"
  );
  CHECK_TRACE_CONTENTS(
      "transform: jump 2:offset\n"
  );
}

void test_break_label() {
  Hide_errors = true;
  transform(
      "def main [\n"
      "  1:num <- copy 0\n"
      "  {\n"
      "    break +foo:offset\n"
      "  }\n"
      "]\n"
  );
  CHECK_TRACE_CONTENTS(
      "transform: jump +foo:offset\n"
  );
}

void test_break_unless() {
  transform(
      "def main [\n"
      "  1:num <- copy 0\n"
      "  2:num <- copy 0\n"
      "  {\n"
      "    break-unless 2:num\n"
      "    3:num <- copy 0\n"
      "  }\n"
      "]\n"
  );
  CHECK_TRACE_CONTENTS(
      "transform: --- transform braces for recipe main\n"
      "transform: copy ...\n"
      "transform: copy ...\n"
      "transform: jump-unless 2, 1:offset\n"
      "transform: copy ...\n"
  );
}

void test_loop_unless() {
  transform(
      "def main [\n"
      "  1:num <- copy 0\n"
      "  2:num <- copy 0\n"
      "  {\n"
      "    loop-unless 2:num\n"
      "    3:num <- copy 0\n"
      "  }\n"
      "]\n"
  );
  CHECK_TRACE_CONTENTS(
      "transform: --- transform braces for recipe main\n"
      "transform: copy ...\n"
      "transform: copy ...\n"
      "transform: jump-unless 2, -1:offset\n"
      "transform: copy ...\n"
  );
}

void test_loop_nested() {
  transform(
      "def main [\n"
      "  1:num <- copy 0\n"
      "  {\n"
      "    2:num <- copy 0\n"
      "    {\n"
      "      3:num <- copy 0\n"
      "    }\n"
      "    loop-if 4:bool\n"
      "    5:num <- copy 0\n"
      "  }\n"
      "]\n"
  );
  CHECK_TRACE_CONTENTS(
      "transform: --- transform braces for recipe main\n"
      "transform: jump-if 4, -5:offset\n"
  );
}

void test_loop_label() {
  transform(
      "def main [\n"
      "  1:num <- copy 0\n"
      "  +foo\n"
      "  2:num <- copy 0\n"
      "]\n"
  );
  CHECK_TRACE_CONTENTS(
      "transform: --- transform braces for recipe main\n"
      "transform: copy ...\n"
      "transform: copy ...\n"
  );
}

//: test how things actually run
void test_brace_conversion_and_run() {
  run(
      "def test-factorial [\n"
      "  1:num <- copy 5\n"
      "  2:num <- copy 1\n"
      "  {\n"
      "    3:bool <- equal 1:num, 1\n"
      "    break-if 3:bool\n"
      "    2:num <- multiply 2:num, 1:num\n"
      "    1:num <- subtract 1:num, 1\n"
      "    loop\n"
      "  }\n"
      "  4:num <- copy 2:num\n"  // trigger a read
      "]\n"
  );
  CHECK_TRACE_CONTENTS(
      "mem: location 2 is 120\n"
  );
}

void test_break_outside_braces_fails() {
  Hide_errors = true;
  run(
      "def main [\n"
      "  break\n"
      "]\n"
  );
  CHECK_TRACE_CONTENTS(
      "error: main: 'break' needs a '{' before\n"
  );
}

void test_break_conditional_without_ingredient_fails() {
  Hide_errors = true;
  run(
      "def main [\n"
      "  {\n"
      "    break-if\n"
      "  }\n"
      "]\n"
  );
  CHECK_TRACE_CONTENTS(
      "error: main: 'break-if' expects 1 or 2 ingredients, but got none\n"
  );
}

//: Using break we can now implement conditional returns.

void test_return_if() {
  run(
      "def main [\n"
      "  1:num <- test1\n"
      "]\n"
      "def test1 [\n"
      "  return-if 0, 34\n"
      "  return 35\n"
      "]\n"
  );
  CHECK_TRACE_CONTENTS(
      "mem: storing 35 in location 1\n"
  );
}

void test_return_if_2() {
  run(
      "def main [\n"
      "  1:num <- test1\n"
      "]\n"
      "def test1 [\n"
      "  return-if 1, 34\n"
      "  return 35\n"
      "]\n"
  );
  CHECK_TRACE_CONTENTS(
      "mem: storing 34 in location 1\n"
  );
}

:(before "End Rewrite Instruction(curr, recipe result)")
// rewrite 'return-if a, b, c, ...' to
//   ```
//   {
//     break-unless a
//     return b, c, ...
//   }
//   ```
if (curr.name == "return-if" || curr.name == "reply-if") {
  if (curr.products.empty()) {
    emit_return_block(result, "break-unless", curr);
    curr.clear();
  }
  else {
    raise << "'" << curr.name << "' never yields any products\n" << end();
  }
}
// rewrite 'return-unless a, b, c, ...' to
//   ```
//   {
//     break-if a
//     return b, c, ...
//   }
//   ```
if (curr.name == "return-unless" || curr.name == "reply-unless") {
  if (curr.products.empty()) {
    emit_return_block(result, "break-if", curr);
    curr.clear();
  }
  else {
    raise << "'" << curr.name << "' never yields any products\n" << end();
  }
}

:(code)
void emit_return_block(recipe& out, const string& break_command, const instruction& inst) {
  const vector<reagent>& ingredients = inst.ingredients;
  reagent/*copy*/ condition = ingredients.at(0);
  vector<reagent> return_ingredients;
  copy(++ingredients.begin(), ingredients.end(), inserter(return_ingredients, return_ingredients.end()));

  // {
  instruction open_label;  open_label.is_label=true;  open_label.label = "{";
  out.steps.push_back(open_label);

  // <break command> <condition>
  instruction break_inst;
  break_inst.operation = get(Recipe_ordinal, break_command);
  break_inst.name = break_command;
  break_inst.ingredients.push_back(condition);
  out.steps.push_back(break_inst);

  // return <return ingredients>
  instruction return_inst;
  return_inst.operation = get(Recipe_ordinal, "return");
  return_inst.name = "return";
  return_inst.ingredients.swap(return_ingredients);
  return_inst.original_string = inst.original_string;
  out.steps.push_back(return_inst);

  // }
  instruction close_label;  close_label.is_label=true;  close_label.label = "}";
  out.steps.push_back(close_label);
}

//: Make sure these pseudo recipes get consistent numbers in all tests, even
//: though they aren't implemented. Allows greater flexibility in ordering
//: transforms.

:(before "End Primitive Recipe Declarations")
BREAK,
BREAK_IF,
BREAK_UNLESS,
LOOP,
LOOP_IF,
LOOP_UNLESS,
:(before "End Primitive Recipe Numbers")
put(Recipe_ordinal, "break", BREAK);
put(Recipe_ordinal, "break-if", BREAK_IF);
put(Recipe_ordinal, "break-unless", BREAK_UNLESS);
put(Recipe_ordinal, "loop", LOOP);
put(Recipe_ordinal, "loop-if", LOOP_IF);
put(Recipe_ordinal, "loop-unless", LOOP_UNLESS);
:(before "End Primitive Recipe Checks")
case BREAK: break;
case BREAK_IF: break;
case BREAK_UNLESS: break;
case LOOP: break;
case LOOP_IF: break;
case LOOP_UNLESS: break;