about summary refs log blame commit diff stats
path: root/html/085scenario_console.cc.html
blob: f0d114d62b8cb5e1404a8d76f7a2c2ed732a778b (plain) (tree)
1
2
3
4
5
6
7
8
9
10



                                                                                          
                                          

                                                
                                  

                                                                                         

                       
                                                                                                 
                                                                                            
                                      
                             
                             
                            



                                                                           

        





                               

       
                         












                                                                                                    



                                                                                                                                                                                                                                                                                                                         














                                                                                                                                                         
                                                                                                                                                  

                                                                                                    


                                                                              
                                                                                                                                                                                                                                                               
 


                                                                                      

                                                                                                                                                                                                                          
                                                                                 

                                                                       
                                                                                         
                                                                                 

                                                                                                                                                                                                                                                                                                                                                                                     

                                                                                                                 
                                                                                                                             
                                                                                
                                                                                                                                                                                                                                                                                                                                                                                               
                                                                                   
                                                                                                                                               


                                                                                                                                                                                                                                                                                                                                                        
                                                                                                                                                                                                                                                                                                                                                             

                                                                                                                                                                                                                                    
                                                                                                                                                                                                                                                                                                                                                                                                       



                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        

                                                                                                                 
                                                                                                                                                                                                                                                                
                                                                                                                                                                                                                                                                                                                                                                                                  
                                                                                                                                                                                                                                        
                                                                                                                                                   
                                                                                                                                                                                                                                                                                          
                                                                                                                                                                                                                          
                                                                                                                                                                                                                                                    


                                                                                                                                                                                                                                                                                                                                                        
                                                               
                                                                                                                                                                                                                                                                                                                                                                                           
                                                                       
                                                                
                                                                                                                                                                                                                                                                                                                                                                                              
                                      


                                                                                                                 
                                                                     

                                                                                                                                                                       
                                                                                                                                                                                                                                                                                                                                                                                                 

                                                                                                                                                                                                                                                                                    


                                                                                                                                                                                                                                                                                                         
                                                                                                                                                                                                                                                                                                                                                                                        
                                                                                                                                                                                                                                                                                                                                                                                           
                                                                                           

                                                                                                                                                                           
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
                                                                                                                                                                                                                                                                                                                                                                                 





                                                                                                                                                            


                                                                                                                                                                                                             
                                                                                                                                                                                                                                                                                                                                                                               
                                                                                                               
                                                                                                                                                                                

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        

                                                                                                                                                                                               


                                                                       
                                                                
                                                                                                                     


                                                                       
                                                                                                                       





















































                                                                                                                                                                                        




                                                                                                              
                  



                                                                                     


                                                                                                               
                                              
                                                                                                                
                                                        
                                                                                                                
                                                       
                                                                                                                


















                                                                                                                                                       
                                                                                                                                                    














                                                                                                                                              

                                                                                                                                                                                                                                  
                                                                                     

                                                                       
                                                                                         
                                                                                     
                                                                                                                                                                                                                             

                                                                                                                                                                                                                               

                                                                         



                                                                                                                                                                                                                                                                                                                                                                                                                                

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
                                                                                                                                                                                                                                                                  
                                                                                                                                                                                                                                                                                                                                         




                                                                       


                                                                                                                                                                                                                                                                                                                                                             

                                                                                                                                                                                                        
                                                                                                                                                                                                                                                                                    
                                    
                                              



                                                                               
                                                                                                               
                                                                    
                                                                                                                                             

                                                                                                                                                                                                                                                                                                                                              
                                                                                       
                                                                         


                                                                               
                                                                                                                 
                                                                    
                                                                                                                                             
                                                                                                                                                                                     
                                                                                                                                                                                                           
                                                                                                                                                                                                                                                                                                                                                  
                                                                                                                                                                                                                                                         
                                                                         




                                                                               
                                     
<!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 - 085scenario_console.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; }
.cSpecial { color: #008000; }
.Comment { color: #9090ff; }
.Delimiter { color: #800080; }
.Special { color: #c00000; }
.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">//: Clean syntax to manipulate and check the console in scenarios.</span>
<span class="Comment">//: Instruction 'assume-console' implicitly creates a variable called</span>
<span class="Comment">//: 'console' that is accessible inside other 'run' instructions in the</span>
<span class="Comment">//: scenario. Like with the fake screen, 'assume-console' transparently</span>
<span class="Comment">//: supports unicode.</span>

<span class="Delimiter">:(scenarios run_mu_scenario)</span>
<span class="Delimiter">:(scenario keyboard_in_scenario)</span>
scenario keyboard-in-scenario [
  assume-console [
    type [abc]
  ]
  run [
    <span class="Constant">1</span>:character<span class="Delimiter">,</span> console:address:console<span class="Delimiter">,</span> <span class="Constant">2</span>:boolean<span class="Special"> &lt;- </span>read-key console:address:console
    <span class="Constant">3</span>:character<span class="Delimiter">,</span> console:address:console<span class="Delimiter">,</span> <span class="Constant">4</span>:boolean<span class="Special"> &lt;- </span>read-key console:address:console
    <span class="Constant">5</span>:character<span class="Delimiter">,</span> console:address:console<span class="Delimiter">,</span> <span class="Constant">6</span>:boolean<span class="Special"> &lt;- </span>read-key console:address:console
    <span class="Constant">7</span>:character<span class="Delimiter">,</span> console:address:console<span class="Delimiter">,</span> <span class="Constant">8</span>:boolean<span class="Delimiter">,</span> <span class="Constant">9</span>:boolean<span class="Special"> &lt;- </span>read-key console:address:console
  ]
  memory-should-contain [
    <span class="Constant">1</span><span class="Special"> &lt;- </span><span class="Constant">97</span>  <span class="Comment"># 'a'</span>
    <span class="Constant">2</span><span class="Special"> &lt;- </span><span class="Constant">1</span>
    <span class="Constant">3</span><span class="Special"> &lt;- </span><span class="Constant">98</span>  <span class="Comment"># 'b'</span>
    <span class="Constant">4</span><span class="Special"> &lt;- </span><span class="Constant">1</span>
    <span class="Constant">5</span><span class="Special"> &lt;- </span><span class="Constant">99</span>  <span class="Comment"># 'c'</span>
    <span class="Constant">6</span><span class="Special"> &lt;- </span><span class="Constant">1</span>
    <span class="Constant">7</span><span class="Special"> &lt;- </span><span class="Constant">0</span>  <span class="Comment"># unset</span>
    <span class="Constant">8</span><span class="Special"> &lt;- </span><span class="Constant">1</span>
    <span class="Constant">9</span><span class="Special"> &lt;- </span><span class="Constant">1</span>  <span class="Comment"># end of test events</span>
  ]
]

<span class="Delimiter">:(before &quot;End Scenario Globals&quot;)</span>
<span class="Normal">const</span> <span class="Normal">int</span> CONSOLE = Next_predefined_global_for_scenarios++<span class="Delimiter">;</span>
<span class="Delimiter">:(before &quot;End Special Scenario Variable Names(r)&quot;)</span>
Name[r][<span class="Constant">&quot;console&quot;</span>] = CONSOLE<span class="Delimiter">;</span>

<span class="Comment">//: allow naming just for 'console'</span>
<span class="Delimiter">:(before &quot;End is_special_name Cases&quot;)</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>s == <span class="Constant">&quot;console&quot;</span><span class="Delimiter">)</span> <span class="Identifier">return</span> <span class="Constant">true</span><span class="Delimiter">;</span>

<span class="Delimiter">:(before &quot;End Primitive Recipe Declarations&quot;)</span>
ASSUME_CONSOLE<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;assume-console&quot;</span><span class="Delimiter">,</span> ASSUME_CONSOLE<span class="Delimiter">);</span>
<span class="Delimiter">:(before &quot;End Primitive Recipe Checks&quot;)</span>
<span class="Normal">case</span> ASSUME_CONSOLE: <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> ASSUME_CONSOLE: <span class="Delimiter">{</span>
  <span class="Comment">// create a temporary recipe just for parsing; it won't contain valid instructions</span>
  istringstream in<span class="Delimiter">(</span><span class="Constant">&quot;[&quot;</span> + current_instruction<span class="Delimiter">().</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>name + <span class="Constant">&quot;]&quot;</span><span class="Delimiter">);</span>
  recipe r<span class="Delimiter">;</span>
  slurp_body<span class="Delimiter">(</span>in<span class="Delimiter">,</span> r<span class="Delimiter">);</span>
  <span class="Normal">int</span> num_events = count_events<span class="Delimiter">(</span>r<span class="Delimiter">);</span>
  <span class="Comment">// initialize the events like in new-fake-console</span>
  <span class="Normal">int</span> size = <span class="Comment">/*</span><span class="Comment">space for refcount</span><span class="Comment">*/</span><span class="Constant">1</span> + <span class="Comment">/*</span><span class="Comment">space for length</span><span class="Comment">*/</span><span class="Constant">1</span> + num_events*size_of_event<span class="Delimiter">();</span>
  ensure_space<span class="Delimiter">(</span>size<span class="Delimiter">);</span>
  <span class="Normal">int</span> event_data_address = Current_routine<span class="Delimiter">-&gt;</span>alloc<span class="Delimiter">;</span>
  <span class="Comment">// store length</span>
  put<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> Current_routine<span class="Delimiter">-&gt;</span>alloc+<span class="Comment">/*</span><span class="Comment">skip refcount</span><span class="Comment">*/</span><span class="Constant">1</span><span class="Delimiter">,</span> num_events<span class="Delimiter">);</span>
  Current_routine<span class="Delimiter">-&gt;</span>alloc += <span class="Comment">/*</span><span class="Comment">skip refcount and length</span><span class="Comment">*/</span><span class="Constant">2</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>r<span class="Delimiter">.</span>steps<span class="Delimiter">);</span> ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span>
    <span class="Normal">const</span> instruction&amp; curr = r<span class="Delimiter">.</span>steps<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">);</span>
    <span class="Normal">if</span> <span class="Delimiter">(</span>curr<span class="Delimiter">.</span>name == <span class="Constant">&quot;left-click&quot;</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
      trace<span class="Delimiter">(</span><span class="Constant">9999</span><span class="Delimiter">,</span> <span class="Constant">&quot;mem&quot;</span><span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot;storing 'left-click' event starting at &quot;</span> &lt;&lt; Current_routine<span class="Delimiter">-&gt;</span>alloc &lt;&lt; end<span class="Delimiter">();</span>
      put<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> Current_routine<span class="Delimiter">-&gt;</span>alloc<span class="Delimiter">,</span> <span class="Comment">/*</span><span class="Comment">tag for 'touch-event' variant of 'event' exclusive-container</span><span class="Comment">*/</span><span class="Constant">2</span><span class="Delimiter">);</span>
      put<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> Current_routine<span class="Delimiter">-&gt;</span>alloc+<span class="Constant">1</span>+<span class="Comment">/*</span><span class="Comment">offset of 'type' in 'mouse-event'</span><span class="Comment">*/</span><span class="Constant">0</span><span class="Delimiter">,</span> TB_KEY_MOUSE_LEFT<span class="Delimiter">);</span>
      put<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> Current_routine<span class="Delimiter">-&gt;</span>alloc+<span class="Constant">1</span>+<span class="Comment">/*</span><span class="Comment">offset of 'row' in 'mouse-event'</span><span class="Comment">*/</span><span class="Constant">1</span><span class="Delimiter">,</span> to_integer<span class="Delimiter">(</span>curr<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>name<span class="Delimiter">));</span>
      put<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> Current_routine<span class="Delimiter">-&gt;</span>alloc+<span class="Constant">1</span>+<span class="Comment">/*</span><span class="Comment">offset of 'column' in 'mouse-event'</span><span class="Comment">*/</span><span class="Constant">2</span><span class="Delimiter">,</span> to_integer<span class="Delimiter">(</span>curr<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">1</span><span class="Delimiter">).</span>name<span class="Delimiter">));</span>
      Current_routine<span class="Delimiter">-&gt;</span>alloc += size_of_event<span class="Delimiter">();</span>
    <span class="Delimiter">}</span>
    <span class="Normal">else</span> <span class="Normal">if</span> <span class="Delimiter">(</span>curr<span class="Delimiter">.</span>name == <span class="Constant">&quot;press&quot;</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
      trace<span class="Delimiter">(</span><span class="Constant">9999</span><span class="Delimiter">,</span> <span class="Constant">&quot;mem&quot;</span><span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot;storing 'press' event starting at &quot;</span> &lt;&lt; Current_routine<span class="Delimiter">-&gt;</span>alloc &lt;&lt; end<span class="Delimiter">();</span>
      string key = curr<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>name<span class="Delimiter">;</span>
      <span class="Normal">if</span> <span class="Delimiter">(</span>is_integer<span class="Delimiter">(</span>key<span class="Delimiter">))</span>
        put<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> Current_routine<span class="Delimiter">-&gt;</span>alloc+<span class="Constant">1</span><span class="Delimiter">,</span> to_integer<span class="Delimiter">(</span>key<span class="Delimiter">));</span>
      <span class="Normal">else</span> <span class="Normal">if</span> <span class="Delimiter">(</span>contains_key<span class="Delimiter">(</span>Key<span class="Delimiter">,</span> key<span class="Delimiter">))</span>
        put<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> Current_routine<span class="Delimiter">-&gt;</span>alloc+<span class="Constant">1</span><span class="Delimiter">,</span> Key[key]<span class="Delimiter">);</span>
      <span class="Normal">else</span>
        raise &lt;&lt; <span class="Constant">&quot;assume-console: can't press &quot;</span> &lt;&lt; key &lt;&lt; <span class="cSpecial">'\n'</span> &lt;&lt; end<span class="Delimiter">();</span>
      <span class="Normal">if</span> <span class="Delimiter">(</span>get_or_insert<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> Current_routine<span class="Delimiter">-&gt;</span>alloc+<span class="Constant">1</span><span class="Delimiter">)</span> &lt; <span class="Constant">256</span><span class="Delimiter">)</span>
        <span class="Comment">// these keys are in ascii</span>
        put<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> Current_routine<span class="Delimiter">-&gt;</span>alloc<span class="Delimiter">,</span> <span class="Comment">/*</span><span class="Comment">tag for 'text' variant of 'event' exclusive-container</span><span class="Comment">*/</span><span class="Constant">0</span><span class="Delimiter">);</span>
      <span class="Normal">else</span> <span class="Delimiter">{</span>
        <span class="Comment">// distinguish from unicode</span>
        put<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> Current_routine<span class="Delimiter">-&gt;</span>alloc<span class="Delimiter">,</span> <span class="Comment">/*</span><span class="Comment">tag for 'keycode' variant of 'event' exclusive-container</span><span class="Comment">*/</span><span class="Constant">1</span><span class="Delimiter">);</span>
      <span class="Delimiter">}</span>
      Current_routine<span class="Delimiter">-&gt;</span>alloc += size_of_event<span class="Delimiter">();</span>
    <span class="Delimiter">}</span>
    <span class="Comment">// End Event Handlers</span>
    <span class="Normal">else</span> <span class="Delimiter">{</span>
      <span class="Comment">// keyboard input</span>
      assert<span class="Delimiter">(</span>curr<span class="Delimiter">.</span>name == <span class="Constant">&quot;type&quot;</span><span class="Delimiter">);</span>
      trace<span class="Delimiter">(</span><span class="Constant">9999</span><span class="Delimiter">,</span> <span class="Constant">&quot;mem&quot;</span><span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot;storing 'type' event starting at &quot;</span> &lt;&lt; Current_routine<span class="Delimiter">-&gt;</span>alloc &lt;&lt; end<span class="Delimiter">();</span>
      <span class="Normal">const</span> string&amp; contents = curr<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>name<span class="Delimiter">;</span>
      <span class="Normal">const</span> <span class="Normal">char</span>* raw_contents = contents<span class="Delimiter">.</span>c_str<span class="Delimiter">();</span>
      <span class="Normal">int</span> num_keyboard_events = unicode_length<span class="Delimiter">(</span>contents<span class="Delimiter">);</span>
      <span class="Normal">int</span> curr = <span class="Constant">0</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; num_keyboard_events<span class="Delimiter">;</span> ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span>
        trace<span class="Delimiter">(</span><span class="Constant">9999</span><span class="Delimiter">,</span> <span class="Constant">&quot;mem&quot;</span><span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot;storing 'text' tag at &quot;</span> &lt;&lt; Current_routine<span class="Delimiter">-&gt;</span>alloc &lt;&lt; end<span class="Delimiter">();</span>
        put<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> Current_routine<span class="Delimiter">-&gt;</span>alloc<span class="Delimiter">,</span> <span class="Comment">/*</span><span class="Comment">tag for 'text' variant of 'event' exclusive-container</span><span class="Comment">*/</span><span class="Constant">0</span><span class="Delimiter">);</span>
        <span class="Normal">uint32_t</span> curr_character<span class="Delimiter">;</span>
        assert<span class="Delimiter">(</span>curr &lt; SIZE<span class="Delimiter">(</span>contents<span class="Delimiter">));</span>
        tb_utf8_char_to_unicode<span class="Delimiter">(</span>&amp;curr_character<span class="Delimiter">,</span> &amp;raw_contents[curr]<span class="Delimiter">);</span>
        trace<span class="Delimiter">(</span><span class="Constant">9999</span><span class="Delimiter">,</span> <span class="Constant">&quot;mem&quot;</span><span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot;storing character &quot;</span> &lt;&lt; curr_character &lt;&lt; <span class="Constant">&quot; at &quot;</span> &lt;&lt; Current_routine<span class="Delimiter">-&gt;</span>alloc+<span class="Constant">1</span> &lt;&lt; end<span class="Delimiter">();</span>
        put<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> Current_routine<span class="Delimiter">-&gt;</span>alloc+<span class="Comment">/*</span><span class="Comment">skip exclusive container tag</span><span class="Comment">*/</span><span class="Constant">1</span><span class="Delimiter">,</span> curr_character<span class="Delimiter">);</span>
        curr += tb_utf8_char_length<span class="Delimiter">(</span>raw_contents[curr]<span class="Delimiter">);</span>
        Current_routine<span class="Delimiter">-&gt;</span>alloc += size_of_event<span class="Delimiter">();</span>
      <span class="Delimiter">}</span>
    <span class="Delimiter">}</span>
  <span class="Delimiter">}</span>
  assert<span class="Delimiter">(</span>Current_routine<span class="Delimiter">-&gt;</span>alloc == event_data_address+size<span class="Delimiter">);</span>
  <span class="Comment">// wrap the array of events in a console object</span>
  ensure_space<span class="Delimiter">(</span>size_of_console<span class="Delimiter">());</span>
  put<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> CONSOLE<span class="Delimiter">,</span> Current_routine<span class="Delimiter">-&gt;</span>alloc<span class="Delimiter">);</span>
  trace<span class="Delimiter">(</span><span class="Constant">9999</span><span class="Delimiter">,</span> <span class="Constant">&quot;mem&quot;</span><span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot;storing console in &quot;</span> &lt;&lt; Current_routine<span class="Delimiter">-&gt;</span>alloc &lt;&lt; end<span class="Delimiter">();</span>
  Current_routine<span class="Delimiter">-&gt;</span>alloc += size_of_console<span class="Delimiter">();</span>
  <span class="Normal">int</span> console_address = get_or_insert<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> CONSOLE<span class="Delimiter">);</span>
  trace<span class="Delimiter">(</span><span class="Constant">9999</span><span class="Delimiter">,</span> <span class="Constant">&quot;mem&quot;</span><span class="Delimiter">)</span> &lt;&lt; <span class="Constant">&quot;storing console data in &quot;</span> &lt;&lt; console_address+<span class="Constant">2</span> &lt;&lt; end<span class="Delimiter">();</span>
  put<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> console_address+<span class="Comment">/*</span><span class="Comment">skip refcount</span><span class="Comment">*/</span><span class="Constant">1</span>+<span class="Comment">/*</span><span class="Comment">offset of 'data' in container 'events'</span><span class="Comment">*/</span><span class="Constant">1</span><span class="Delimiter">,</span> event_data_address<span class="Delimiter">);</span>
  <span class="Comment">// increment refcount for event data</span>
  put<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> event_data_address<span class="Delimiter">,</span> <span class="Constant">1</span><span class="Delimiter">);</span>
  <span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>

<span class="Delimiter">:(before &quot;End Globals&quot;)</span>
map&lt;string<span class="Delimiter">,</span> <span class="Normal">int</span>&gt; Key<span class="Delimiter">;</span>
<span class="Delimiter">:(before &quot;End One-time Setup&quot;)</span>
initialize_key_names<span class="Delimiter">();</span>
<span class="Delimiter">:(code)</span>
<span class="Normal">void</span> initialize_key_names<span class="Delimiter">()</span> <span class="Delimiter">{</span>
  Key[<span class="Constant">&quot;F1&quot;</span>] = TB_KEY_F1<span class="Delimiter">;</span>
  Key[<span class="Constant">&quot;F2&quot;</span>] = TB_KEY_F2<span class="Delimiter">;</span>
  Key[<span class="Constant">&quot;F3&quot;</span>] = TB_KEY_F3<span class="Delimiter">;</span>
  Key[<span class="Constant">&quot;F4&quot;</span>] = TB_KEY_F4<span class="Delimiter">;</span>
  Key[<span class="Constant">&quot;F5&quot;</span>] = TB_KEY_F5<span class="Delimiter">;</span>
  Key[<span class="Constant">&quot;F6&quot;</span>] = TB_KEY_F6<span class="Delimiter">;</span>
  Key[<span class="Constant">&quot;F7&quot;</span>] = TB_KEY_F7<span class="Delimiter">;</span>
  Key[<span class="Constant">&quot;F8&quot;</span>] = TB_KEY_F8<span class="Delimiter">;</span>
  Key[<span class="Constant">&quot;F9&quot;</span>] = TB_KEY_F9<span class="Delimiter">;</span>
  Key[<span class="Constant">&quot;F10&quot;</span>] = TB_KEY_F10<span class="Delimiter">;</span>
  Key[<span class="Constant">&quot;F11&quot;</span>] = TB_KEY_F11<span class="Delimiter">;</span>
  Key[<span class="Constant">&quot;F12&quot;</span>] = TB_KEY_F12<span class="Delimiter">;</span>
  Key[<span class="Constant">&quot;insert&quot;</span>] = TB_KEY_INSERT<span class="Delimiter">;</span>
  Key[<span class="Constant">&quot;delete&quot;</span>] = TB_KEY_DELETE<span class="Delimiter">;</span>
  Key[<span class="Constant">&quot;home&quot;</span>] = TB_KEY_HOME<span class="Delimiter">;</span>
  Key[<span class="Constant">&quot;end&quot;</span>] = TB_KEY_END<span class="Delimiter">;</span>
  Key[<span class="Constant">&quot;page-up&quot;</span>] = TB_KEY_PGUP<span class="Delimiter">;</span>
  Key[<span class="Constant">&quot;page-down&quot;</span>] = TB_KEY_PGDN<span class="Delimiter">;</span>
  Key[<span class="Constant">&quot;up-arrow&quot;</span>] = TB_KEY_ARROW_UP<span class="Delimiter">;</span>
  Key[<span class="Constant">&quot;down-arrow&quot;</span>] = TB_KEY_ARROW_DOWN<span class="Delimiter">;</span>
  Key[<span class="Constant">&quot;left-arrow&quot;</span>] = TB_KEY_ARROW_LEFT<span class="Delimiter">;</span>
  Key[<span class="Constant">&quot;right-arrow&quot;</span>] = TB_KEY_ARROW_RIGHT<span class="Delimiter">;</span>
  Key[<span class="Constant">&quot;ctrl-a&quot;</span>] = TB_KEY_CTRL_A<span class="Delimiter">;</span>
  Key[<span class="Constant">&quot;ctrl-b&quot;</span>] = TB_KEY_CTRL_B<span class="Delimiter">;</span>
  Key[<span class="Constant">&quot;ctrl-c&quot;</span>] = TB_KEY_CTRL_C<span class="Delimiter">;</span>
  Key[<span class="Constant">&quot;ctrl-d&quot;</span>] = TB_KEY_CTRL_D<span class="Delimiter">;</span>
  Key[<span class="Constant">&quot;ctrl-e&quot;</span>] = TB_KEY_CTRL_E<span class="Delimiter">;</span>
  Key[<span class="Constant">&quot;ctrl-f&quot;</span>] = TB_KEY_CTRL_F<span class="Delimiter">;</span>
  Key[<span class="Constant">&quot;ctrl-g&quot;</span>] = TB_KEY_CTRL_G<span class="Delimiter">;</span>
  Key[<span class="Constant">&quot;backspace&quot;</span>] = TB_KEY_BACKSPACE<span class="Delimiter">;</span>
  Key[<span class="Constant">&quot;ctrl-h&quot;</span>] = TB_KEY_CTRL_H<span class="Delimiter">;</span>
  Key[<span class="Constant">&quot;tab&quot;</span>] = TB_KEY_TAB<span class="Delimiter">;</span>
  Key[<span class="Constant">&quot;ctrl-i&quot;</span>] = TB_KEY_CTRL_I<span class="Delimiter">;</span>
  Key[<span class="Constant">&quot;ctrl-j&quot;</span>] = TB_KEY_CTRL_J<span class="Delimiter">;</span>
  Key[<span class="Constant">&quot;enter&quot;</span>] = TB_KEY_NEWLINE<span class="Delimiter">;</span>  <span class="Comment">// ignore CR/LF distinction; there is only 'enter'</span>
  Key[<span class="Constant">&quot;ctrl-k&quot;</span>] = TB_KEY_CTRL_K<span class="Delimiter">;</span>
  Key[<span class="Constant">&quot;ctrl-l&quot;</span>] = TB_KEY_CTRL_L<span class="Delimiter">;</span>
  Key[<span class="Constant">&quot;ctrl-m&quot;</span>] = TB_KEY_CTRL_M<span class="Delimiter">;</span>
  Key[<span class="Constant">&quot;ctrl-n&quot;</span>] = TB_KEY_CTRL_N<span class="Delimiter">;</span>
  Key[<span class="Constant">&quot;ctrl-o&quot;</span>] = TB_KEY_CTRL_O<span class="Delimiter">;</span>
  Key[<span class="Constant">&quot;ctrl-p&quot;</span>] = TB_KEY_CTRL_P<span class="Delimiter">;</span>
  Key[<span class="Constant">&quot;ctrl-q&quot;</span>] = TB_KEY_CTRL_Q<span class="Delimiter">;</span>
  Key[<span class="Constant">&quot;ctrl-r&quot;</span>] = TB_KEY_CTRL_R<span class="Delimiter">;</span>
  Key[<span class="Constant">&quot;ctrl-s&quot;</span>] = TB_KEY_CTRL_S<span class="Delimiter">;</span>
  Key[<span class="Constant">&quot;ctrl-t&quot;</span>] = TB_KEY_CTRL_T<span class="Delimiter">;</span>
  Key[<span class="Constant">&quot;ctrl-u&quot;</span>] = TB_KEY_CTRL_U<span class="Delimiter">;</span>
  Key[<span class="Constant">&quot;ctrl-v&quot;</span>] = TB_KEY_CTRL_V<span class="Delimiter">;</span>
  Key[<span class="Constant">&quot;ctrl-w&quot;</span>] = TB_KEY_CTRL_W<span class="Delimiter">;</span>
  Key[<span class="Constant">&quot;ctrl-x&quot;</span>] = TB_KEY_CTRL_X<span class="Delimiter">;</span>
  Key[<span class="Constant">&quot;ctrl-y&quot;</span>] = TB_KEY_CTRL_Y<span class="Delimiter">;</span>
  Key[<span class="Constant">&quot;ctrl-z&quot;</span>] = TB_KEY_CTRL_Z<span class="Delimiter">;</span>
  Key[<span class="Constant">&quot;escape&quot;</span>] = TB_KEY_ESC<span class="Delimiter">;</span>
<span class="Delimiter">}</span>

<span class="Delimiter">:(scenario events_in_scenario)</span>
scenario events-in-scenario [
  assume-console [
    type [abc]
    left-click <span class="Constant">0</span><span class="Delimiter">,</span> <span class="Constant">1</span>
    press up-arrow
    type [d]
  ]
  run [
    <span class="Comment"># 3 keyboard events; each event occupies 4 locations</span>
    <span class="Constant">1</span>:event<span class="Special"> &lt;- </span>read-event console:address:console
    <span class="Constant">5</span>:event<span class="Special"> &lt;- </span>read-event console:address:console
    <span class="Constant">9</span>:event<span class="Special"> &lt;- </span>read-event console:address:console
    <span class="Comment"># mouse click</span>
    <span class="Constant">13</span>:event<span class="Special"> &lt;- </span>read-event console:address:console
    <span class="Comment"># non-character keycode</span>
    <span class="Constant">17</span>:event<span class="Special"> &lt;- </span>read-event console:address:console
    <span class="Comment"># final keyboard event</span>
    <span class="Constant">21</span>:event<span class="Special"> &lt;- </span>read-event console:address:console
  ]
  memory-should-contain [
    <span class="Constant">1</span><span class="Special"> &lt;- </span><span class="Constant">0</span>  <span class="Comment"># 'text'</span>
    <span class="Constant">2</span><span class="Special"> &lt;- </span><span class="Constant">97</span>  <span class="Comment"># 'a'</span>
    <span class="Constant">3</span><span class="Special"> &lt;- </span><span class="Constant">0</span>  <span class="Comment"># unused</span>
    <span class="Constant">4</span><span class="Special"> &lt;- </span><span class="Constant">0</span>  <span class="Comment"># unused</span>
    <span class="Constant">5</span><span class="Special"> &lt;- </span><span class="Constant">0</span>  <span class="Comment"># 'text'</span>
    <span class="Constant">6</span><span class="Special"> &lt;- </span><span class="Constant">98</span>  <span class="Comment"># 'b'</span>
    <span class="Constant">7</span><span class="Special"> &lt;- </span><span class="Constant">0</span>  <span class="Comment"># unused</span>
    <span class="Constant">8</span><span class="Special"> &lt;- </span><span class="Constant">0</span>  <span class="Comment"># unused</span>
    <span class="Constant">9</span><span class="Special"> &lt;- </span><span class="Constant">0</span>  <span class="Comment"># 'text'</span>
    <span class="Constant">10</span><span class="Special"> &lt;- </span><span class="Constant">99</span>  <span class="Comment"># 'c'</span>
    <span class="Constant">11</span><span class="Special"> &lt;- </span><span class="Constant">0</span>  <span class="Comment"># unused</span>
    <span class="Constant">12</span><span class="Special"> &lt;- </span><span class="Constant">0</span>  <span class="Comment"># unused</span>
    <span class="Constant">13</span><span class="Special"> &lt;- </span><span class="Constant">2</span>  <span class="Comment"># 'mouse'</span>
    <span class="Constant">14</span><span class="Special"> &lt;- </span><span class="Constant">65513</span>  <span class="Comment"># mouse click</span>
    <span class="Constant">15</span><span class="Special"> &lt;- </span><span class="Constant">0</span>  <span class="Comment"># row</span>
    <span class="Constant">16</span><span class="Special"> &lt;- </span><span class="Constant">1</span>  <span class="Comment"># column</span>
    <span class="Constant">17</span><span class="Special"> &lt;- </span><span class="Constant">1</span>  <span class="Comment"># 'keycode'</span>
    <span class="Constant">18</span><span class="Special"> &lt;- </span><span class="Constant">65517</span>  <span class="Comment"># up arrow</span>
    <span class="Constant">19</span><span class="Special"> &lt;- </span><span class="Constant">0</span>  <span class="Comment"># unused</span>
    <span class="Constant">20</span><span class="Special"> &lt;- </span><span class="Constant">0</span>  <span class="Comment"># unused</span>
    <span class="Constant">21</span><span class="Special"> &lt;- </span><span class="Constant">0</span>  <span class="Comment"># 'text'</span>
    <span class="Constant">22</span><span class="Special"> &lt;- </span><span class="Constant">100</span>  <span class="Comment"># 'd'</span>
    <span class="Constant">23</span><span class="Special"> &lt;- </span><span class="Constant">0</span>  <span class="Comment"># unused</span>
    <span class="Constant">24</span><span class="Special"> &lt;- </span><span class="Constant">0</span>  <span class="Comment"># unused</span>
    <span class="Constant">25</span><span class="Special"> &lt;- </span><span class="Constant">0</span>
  ]
]

<span class="Comment">//: Deal with special keys and unmatched brackets by allowing each test to</span>
<span class="Comment">//: independently choose the unicode symbol to denote them.</span>
<span class="Delimiter">:(before &quot;End Primitive Recipe Declarations&quot;)</span>
REPLACE_IN_CONSOLE<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;replace-in-console&quot;</span><span class="Delimiter">,</span> REPLACE_IN_CONSOLE<span class="Delimiter">);</span>
<span class="Delimiter">:(before &quot;End Primitive Recipe Checks&quot;)</span>
<span class="Normal">case</span> REPLACE_IN_CONSOLE: <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> REPLACE_IN_CONSOLE: <span class="Delimiter">{</span>
  assert<span class="Delimiter">(</span>scalar<span class="Delimiter">(</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">)));</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>!get_or_insert<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> CONSOLE<span class="Delimiter">))</span> <span class="Delimiter">{</span>
    raise &lt;&lt; <span class="Constant">&quot;console not initialized</span><span class="cSpecial">\n</span><span class="Constant">&quot;</span> &lt;&lt; end<span class="Delimiter">();</span>
    <span class="Identifier">break</span><span class="Delimiter">;</span>
  <span class="Delimiter">}</span>
  <span class="Normal">int</span> console_address = get_or_insert<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> CONSOLE<span class="Delimiter">);</span>
  <span class="Normal">int</span> console_data = get_or_insert<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> console_address+<span class="Constant">1</span><span class="Delimiter">);</span>
  <span class="Normal">int</span> size = get_or_insert<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> console_data<span class="Delimiter">);</span>  <span class="Comment">// array size</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> curr = console_data+<span class="Constant">1</span><span class="Delimiter">;</span> i &lt; size<span class="Delimiter">;</span> ++i<span class="Delimiter">,</span> curr+=size_of_event<span class="Delimiter">())</span> <span class="Delimiter">{</span>
    <span class="Normal">if</span> <span class="Delimiter">(</span>get_or_insert<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> curr<span class="Delimiter">)</span> != <span class="Comment">/*</span><span class="Comment">text</span><span class="Comment">*/</span><span class="Constant">0</span><span class="Delimiter">)</span> <span class="Identifier">continue</span><span class="Delimiter">;</span>
    <span class="Normal">if</span> <span class="Delimiter">(</span>get_or_insert<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> curr+<span class="Constant">1</span><span class="Delimiter">)</span> != ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">))</span> <span class="Identifier">continue</span><span class="Delimiter">;</span>
    <span class="Normal">for</span> <span class="Delimiter">(</span><span class="Normal">int</span> n = <span class="Constant">0</span><span class="Delimiter">;</span> n &lt; size_of_event<span class="Delimiter">();</span> ++n<span class="Delimiter">)</span>
      put<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> curr+n<span class="Delimiter">,</span> ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">1</span><span class="Delimiter">).</span>at<span class="Delimiter">(</span>n<span class="Delimiter">));</span>
  <span class="Delimiter">}</span>
  <span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>

<span class="Delimiter">:(code)</span>
<span class="Normal">int</span> count_events<span class="Delimiter">(</span><span class="Normal">const</span> recipe&amp; r<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  <span class="Normal">int</span> result = <span class="Constant">0</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>r<span class="Delimiter">.</span>steps<span class="Delimiter">);</span> ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span>
    <span class="Normal">const</span> instruction&amp; curr = r<span class="Delimiter">.</span>steps<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">);</span>
    <span class="Normal">if</span> <span class="Delimiter">(</span>curr<span class="Delimiter">.</span>name == <span class="Constant">&quot;type&quot;</span><span class="Delimiter">)</span>
      result += unicode_length<span class="Delimiter">(</span>curr<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>name<span class="Delimiter">);</span>
    <span class="Normal">else</span>
      result++<span class="Delimiter">;</span>
  <span class="Delimiter">}</span>
  <span class="Identifier">return</span> result<span class="Delimiter">;</span>
<span class="Delimiter">}</span>

<span class="Normal">int</span> size_of_event<span class="Delimiter">()</span> <span class="Delimiter">{</span>
  <span class="Comment">// memoize result if already computed</span>
  <span class="Normal">static</span> <span class="Normal">int</span> result = <span class="Constant">0</span><span class="Delimiter">;</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>result<span class="Delimiter">)</span> <span class="Identifier">return</span> result<span class="Delimiter">;</span>
  type_tree* type = <span class="Normal">new</span> type_tree<span class="Delimiter">(</span><span class="Constant">&quot;event&quot;</span><span class="Delimiter">,</span> get<span class="Delimiter">(</span>Type_ordinal<span class="Delimiter">,</span> <span class="Constant">&quot;event&quot;</span><span class="Delimiter">));</span>
  result = size_of<span class="Delimiter">(</span>type<span class="Delimiter">);</span>
  <span class="Normal">delete</span> type<span class="Delimiter">;</span>
  <span class="Identifier">return</span> result<span class="Delimiter">;</span>
<span class="Delimiter">}</span>

<span class="Normal">int</span> size_of_console<span class="Delimiter">()</span> <span class="Delimiter">{</span>
  <span class="Comment">// memoize result if already computed</span>
  <span class="Normal">static</span> <span class="Normal">int</span> result = <span class="Constant">0</span><span class="Delimiter">;</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>result<span class="Delimiter">)</span> <span class="Identifier">return</span> result<span class="Delimiter">;</span>
  assert<span class="Delimiter">(</span>get<span class="Delimiter">(</span>Type_ordinal<span class="Delimiter">,</span> <span class="Constant">&quot;console&quot;</span><span class="Delimiter">));</span>
  type_tree* type = <span class="Normal">new</span> type_tree<span class="Delimiter">(</span><span class="Constant">&quot;console&quot;</span><span class="Delimiter">,</span> get<span class="Delimiter">(</span>Type_ordinal<span class="Delimiter">,</span> <span class="Constant">&quot;console&quot;</span><span class="Delimiter">));</span>
  result = size_of<span class="Delimiter">(</span>type<span class="Delimiter">)</span>+<span class="Comment">/*</span><span class="Comment">refcount</span><span class="Comment">*/</span><span class="Constant">1</span><span class="Delimiter">;</span>
  <span class="Normal">delete</span> type<span class="Delimiter">;</span>
  <span class="Identifier">return</span> result<span class="Delimiter">;</span>
<span class="Delimiter">}</span>
</pre>
</body>
</html>
<!-- vim: set foldmethod=manual : -->