about summary refs log tree commit diff stats
path: root/042name.cc
blob: 557469d07cdb99caf90434208f52c171191e0456 (plain) (blame)
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
//: A big convenience high-level languages provide is the ability to name memory
//: locations. In Mu, a transform called 'transform_names' provides this
//: convenience.

void test_transform_names() {
  run(
      "def main [\n"
      "  x:num <- copy 0\n"
      "]\n"
  );
  CHECK_TRACE_CONTENTS(
      "name: assign x 2\n"
      "mem: storing 0 in location 2\n"
  );
}

void test_transform_names_fails_on_use_before_define() {
  Hide_errors = true;
  transform(
      "def main [\n"
      "  x:num <- copy y:num\n"
      "]\n"
  );
  CHECK_TRACE_CONTENTS(
      "error: main: tried to read ingredient 'y' in 'x:num <- copy y:num' but it hasn't been written to yet\n"
  );
}

// todo: detect conditional defines

:(after "End Type Modifying Transforms")
Transform.push_back(transform_names);  // idempotent

:(before "End Globals")
map<recipe_ordinal, map<string, int> > Name;

//: the Name map is a global, so save it before tests and reset it for every
//: test, just to be safe.
:(before "End Globals")
map<recipe_ordinal, map<string, int> > Name_snapshot;
:(before "End save_snapshots")
Name_snapshot = Name;
:(before "End restore_snapshots")
Name = Name_snapshot;

:(code)
void transform_names(const recipe_ordinal r) {
  recipe& caller = get(Recipe, r);
  trace(101, "transform") << "--- transform names for recipe " << caller.name << end();
  bool names_used = false;
  bool numeric_locations_used = false;
  map<string, int>& names = Name[r];
  // record the indices 'used' so far in the map
  int& curr_idx = names[""];
  // reserve indices 0 and 1 for the chaining slot in a later layer.
  // transform_names may get called multiple times in later layers, so
  // curr_idx may already be set.
  if (curr_idx < 2) curr_idx = 2;
  for (int i = 0;  i < SIZE(caller.steps);  ++i) {
    instruction& inst = caller.steps.at(i);
    // End transform_names(inst) Special-cases
    // map names to addresses
    for (int in = 0;  in < SIZE(inst.ingredients);  ++in) {
      reagent& ingredient = inst.ingredients.at(in);
      if (is_disqualified(ingredient, inst, caller.name)) continue;
      if (is_numeric_location(ingredient)) numeric_locations_used = true;
      if (is_named_location(ingredient)) names_used = true;
      if (is_integer(ingredient.name)) continue;
      if (!already_transformed(ingredient, names)) {
        raise << maybe(caller.name) << "tried to read ingredient '" << ingredient.name << "' in '" << to_original_string(inst) << "' but it hasn't been written to yet\n" << end();
        // use-before-set Error
        return;
      }
      int v = lookup_name(ingredient, r);
      if (v >= 0) {
        ingredient.set_value(v);
        // Done Placing Ingredient(ingredient, inst, caller)
      }
      else {
        raise << maybe(caller.name) << "can't find a place to store '" << ingredient.name << "'\n" << end();
        return;
      }
    }
    for (int out = 0;  out < SIZE(inst.products);  ++out) {
      reagent& product = inst.products.at(out);
      if (is_disqualified(product, inst, caller.name)) continue;
      if (is_numeric_location(product)) numeric_locations_used = true;
      if (is_named_location(product)) names_used = true;
      if (is_integer(product.name)) continue;
      if (names.find(product.name) == names.end()) {
        trace(103, "name") << "assign " << product.name << " " << curr_idx << end();
        names[product.name] = curr_idx;
        curr_idx += size_of(product);
      }
      int v = lookup_name(product, r);
      if (v >= 0) {
        product.set_value(v);
        // Done Placing Product(product, inst, caller)
      }
      else {
        raise << maybe(caller.name) << "can't find a place to store '" << product.name << "'\n" << end();
        return;
      }
    }
  }
  if (names_used && numeric_locations_used)
    raise << maybe(caller.name) << "mixing variable names and numeric addresses\n" << end();
}

bool is_disqualified(/*mutable*/ reagent& x, const instruction& inst, const string& recipe_name) {
  if (!x.type) {
    raise << maybe(recipe_name) << "missing type for '" << x.original_string << "' in '" << to_original_string(inst) << "'\n" << end();
    // missing-type Error 1
    return true;
  }
  if (is_raw(x)) return true;
  if (is_literal(x)) return true;
  // End is_disqualified Special-cases
  if (x.initialized) return true;
  return false;
}

bool already_transformed(const reagent& r, const map<string, int>& names) {
  return contains_key(names, r.name);
}

int lookup_name(const reagent& r, const recipe_ordinal default_recipe) {
  return Name[default_recipe][r.name];
}

type_ordinal skip_addresses(type_tree* type) {
  while (type && is_compound_type_starting_with(type, "address"))
    type = type->right;
  if (!type) return -1;  // error handled elsewhere
  if (type->atom) return type->value;
  const type_tree* base_type = type;
  // Update base_type in skip_addresses
  if (base_type->atom)
    return base_type->value;
  assert(base_type->left->atom);
  return base_type->left->value;
}

bool is_compound_type_starting_with(const type_tree* type, const string& expected_name) {
  if (!type) return false;
  if (type->atom) return false;
  if (!type->left->atom) return false;
  return type->left->value == get(Type_ordinal, expected_name);
}

int find_element_offset(const type_ordinal t, const string& name, const string& recipe_name) {
  const type_info& container = get(Type, t);
  for (int i = 0;  i < SIZE(container.elements);  ++i)
    if (container.elements.at(i).name == name) return i;
  raise << maybe(recipe_name) << "unknown element '" << name << "' in container '" << get(Type, t).name << "'\n" << end();
  return -1;
}
int find_element_location(int base_address, const string& name, const type_tree* type, const string& recipe_name) {
  int offset = find_element_offset(get_base_type(type)->value, name, recipe_name);
  if (offset == -1) return offset;
  int result = base_address;
  for (int i = 0; i < offset; ++i)
    result += size_of(element_type(type, i));
  return result;
}

bool is_numeric_location(const reagent& x) {
  if (is_literal(x)) return false;
  if (is_raw(x)) return false;
  if (x.name == "0") return false;  // used for chaining lexical scopes
  return is_integer(x.name);
}

bool is_named_location(const reagent& x) {
  if (is_literal(x)) return false;
  if (is_raw(x)) return false;
  if (is_special_name(x.name)) return false;
  return !is_integer(x.name);
}

// all names here should either be disqualified or also in bind_special_scenario_names
bool is_special_name(const string& s) {
  if (s == "_") return true;
  if (s == "0") return true;
  // End is_special_name Special-cases
  return false;
}

bool is_raw(const reagent& r) {
  return has_property(r, "raw");
}

void test_transform_names_supports_containers() {
  transform(
      "def main [\n"
      "  x:point <- merge 34, 35\n"
      "  y:num <- copy 3\n"
      "]\n"
  );
  CHECK_TRACE_CONTENTS(
      "name: assign x 2\n"
      // skip location 3 because x occupies two locations
      "name: assign y 4\n"
  );
}

void test_transform_names_supports_static_arrays() {
  transform(
      "def main [\n"
      "  x:@:num:3 <- create-array\n"
      "  y:num <- copy 3\n"
      "]\n"
  );
  CHECK_TRACE_CONTENTS(
      "name: assign x 2\n"
      // skip locations 3, 4, 5 because x occupies four locations
      "name: assign y 6\n"
  );
}

void test_transform_names_passes_dummy() {
  transform(
      "def main [\n"
      // _ is just a dummy result that never gets consumed
      "  _, x:num <- copy 0, 1\n"
      "]\n"
  );
  CHECK_TRACE_CONTENTS(
      "name: assign x 2\n"
  );
  CHECK_TRACE_DOESNT_CONTAIN("name: assign _ 2");
}

//: an escape hatch to suppress name conversion that we'll use later
void test_transform_names_passes_raw() {
  Hide_errors = true;
  run(
      "def main [\n"
      "  x:num/raw <- copy 0\n"
      "]\n"
  );
  CHECK_TRACE_DOESNT_CONTAIN("name: assign x 2");
  CHECK_TRACE_CONTENTS(
      "error: can't write to location 0 in 'x:num/raw <- copy 0'\n"
  );
}

void test_transform_names_fails_when_mixing_names_and_numeric_locations() {
  Hide_errors = true;
  transform(
      "def main [\n"
      "  x:num <- copy 1:num\n"
      "]\n"
  );
  CHECK_TRACE_CONTENTS(
      "error: main: mixing variable names and numeric addresses\n"
  );
}

void test_transform_names_fails_when_mixing_names_and_numeric_locations_2() {
  Hide_errors = true;
  transform(
      "def main [\n"
      "  x:num <- copy 1\n"
      "  1:num <- copy x:num\n"
      "]\n"
  );
  CHECK_TRACE_CONTENTS(
      "error: main: mixing variable names and numeric addresses\n"
  );
}

void test_transform_names_does_not_fail_when_mixing_names_and_raw_locations() {
  transform(
      "def main [\n"
      "  x:num <- copy 1:num/raw\n"
      "]\n"
  );
  CHECK_TRACE_DOESNT_CONTAIN("error: main: mixing variable names and numeric addresses");
  CHECK_TRACE_COUNT("error", 0);
}

void test_transform_names_does_not_fail_when_mixing_names_and_literals() {
  transform(
      "def main [\n"
      "  x:num <- copy 1\n"
      "]\n"
  );
  CHECK_TRACE_DOESNT_CONTAIN("error: main: mixing variable names and numeric addresses");
  CHECK_TRACE_COUNT("error", 0);
}

//:: Support element names for containers in 'get' and 'get-location' and 'put'.
//: (get-location is implemented later)

:(before "End update GET offset_value in Check")
else {
  if (!offset.initialized) {
    raise << maybe(get(Recipe, r).name) << "uninitialized offset '" << offset.name << "' in '" << to_original_string(inst) << "'\n" << end();
    break;
  }
  offset_value = offset.value;
}

:(code)
void test_transform_names_transforms_container_elements() {
  transform(
      "def main [\n"
      "  p:&:point <- copy null\n"
      "  a:num <- get *p:&:point, y:offset\n"
      "  b:num <- get *p:&:point, x:offset\n"
      "]\n"
  );
  CHECK_TRACE_CONTENTS(
      "name: element y of type point is at offset 1\n"
      "name: element x of type point is at offset 0\n"
  );
}

:(before "End transform_names(inst) Special-cases")
// replace element names of containers with offsets
if (inst.name == "get" || inst.name == "get-location" || inst.name == "put") {
  //: avoid raising any errors here; later layers will support overloading new
  //: instructions with the same names (static dispatch), which could lead to
  //: spurious errors
  if (SIZE(inst.ingredients) < 2)
    break;  // error raised elsewhere
  if (!is_literal(inst.ingredients.at(1)))
    break;  // error raised elsewhere
  if (inst.ingredients.at(1).name.find_first_not_of("0123456789") != string::npos) {
    // since first non-address in base type must be a container, we don't have to canonize
    type_ordinal base_type = skip_addresses(inst.ingredients.at(0).type);
    if (contains_key(Type, base_type)) {  // otherwise we'll raise an error elsewhere
      inst.ingredients.at(1).set_value(find_element_offset(base_type, inst.ingredients.at(1).name, get(Recipe, r).name));
      trace(103, "name") << "element " << inst.ingredients.at(1).name << " of type " << get(Type, base_type).name << " is at offset " << no_scientific(inst.ingredients.at(1).value) << end();
    }
  }
}

:(code)
void test_missing_type_in_get() {
  Hide_errors = true;
  transform(
      "def main [\n"
      "  get a, x:offset\n"
      "]\n"
  );
  CHECK_TRACE_CONTENTS(
      "error: main: missing type for 'a' in 'get a, x:offset'\n"
  );
}

void test_transform_names_handles_containers() {
  transform(
      "def main [\n"
      "  a:point <- merge 0, 0\n"
      "  b:num <- copy 0\n"
      "]\n"
  );
  CHECK_TRACE_CONTENTS(
      "name: assign a 2\n"
      "name: assign b 4\n"
  );
}

//:: Support variant names for exclusive containers in 'maybe-convert'.

void test_transform_names_handles_exclusive_containers() {
  run(
      "def main [\n"
      "  12:num <- copy 1\n"
      "  13:num <- copy 35\n"
      "  14:num <- copy 36\n"
      "  20:point, 22:bool <- maybe-convert 12:number-or-point/unsafe, p:variant\n"
      "]\n"
  );
  CHECK_TRACE_CONTENTS(
      "name: variant p of type number-or-point has tag 1\n"
      "mem: storing 1 in location 22\n"
      "mem: storing 35 in location 20\n"
      "mem: storing 36 in location 21\n"
  );
}

:(before "End transform_names(inst) Special-cases")
// convert variant names of exclusive containers
if (inst.name == "maybe-convert") {
  if (SIZE(inst.ingredients) != 2) {
    raise << maybe(get(Recipe, r).name) << "exactly 2 ingredients expected in '" << to_original_string(inst) << "'\n" << end();
    break;
  }
  assert(is_literal(inst.ingredients.at(1)));
  if (inst.ingredients.at(1).name.find_first_not_of("0123456789") != string::npos) {
    // since first non-address in base type must be an exclusive container, we don't have to canonize
    type_ordinal base_type = skip_addresses(inst.ingredients.at(0).type);
    if (contains_key(Type, base_type)) {  // otherwise we'll raise an error elsewhere
      inst.ingredients.at(1).set_value(find_element_offset(base_type, inst.ingredients.at(1).name, get(Recipe, r).name));
      trace(103, "name") << "variant " << inst.ingredients.at(1).name << " of type " << get(Type, base_type).name << " has tag " << no_scientific(inst.ingredients.at(1).value) << end();
    }
  }
}

:(code)
void test_missing_type_in_maybe_convert() {
  Hide_errors = true;
  run(
      "def main [\n"
      "  maybe-convert a, x:variant\n"
      "]\n"
  );
  CHECK_TRACE_CONTENTS(
      "error: main: missing type for 'a' in 'maybe-convert a, x:variant'\n"
  );
}
>
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285



                                                                                          
                                       

                                                
                                  

                                                                                         

                       
                                                                                                 
                                                                                            
                                      
                             
                            



                                                                           

        





                               

       
                         


                                                                                         


                                                                                      

                                                                                                                                                                                                                        
                                                                                

                                                                       
                                                                                         
                                                                                



                                                                       

                                                                       
                                                                                                                                                                                                                                                                                                                                                        




                                                                                                                  
                                                                



                                                                                                                                                                                                                    

                                      

                                                                                                                                                                                     
                                                                                                                                                                                      

                                                                                                                                                                                                                                                                                                                                                                                 
                                                                                                                                                                                              
                                                                                                                                                                                                                                                           
                                  
                                                                                                                                                                      
                                                                                                                                                                                  
                                                                                                                                                                                                                                                                                                                                                                                 
                                                                                                                                                                                                                                                                                   






                                                                                                                     
                                                                                                                                                                       
                                            
                                                                   
                                                                                              
                                                                                                                                                                                                 
                                                                                                                                                                                                       

                                                                                                                                                                                                                                                             
                                                               
                                                                                                                                                                                    
                                    
                                                                                                                                                                                                      
                                                             
                                                                                                                                                                                                   
                                    
                                                                                                                                                                            


                                                                                   
                                                                                                                                                                            


                                                                                                                              
                                                                                                                                                                            


                                                                                                                              
                                                                                                                                                                                                  
                                               
                                                                                                                                                                                                                                                                                                                                                                                   
                                                                                                                                                                                                                                                                                   


                                                             
                                                                                                                                                                                                  
                                                                
                                                                                                                                                                                                                                                                                                                                                                                          
                                                       

                                                                                                                                                                                                                                                                           
                                                         
                                      
                                                                                                                                                        

                                                             
                                                                                                                                                                            

                                                                                                                                                                                                                    
                                                                                                                                                                                                                                                                                                                                                                                          
                                                       

                                                                                                                                                                                                                                                                           






                                                                    
                                                                                                                                                                 
                                                                             
                                                                                                                                                                                 

                                                                                                                                                                               
                                                                                    


                                                                                                                                                                                                                                                                                                                                                                             
                                                                                                                                                                                                      
                                                                                                                                                                                                                                                                                                                
                                                                                                                                                                                                                                                               
                                      
                                                                                       
                                                                                          
                                                                                                                                                                                                                                                       
                                                                                                                                                                                                      
                                                                                                                                                                                                   


                                                                                                                             

                                                           
                                                                                                                                                                                       
                                                                      
                                                                                                                                                                                 

                                                                                                                                                                               
                                                                                                             


                                                                                                                                                                                                                                                                                                                                                                             
                                                                                                                                                                                                      
                                                                                                                                                                                                                                               
                                      
                                                                                       
                                                                                          
                                                                                                                                                                                                                                                                                       








                                                                                                                          
                                                                                                                      
                                                                                                                                                                                      
                                                                                    
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
                                                                    
                                                                                                                                                                                                                                  
                                             
                                                                                                                                                                                                                                                                                          

                                                                                                                                                                                     
                                                                                                                                                                    
                                  
                                                                 

                                
                                                                                                         
                                                                                                              

                                                                                                                                                                                                                                                                                        
                                                                                                                                                                                                                                                                                       

                                                                                                                                                                                                                                                                                                                                                                                                                                     
                                                                                                                                                                                                                              
                                                                                                                                     
                                                                       
                                                                                                                                                                                                                                          
                                                                                                                                                                   
                                                                                                                                                                                                                                          
                                    



                                                                                                                                                                   
                                                                                                                                                                                                             






                                                                                                                                                                      
                                                                                                                                                                                       
                                                                                                                                                                            
                                                                                                                                                                                                                                     
                                                                                                                                                                                                                                                                                                      
                                  
                                                                                                                                                                                                                                                                                                                                 

                                
                                                                                                                                                                                                                                                                       
                                                                                                       



                                                                                                                                                                                                                                                                                                                                                            
                                                                                

                                                                                                                                                                                                                                                                                                                                                                                                                                                       
                                                                                                                                                                                                                                                  
                                  
                                                                                                                                                                                              


                                                                                                                                                                                                                                                                                     
 
                                                                                                                                                                                                                        
                                                                                       
                                                                                                                                      


                                                                                                                                                                        

                                                                                                                                                                                 

                                                                                                         
                                                                                                                                                                                                                        




                                                                                                                         
                                                                         


                                                      
                                                                                                                                                                                                                                                                                                                                                                            



                                                                                                                                                                                                                                                                                                   
                                                                                                                                                                                                                                                                                           
                                


       
                                     
<!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 - 090trace_browser.cc</title>
<meta name="Generator" content="Vim/7.4">
<meta name="plugin-version" content="vim7.4_v2">
<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-size: 12pt; font-family: monospace; color: #eeeeee; background-color: #080808; }
* { font-size: 12pt; font-size: 1em; }
.Constant { color: #00a0a0; }
.Comment { color: #9090ff; }
.Delimiter { color: #800080; }
.cSpecial { color: #008000; }
.Identifier { color: #fcb165; }
.Normal { color: #eeeeee; background-color: #080808; padding-bottom: 1px; }
-->
</style>

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

-->
</script>
</head>
<body>
<pre id='vimCodeElement'>
<span class="Comment">//: A debugging helper that lets you zoom in/out on a trace.</span>

<span class="Comment">//: browse the trace we just created</span>
<span class="Delimiter">:(before &quot;End Primitive Recipe Declarations&quot;)</span>
_BROWSE_TRACE<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;$browse-trace&quot;</span><span class="Delimiter">,</span> _BROWSE_TRACE<span class="Delimiter">);</span>
<span class="Delimiter">:(before &quot;End Primitive Recipe Checks&quot;)</span>
<span class="Normal">case</span> _BROWSE_TRACE: <span class="Delimiter">{</span>
  <span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Delimiter">:(before &quot;End Primitive Recipe Implementations&quot;)</span>
<span class="Normal">case</span> _BROWSE_TRACE: <span class="Delimiter">{</span>
  start_trace_browser<span class="Delimiter">();</span>
  <span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>

<span class="Comment">//: browse a trace loaded from a file</span>
<span class="Delimiter">:(after &quot;Commandline Parsing&quot;)</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>argc == <span class="Constant">3</span> &amp;&amp; is_equal<span class="Delimiter">(</span>argv[<span class="Constant">1</span>]<span class="Delimiter">,</span> <span class="Constant">&quot;browse-trace&quot;</span><span class="Delimiter">))</span> <span class="Delimiter">{</span>
  load_trace<span class="Delimiter">(</span>argv[<span class="Constant">2</span>]<span class="Delimiter">);</span>
  start_trace_browser<span class="Delimiter">();</span>
  <span class="Identifier">return</span> <span class="Constant">0</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>

<span class="Delimiter">:(before &quot;End Globals&quot;)</span>
set&lt;<span class="Normal">int</span>&gt; Visible<span class="Delimiter">;</span>
<span class="Normal">int</span> Top_of_screen = <span class="Constant">0</span><span class="Delimiter">;</span>
<span class="Normal">int</span> Last_printed_row = <span class="Constant">0</span><span class="Delimiter">;</span>
map&lt;<span class="Normal">int</span><span class="Delimiter">,</span> <span class="Normal">int</span>&gt; Trace_index<span class="Delimiter">;</span>  <span class="Comment">// screen row -&gt; trace index</span>

<span class="Delimiter">:(code)</span>
<span class="Normal">void</span> start_trace_browser<span class="Delimiter">()</span> <span class="Delimiter">{</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>!Trace_stream<span class="Delimiter">)</span> <span class="Identifier">return</span><span class="Delimiter">;</span>
  cerr &lt;&lt; <span class="Constant">&quot;computing min depth to display</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span><span class="Delimiter">;</span>
  <span class="Normal">int</span> min_depth = <span class="Constant">9999</span><span class="Delimiter">;</span>
  <span class="Normal">for</span> <span class="Delimiter">(</span><span class="Normal">int</span> i = <span class="Constant">0</span><span class="Delimiter">;</span> i &lt; SIZE<span class="Delimiter">(</span>Trace_stream<span class="Delimiter">-&gt;</span>past_lines<span class="Delimiter">);</span> ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span>
    trace_line&amp; curr_line = Trace_stream<span class="Delimiter">-&gt;</span>past_lines<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">);</span>
    <span class="Normal">if</span> <span class="Delimiter">(</span>curr_line<span class="Delimiter">.</span>depth &lt; min_depth<span class="Delimiter">)</span> min_depth = curr_line<span class="Delimiter">.</span>depth<span class="Delimiter">;</span>
  <span class="Delimiter">}</span>
  cerr &lt;&lt; <span class="Constant">&quot;min depth is &quot;</span> &lt;&lt; min_depth &lt;&lt; <span class="cSpecial">'\n'</span><span class="Delimiter">;</span>
  cerr &lt;&lt; <span class="Constant">&quot;computing lines to display</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span><span class="Delimiter">;</span>
  <span class="Normal">for</span> <span class="Delimiter">(</span><span class="Normal">int</span> i = <span class="Constant">0</span><span class="Delimiter">;</span> i &lt; SIZE<span class="Delimiter">(</span>Trace_stream<span class="Delimiter">-&gt;</span>past_lines<span class="Delimiter">);</span> ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span>
    <span class="Normal">if</span> <span class="Delimiter">(</span>Trace_stream<span class="Delimiter">-&gt;</span>past_lines<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">).</span>depth == min_depth<span class="Delimiter">)</span>
      Visible<span class="Delimiter">.</span>insert<span class="Delimiter">(</span>i<span class="Delimiter">);</span>
  <span class="Delimiter">}</span>
  tb_init<span class="Delimiter">();</span>
  Display_row = Display_column = <span class="Constant">0</span><span class="Delimiter">;</span>
  tb_event event<span class="Delimiter">;</span>
  Top_of_screen = <span class="Constant">0</span><span class="Delimiter">;</span>
  refresh_screen_rows<span class="Delimiter">();</span>
  <span class="Normal">while</span> <span class="Delimiter">(</span><span class="Constant">true</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
    render<span class="Delimiter">();</span>
    <span class="Normal">do</span> <span class="Delimiter">{</span>
      tb_poll_event<span class="Delimiter">(</span>&amp;event<span class="Delimiter">);</span>
    <span class="Delimiter">}</span> <span class="Normal">while</span> <span class="Delimiter">(</span>event<span class="Delimiter">.</span>type != TB_EVENT_KEY<span class="Delimiter">);</span>
    <span class="Normal">int</span> key = event<span class="Delimiter">.</span>key ? event<span class="Delimiter">.</span>key : event<span class="Delimiter">.</span>ch<span class="Delimiter">;</span>
    <span class="Normal">if</span> <span class="Delimiter">(</span>key == <span class="Constant">'q'</span> || key == <span class="Constant">'Q'</span><span class="Delimiter">)</span> <span class="Identifier">break</span><span class="Delimiter">;</span>
    <span class="Normal">if</span> <span class="Delimiter">(</span>key == <span class="Constant">'j'</span> || key == TB_KEY_ARROW_DOWN<span class="Delimiter">)</span> <span class="Delimiter">{</span>
      <span class="Comment">// move cursor one line down</span>
      <span class="Normal">if</span> <span class="Delimiter">(</span>Display_row &lt; Last_printed_row<span class="Delimiter">)</span> ++Display_row<span class="Delimiter">;</span>
    <span class="Delimiter">}</span>
    <span class="Normal">if</span> <span class="Delimiter">(</span>key == <span class="Constant">'k'</span> || key == TB_KEY_ARROW_UP<span class="Delimiter">)</span> <span class="Delimiter">{</span>
      <span class="Comment">// move cursor one line up</span>
      <span class="Normal">if</span> <span class="Delimiter">(</span>Display_row &gt; <span class="Constant">0</span><span class="Delimiter">)</span> --Display_row<span class="Delimiter">;</span>
    <span class="Delimiter">}</span>
    <span class="Normal">if</span> <span class="Delimiter">(</span>key == <span class="Constant">'H'</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
      <span class="Comment">// move cursor to top of screen</span>
      Display_row = <span class="Constant">0</span><span class="Delimiter">;</span>
    <span class="Delimiter">}</span>
    <span class="Normal">if</span> <span class="Delimiter">(</span>key == <span class="Constant">'M'</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
      <span class="Comment">// move cursor to center of screen</span>
      Display_row = tb_height<span class="Delimiter">()</span>/<span class="Constant">2</span><span class="Delimiter">;</span>
    <span class="Delimiter">}</span>
    <span class="Normal">if</span> <span class="Delimiter">(</span>key == <span class="Constant">'L'</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
      <span class="Comment">// move cursor to bottom of screen</span>
      Display_row = tb_height<span class="Delimiter">()</span>-<span class="Constant">1</span><span class="Delimiter">;</span>
    <span class="Delimiter">}</span>
    <span class="Normal">if</span> <span class="Delimiter">(</span>key == <span class="Constant">'J'</span> || key == TB_KEY_PGDN<span class="Delimiter">)</span> <span class="Delimiter">{</span>
      <span class="Comment">// page-down</span>
      <span class="Normal">if</span> <span class="Delimiter">(</span>Trace_index<span class="Delimiter">.</span>find<span class="Delimiter">(</span>tb_height<span class="Delimiter">()</span>-<span class="Constant">1</span><span class="Delimiter">)</span> != Trace_index<span class="Delimiter">.</span>end<span class="Delimiter">())</span> <span class="Delimiter">{</span>
        Top_of_screen = get<span class="Delimiter">(</span>Trace_index<span class="Delimiter">,</span> tb_height<span class="Delimiter">()</span>-<span class="Constant">1</span><span class="Delimiter">)</span> + <span class="Constant">1</span><span class="Delimiter">;</span>
        refresh_screen_rows<span class="Delimiter">();</span>
      <span class="Delimiter">}</span>
    <span class="Delimiter">}</span>
    <span class="Normal">if</span> <span class="Delimiter">(</span>key == <span class="Constant">'K'</span> || key == TB_KEY_PGUP<span class="Delimiter">)</span> <span class="Delimiter">{</span>
      <span class="Comment">// page-up is more convoluted</span>
      <span class="Normal">for</span> <span class="Delimiter">(</span><span class="Normal">int</span> screen_row = tb_height<span class="Delimiter">();</span> screen_row &gt; <span class="Constant">0</span> &amp;&amp; Top_of_screen &gt; <span class="Constant">0</span><span class="Delimiter">;</span> --screen_row<span class="Delimiter">)</span> <span class="Delimiter">{</span>
        --Top_of_screen<span class="Delimiter">;</span>
        <span class="Normal">if</span> <span class="Delimiter">(</span>Top_of_screen &lt;= <span class="Constant">0</span><span class="Delimiter">)</span> <span class="Identifier">break</span><span class="Delimiter">;</span>
        <span class="Normal">while</span> <span class="Delimiter">(</span>Top_of_screen &gt; <span class="Constant">0</span> &amp;&amp; !contains_key<span class="Delimiter">(</span>Visible<span class="Delimiter">,</span> Top_of_screen<span class="Delimiter">))</span>
          --Top_of_screen<span class="Delimiter">;</span>
      <span class="Delimiter">}</span>
      <span class="Normal">if</span> <span class="Delimiter">(</span>Top_of_screen &gt;= <span class="Constant">0</span><span class="Delimiter">)</span>
        refresh_screen_rows<span class="Delimiter">();</span>
    <span class="Delimiter">}</span>
    <span class="Normal">if</span> <span class="Delimiter">(</span>key == <span class="Constant">'G'</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
      <span class="Comment">// go to bottom of screen; largely like page-up, interestingly</span>
      Top_of_screen = SIZE<span class="Delimiter">(</span>Trace_stream<span class="Delimiter">-&gt;</span>past_lines<span class="Delimiter">)</span>-<span class="Constant">1</span><span class="Delimiter">;</span>
      <span class="Normal">for</span> <span class="Delimiter">(</span><span class="Normal">int</span> screen_row = tb_height<span class="Delimiter">();</span> screen_row &gt; <span class="Constant">0</span> &amp;&amp; Top_of_screen &gt; <span class="Constant">0</span><span class="Delimiter">;</span> --screen_row<span class="Delimiter">)</span> <span class="Delimiter">{</span>
        --Top_of_screen<span class="Delimiter">;</span>
        <span class="Normal">if</span> <span class="Delimiter">(</span>Top_of_screen &lt;= <span class="Constant">0</span><span class="Delimiter">)</span> <span class="Identifier">break</span><span class="Delimiter">;</span>
        <span class="Normal">while</span> <span class="Delimiter">(</span>Top_of_screen &gt; <span class="Constant">0</span> &amp;&amp; !contains_key<span class="Delimiter">(</span>Visible<span class="Delimiter">,</span> Top_of_screen<span class="Delimiter">))</span>
          --Top_of_screen<span class="Delimiter">;</span>
      <span class="Delimiter">}</span>
      refresh_screen_rows<span class="Delimiter">();</span>
      <span class="Comment">// move cursor to bottom</span>
      Display_row = Last_printed_row<span class="Delimiter">;</span>
      refresh_screen_rows<span class="Delimiter">();</span>
    <span class="Delimiter">}</span>
    <span class="Normal">if</span> <span class="Delimiter">(</span>key == TB_KEY_CARRIAGE_RETURN<span class="Delimiter">)</span> <span class="Delimiter">{</span>
      <span class="Comment">// expand lines under current by one level</span>
      assert<span class="Delimiter">(</span>contains_key<span class="Delimiter">(</span>Trace_index<span class="Delimiter">,</span> Display_row<span class="Delimiter">));</span>
      <span class="Normal">int</span> start_index = get<span class="Delimiter">(</span>Trace_index<span class="Delimiter">,</span> Display_row<span class="Delimiter">);</span>
      <span class="Normal">int</span> index = <span class="Constant">0</span><span class="Delimiter">;</span>
      <span class="Comment">// simultaneously compute end_index and min_depth</span>
      <span class="Normal">int</span> min_depth = <span class="Constant">9999</span><span class="Delimiter">;</span>
      <span class="Normal">for</span> <span class="Delimiter">(</span>index = start_index+<span class="Constant">1</span><span class="Delimiter">;</span> index &lt; SIZE<span class="Delimiter">(</span>Trace_stream<span class="Delimiter">-&gt;</span>past_lines<span class="Delimiter">);</span> ++index<span class="Delimiter">)</span> <span class="Delimiter">{</span>
        <span class="Normal">if</span> <span class="Delimiter">(</span>contains_key<span class="Delimiter">(</span>Visible<span class="Delimiter">,</span> index<span class="Delimiter">))</span> <span class="Identifier">break</span><span class="Delimiter">;</span>
        trace_line&amp; curr_line = Trace_stream<span class="Delimiter">-&gt;</span>past_lines<span class="Delimiter">.</span>at<span class="Delimiter">(</span>index<span class="Delimiter">);</span>
        assert<span class="Delimiter">(</span>curr_line<span class="Delimiter">.</span>depth &gt; Trace_stream<span class="Delimiter">-&gt;</span>past_lines<span class="Delimiter">.</span>at<span class="Delimiter">(</span>start_index<span class="Delimiter">).</span>depth<span class="Delimiter">);</span>
        <span class="Normal">if</span> <span class="Delimiter">(</span>curr_line<span class="Delimiter">.</span>depth &lt; min_depth<span class="Delimiter">)</span> min_depth = curr_line<span class="Delimiter">.</span>depth<span class="Delimiter">;</span>
      <span class="Delimiter">}</span>
      <span class="Normal">int</span> end_index = index<span class="Delimiter">;</span>
      <span class="Comment">// mark as visible all intervening indices at min_depth</span>
      <span class="Normal">for</span> <span class="Delimiter">(</span>index = start_index<span class="Delimiter">;</span> index &lt; end_index<span class="Delimiter">;</span> ++index<span class="Delimiter">)</span> <span class="Delimiter">{</span>
        trace_line&amp; curr_line = Trace_stream<span class="Delimiter">-&gt;</span>past_lines<span class="Delimiter">.</span>at<span class="Delimiter">(</span>index<span class="Delimiter">);</span>
        <span class="Normal">if</span> <span class="Delimiter">(</span>curr_line<span class="Delimiter">.</span>depth == min_depth<span class="Delimiter">)</span> <span class="Delimiter">{</span>
          Visible<span class="Delimiter">.</span>insert<span class="Delimiter">(</span>index<span class="Delimiter">);</span>
        <span class="Delimiter">}</span>
      <span class="Delimiter">}</span>
      refresh_screen_rows<span class="Delimiter">();</span>
    <span class="Delimiter">}</span>
    <span class="Normal">if</span> <span class="Delimiter">(</span>key == TB_KEY_BACKSPACE || key == TB_KEY_BACKSPACE2<span class="Delimiter">)</span> <span class="Delimiter">{</span>
      <span class="Comment">// collapse all lines under current</span>
      assert<span class="Delimiter">(</span>contains_key<span class="Delimiter">(</span>Trace_index<span class="Delimiter">,</span> Display_row<span class="Delimiter">));</span>
      <span class="Normal">int</span> start_index = get<span class="Delimiter">(</span>Trace_index<span class="Delimiter">,</span> Display_row<span class="Delimiter">);</span>
      <span class="Normal">int</span> index = <span class="Constant">0</span><span class="Delimiter">;</span>
      <span class="Comment">// end_index is the next line at a depth same as or lower than start_index</span>
      <span class="Normal">int</span> initial_depth = Trace_stream<span class="Delimiter">-&gt;</span>past_lines<span class="Delimiter">.</span>at<span class="Delimiter">(</span>start_index<span class="Delimiter">).</span>depth<span class="Delimiter">;</span>
      <span class="Normal">for</span> <span class="Delimiter">(</span>index = start_index+<span class="Constant">1</span><span class="Delimiter">;</span> index &lt; SIZE<span class="Delimiter">(</span>Trace_stream<span class="Delimiter">-&gt;</span>past_lines<span class="Delimiter">);</span> ++index<span class="Delimiter">)</span> <span class="Delimiter">{</span>
        <span class="Normal">if</span> <span class="Delimiter">(</span>!contains_key<span class="Delimiter">(</span>Visible<span class="Delimiter">,</span> index<span class="Delimiter">))</span> <span class="Identifier">continue</span><span class="Delimiter">;</span>
        trace_line&amp; curr_line = Trace_stream<span class="Delimiter">-&gt;</span>past_lines<span class="Delimiter">.</span>at<span class="Delimiter">(</span>index<span class="Delimiter">);</span>
        <span class="Normal">if</span> <span class="Delimiter">(</span>curr_line<span class="Delimiter">.</span>depth &lt;= initial_depth<span class="Delimiter">)</span> <span class="Identifier">break</span><span class="Delimiter">;</span>
      <span class="Delimiter">}</span>
      <span class="Normal">int</span> end_index = index<span class="Delimiter">;</span>
      <span class="Comment">// mark as visible all intervening indices at min_depth</span>
      <span class="Normal">for</span> <span class="Delimiter">(</span>index = start_index+<span class="Constant">1</span><span class="Delimiter">;</span> index &lt; end_index<span class="Delimiter">;</span> ++index<span class="Delimiter">)</span> <span class="Delimiter">{</span>
        Visible<span class="Delimiter">.</span>erase<span class="Delimiter">(</span>index<span class="Delimiter">);</span>
      <span class="Delimiter">}</span>
      refresh_screen_rows<span class="Delimiter">();</span>
    <span class="Delimiter">}</span>
  <span class="Delimiter">}</span>
  tb_shutdown<span class="Delimiter">();</span>
<span class="Delimiter">}</span>

<span class="Comment">// update Trace_indices for each screen_row on the basis of Top_of_screen and Visible</span>
<span class="Normal">void</span> refresh_screen_rows<span class="Delimiter">()</span> <span class="Delimiter">{</span>
  <span class="Normal">int</span> screen_row = <span class="Constant">0</span><span class="Delimiter">,</span> index = <span class="Constant">0</span><span class="Delimiter">;</span>
  Trace_index<span class="Delimiter">.</span>clear<span class="Delimiter">();</span>
  <span class="Normal">for</span> <span class="Delimiter">(</span>screen_row = <span class="Constant">0</span><span class="Delimiter">,</span> index = Top_of_screen<span class="Delimiter">;</span> screen_row &lt; tb_height<span class="Delimiter">()</span> &amp;&amp; index &lt; SIZE<span class="Delimiter">(</span>Trace_stream<span class="Delimiter">-&gt;</span>past_lines<span class="Delimiter">);</span> ++screen_row<span class="Delimiter">,</span> ++index<span class="Delimiter">)</span> <span class="Delimiter">{</span>
    <span class="Comment">// skip lines without depth for now</span>
    <span class="Normal">while</span> <span class="Delimiter">(</span>!contains_key<span class="Delimiter">(</span>Visible<span class="Delimiter">,</span> index<span class="Delimiter">))</span> <span class="Delimiter">{</span>
      ++index<span class="Delimiter">;</span>
      <span class="Normal">if</span> <span class="Delimiter">(</span>index &gt;= SIZE<span class="Delimiter">(</span>Trace_stream<span class="Delimiter">-&gt;</span>past_lines<span class="Delimiter">))</span> <span class="Identifier">goto</span> done<span class="Delimiter">;</span>
    <span class="Delimiter">}</span>
    assert<span class="Delimiter">(</span>index &lt; SIZE<span class="Delimiter">(</span>Trace_stream<span class="Delimiter">-&gt;</span>past_lines<span class="Delimiter">));</span>
    put<span class="Delimiter">(</span>Trace_index<span class="Delimiter">,</span> screen_row<span class="Delimiter">,</span> index<span class="Delimiter">);</span>
  <span class="Delimiter">}</span>
<span class="Normal">done</span>:<span class="Delimiter">;</span>
<span class="Delimiter">}</span>

<span class="Normal">void</span> render<span class="Delimiter">()</span> <span class="Delimiter">{</span>
  <span class="Normal">int</span> screen_row = <span class="Constant">0</span><span class="Delimiter">;</span>
  <span class="Normal">for</span> <span class="Delimiter">(</span>screen_row = <span class="Constant">0</span><span class="Delimiter">;</span> screen_row &lt; tb_height<span class="Delimiter">();</span> ++screen_row<span class="Delimiter">)</span> <span class="Delimiter">{</span>
    <span class="Normal">if</span> <span class="Delimiter">(</span>!contains_key<span class="Delimiter">(</span>Trace_index<span class="Delimiter">,</span> screen_row<span class="Delimiter">))</span> <span class="Identifier">break</span><span class="Delimiter">;</span>
    trace_line&amp; curr_line = Trace_stream<span class="Delimiter">-&gt;</span>past_lines<span class="Delimiter">.</span>at<span class="Delimiter">(</span>get<span class="Delimiter">(</span>Trace_index<span class="Delimiter">,</span> screen_row<span class="Delimiter">));</span>
    ostringstream out<span class="Delimiter">;</span>
    out &lt;&lt; std::setw<span class="Delimiter">(</span><span class="Constant">4</span><span class="Delimiter">)</span> &lt;&lt; curr_line<span class="Delimiter">.</span>depth &lt;&lt; <span class="Constant">' '</span> &lt;&lt; curr_line<span class="Delimiter">.</span>label &lt;&lt; <span class="Constant">&quot;: &quot;</span> &lt;&lt; curr_line<span class="Delimiter">.</span>contents<span class="Delimiter">;</span>
    <span class="Normal">if</span> <span class="Delimiter">(</span>screen_row &lt; tb_height<span class="Delimiter">()</span>-<span class="Constant">1</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
      <span class="Normal">int</span> delta = lines_hidden<span class="Delimiter">(</span>screen_row<span class="Delimiter">);</span>
      <span class="Comment">// home-brew escape sequence for red</span>
      <span class="Normal">if</span> <span class="Delimiter">(</span>delta &gt; <span class="Constant">999</span><span class="Delimiter">)</span> out &lt;&lt; <span class="Constant">&quot;{&quot;</span><span class="Delimiter">;</span>
      out &lt;&lt; <span class="Constant">&quot; (&quot;</span> &lt;&lt; delta &lt;&lt; <span class="Constant">&quot;)&quot;</span><span class="Delimiter">;</span>
      <span class="Normal">if</span> <span class="Delimiter">(</span>delta &gt; <span class="Constant">999</span><span class="Delimiter">)</span> out &lt;&lt; <span class="Constant">&quot;}&quot;</span><span class="Delimiter">;</span>
    <span class="Delimiter">}</span>
    render_line<span class="Delimiter">(</span>screen_row<span class="Delimiter">,</span> out<span class="Delimiter">.</span>str<span class="Delimiter">());</span>
  <span class="Delimiter">}</span>
  <span class="Comment">// clear rest of screen</span>
  Last_printed_row = screen_row-<span class="Constant">1</span><span class="Delimiter">;</span>
  <span class="Normal">for</span> <span class="Delimiter">(;</span> screen_row &lt; tb_height<span class="Delimiter">();</span> ++screen_row<span class="Delimiter">)</span> <span class="Delimiter">{</span>
    render_line<span class="Delimiter">(</span>screen_row<span class="Delimiter">,</span> <span class="Constant">&quot;~&quot;</span><span class="Delimiter">);</span>
  <span class="Delimiter">}</span>
  <span class="Comment">// move cursor back to display row at the end</span>
  tb_set_cursor<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">,</span> Display_row<span class="Delimiter">);</span>
  tb_present<span class="Delimiter">();</span>
<span class="Delimiter">}</span>

<span class="Normal">int</span> lines_hidden<span class="Delimiter">(</span><span class="Normal">int</span> screen_row<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  assert<span class="Delimiter">(</span>contains_key<span class="Delimiter">(</span>Trace_index<span class="Delimiter">,</span> screen_row<span class="Delimiter">));</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>!contains_key<span class="Delimiter">(</span>Trace_index<span class="Delimiter">,</span> screen_row+<span class="Constant">1</span><span class="Delimiter">))</span>
    <span class="Identifier">return</span> SIZE<span class="Delimiter">(</span>Trace_stream<span class="Delimiter">-&gt;</span>past_lines<span class="Delimiter">)</span> - get<span class="Delimiter">(</span>Trace_index<span class="Delimiter">,</span> screen_row<span class="Delimiter">);</span>
  <span class="Normal">else</span>
    <span class="Identifier">return</span> get<span class="Delimiter">(</span>Trace_index<span class="Delimiter">,</span> screen_row+<span class="Constant">1</span><span class="Delimiter">)</span> - get<span class="Delimiter">(</span>Trace_index<span class="Delimiter">,</span> screen_row<span class="Delimiter">);</span>
<span class="Delimiter">}</span>

<span class="Normal">void</span> render_line<span class="Delimiter">(</span><span class="Normal">int</span> screen_row<span class="Delimiter">,</span> <span class="Normal">const</span> string&amp; s<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  <span class="Normal">int</span> col = <span class="Constant">0</span><span class="Delimiter">;</span>
  <span class="Normal">int</span> color = TB_WHITE<span class="Delimiter">;</span>
  <span class="Normal">for</span> <span class="Delimiter">(</span>col = <span class="Constant">0</span><span class="Delimiter">;</span> col &lt; tb_width<span class="Delimiter">()</span> &amp;&amp; col &lt; SIZE<span class="Delimiter">(</span>s<span class="Delimiter">);</span> ++col<span class="Delimiter">)</span> <span class="Delimiter">{</span>
    <span class="Normal">char</span> c = s<span class="Delimiter">.</span>at<span class="Delimiter">(</span>col<span class="Delimiter">);</span>  <span class="Comment">// todo: unicode</span>
    <span class="Normal">if</span> <span class="Delimiter">(</span>c == <span class="cSpecial">'\n'</span><span class="Delimiter">)</span> c = <span class="Constant">';'</span><span class="Delimiter">;</span>  <span class="Comment">// replace newlines with semi-colons</span>
    <span class="Comment">// escapes. hack: can't start a line with them.</span>
    <span class="Normal">if</span> <span class="Delimiter">(</span>c == <span class="Constant">'{'</span><span class="Delimiter">)</span> <span class="Delimiter">{</span> color = <span class="Comment">/*</span><span class="Comment">red</span><span class="Comment">*/</span><span class="Constant">1</span><span class="Delimiter">;</span> c = <span class="Constant">' '</span><span class="Delimiter">;</span> <span class="Delimiter">}</span>
    <span class="Normal">if</span> <span class="Delimiter">(</span>c == <span class="Constant">'}'</span><span class="Delimiter">)</span> <span class="Delimiter">{</span> color = TB_WHITE<span class="Delimiter">;</span> c = <span class="Constant">' '</span><span class="Delimiter">;</span> <span class="Delimiter">}</span>
    tb_change_cell<span class="Delimiter">(</span>col<span class="Delimiter">,</span> screen_row<span class="Delimiter">,</span> c<span class="Delimiter">,</span> color<span class="Delimiter">,</span> TB_BLACK<span class="Delimiter">);</span>
  <span class="Delimiter">}</span>
  <span class="Normal">for</span> <span class="Delimiter">(;</span> col &lt; tb_width<span class="Delimiter">();</span> ++col<span class="Delimiter">)</span> <span class="Delimiter">{</span>
    tb_change_cell<span class="Delimiter">(</span>col<span class="Delimiter">,</span> screen_row<span class="Delimiter">,</span> <span class="Constant">' '</span><span class="Delimiter">,</span> TB_WHITE<span class="Delimiter">,</span> TB_BLACK<span class="Delimiter">);</span>
  <span class="Delimiter">}</span>
<span class="Delimiter">}</span>

<span class="Normal">void</span> load_trace<span class="Delimiter">(</span><span class="Normal">const</span> <span class="Normal">char</span>* filename<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  ifstream tin<span class="Delimiter">(</span>filename<span class="Delimiter">);</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>!tin<span class="Delimiter">)</span> <span class="Delimiter">{</span>
    cerr &lt;&lt; <span class="Constant">&quot;no such file: &quot;</span> &lt;&lt; filename &lt;&lt; <span class="cSpecial">'\n'</span><span class="Delimiter">;</span>
    exit<span class="Delimiter">(</span><span class="Constant">1</span><span class="Delimiter">);</span>
  <span class="Delimiter">}</span>
  Trace_stream = <span class="Normal">new</span> trace_stream<span class="Delimiter">;</span>
  <span class="Normal">while</span> <span class="Delimiter">(</span>has_data<span class="Delimiter">(</span>tin<span class="Delimiter">))</span> <span class="Delimiter">{</span>
    tin &gt;&gt; std::noskipws<span class="Delimiter">;</span>
      skip_whitespace_but_not_newline<span class="Delimiter">(</span>tin<span class="Delimiter">);</span>
      <span class="Normal">if</span> <span class="Delimiter">(</span>!isdigit<span class="Delimiter">(</span>tin<span class="Delimiter">.</span>peek<span class="Delimiter">()))</span> <span class="Delimiter">{</span>
        string dummy<span class="Delimiter">;</span>
        getline<span class="Delimiter">(</span>tin<span class="Delimiter">,</span> dummy<span class="Delimiter">);</span>
        <span class="Identifier">continue</span><span class="Delimiter">;</span>
      <span class="Delimiter">}</span>
    tin &gt;&gt; std::skipws<span class="Delimiter">;</span>
    <span class="Normal">int</span> depth<span class="Delimiter">;</span>
    tin &gt;&gt; depth<span class="Delimiter">;</span>
    string label<span class="Delimiter">;</span>
    tin &gt;&gt; label<span class="Delimiter">;</span>
    <span class="Normal">if</span> <span class="Delimiter">(</span>*--label<span class="Delimiter">.</span>end<span class="Delimiter">()</span> == <span class="Constant">':'</span><span class="Delimiter">)</span> label<span class="Delimiter">.</span>erase<span class="Delimiter">(</span>--label<span class="Delimiter">.</span>end<span class="Delimiter">());</span>
    string line<span class="Delimiter">;</span>
    getline<span class="Delimiter">(</span>tin<span class="Delimiter">,</span> line<span class="Delimiter">);</span>
    Trace_stream<span class="Delimiter">-&gt;</span>past_lines<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>trace_line<span class="Delimiter">(</span>depth<span class="Delimiter">,</span> label<span class="Delimiter">,</span> line<span class="Delimiter">));</span>
  <span class="Delimiter">}</span>
  cerr &lt;&lt; <span class="Constant">&quot;lines read: &quot;</span> &lt;&lt; Trace_stream<span class="Delimiter">-&gt;</span>past_lines<span class="Delimiter">.</span>size<span class="Delimiter">()</span> &lt;&lt; <span class="cSpecial">'\n'</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
</pre>
</body>
</html>
<!-- vim: set foldmethod=manual : -->