about summary refs log blame commit diff stats
path: root/html/075scenario_keyboard.cc.html
blob: b58ca988d2299b2f987b79b635d9454c2ea9c893 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12



                                                                                          
                                           






                                                                                         

                                                                                                 




                              
                               
                                  













                                                                                                     

                                                                                                     












                                                                                                                                                                                                                                   
                                                                                                      
                                                                                                                                           
                                                                                                      
                                                                                                                                           


                                                                                                                                          



                                                                         
                                                                                                     
                                                                                          
                                                                                                                                                                                                                                                
 



                                                                                                                                                                                                                                    










                                                                                                                                                                                                                                                          
                                                                                                                                                                                                                                                                  



                                                                                                                                                                                                                                                                              
                                                                                                                                         

                                                                                                                                                                                                                                                             
                                                                                                                                                                                                                                                                     

                                                                                                                                                                                                                                                          
                                                                                                                                                                                                                                                                  
                                





















                                                                                                                                                                                                                                                                                                                                       



                                     
<!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 - 075scenario_keyboard.cc</title>
<meta name="Generator" content="Vim/7.4">
<meta name="plugin-version" content="vim7.4_v1">
<meta name="syntax" content="cpp">
<meta name="settings" content="use_css,pre_wrap,no_foldcolumn,expand_tabs,prevent_copy=">
<meta name="colorscheme" content="minimal">
<style type="text/css">
<!--
pre { white-space: pre-wrap; font-family: monospace; color: #eeeeee; background-color: #080808; }
body { font-family: monospace; color: #eeeeee; background-color: #080808; }
* { font-size: 1em; }
.Constant { color: #008080; }
.Comment { color: #8080ff; }
.Delimiter { color: #c000c0; }
.Special { color: #ff6060; }
.Identifier { color: #804000; }
.CommentedCode { color: #6c6c6c; }
-->
</style>

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

-->
</script>
</head>
<body>
<pre id='vimCodeElement'>
<span class="Comment">//: Clean syntax to manipulate and check the keyboard in scenarios.</span>
<span class="Comment">//: Instruction 'assume-keyboard' implicitly creates a variable called</span>
<span class="Comment">//: 'keyboard' that is accessible inside other 'run' instructions in the</span>
<span class="Comment">//: scenario. Like with the fake screen, 'assume-keyboard' 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-keyboard [abc]
  run [
    <span class="Constant">1</span>:character<span class="Delimiter">,</span> <span class="Constant">2</span>:boolean<span class="Delimiter">,</span> keyboard:address<span class="Special"> &lt;- </span>read-key keyboard:address
    <span class="Constant">3</span>:character<span class="Delimiter">,</span> <span class="Constant">4</span>:boolean<span class="Delimiter">,</span> keyboard:address<span class="Special"> &lt;- </span>read-key keyboard:address
    <span class="Constant">5</span>:character<span class="Delimiter">,</span> <span class="Constant">6</span>:boolean<span class="Delimiter">,</span> keyboard:address<span class="Special"> &lt;- </span>read-key keyboard:address
    <span class="Constant">7</span>:character<span class="Delimiter">,</span> <span class="Constant">8</span>:boolean<span class="Delimiter">,</span> keyboard:address<span class="Special"> &lt;- </span>read-key keyboard:address
  ]
  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"># eof</span>
    <span class="Constant">8</span><span class="Special"> &lt;- </span><span class="Constant">1</span>
  ]
]

<span class="Delimiter">:(before &quot;End Scenario Globals&quot;)</span>
const long long int KEYBOARD = Next_predefined_global_for_scenarios++<span class="Delimiter">;</span>
<span class="Delimiter">:(before &quot;End Predefined Scenario Locals In Run&quot;)</span>
Name[tmp_recipe<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">)</span>][<span class="Constant">&quot;keyboard&quot;</span>] = KEYBOARD<span class="Delimiter">;</span>

<span class="Comment">//: allow naming just for 'keyword'</span>
<span class="Delimiter">:(before &quot;End is_special_name Cases&quot;)</span>
if <span class="Delimiter">(</span>s == <span class="Constant">&quot;keyboard&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 Rewrite Instruction(curr)&quot;)</span>
<span class="Comment">// rewrite `assume-keyboard string` to</span>
<span class="Comment">//   ```</span>
<span class="Comment">//   keyboard:address &lt;- new string  # hacky reuse of location</span>
<span class="Comment">//   keyboard:address &lt;- init-fake-keyboard keyboard:address</span>
<span class="Comment">//   ```</span>
if <span class="Delimiter">(</span>curr<span class="Delimiter">.</span>name == <span class="Constant">&quot;assume-keyboard&quot;</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
  <span class="Comment">// insert first instruction</span>
  curr<span class="Delimiter">.</span>operation = Recipe_number[<span class="Constant">&quot;new&quot;</span>]<span class="Delimiter">;</span>
  assert<span class="Delimiter">(</span>curr<span class="Delimiter">.</span>products<span class="Delimiter">.</span>empty<span class="Delimiter">());</span>
  curr<span class="Delimiter">.</span>products<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>reagent<span class="Delimiter">(</span><span class="Constant">&quot;keyboard:address&quot;</span><span class="Delimiter">));</span>
  curr<span class="Delimiter">.</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>set_value<span class="Delimiter">(</span>KEYBOARD<span class="Delimiter">);</span>
  result<span class="Delimiter">.</span>steps<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>curr<span class="Delimiter">);</span>  <span class="Comment">// hacky that &quot;Rewrite Instruction&quot; is converting to multiple instructions</span>
  <span class="Comment">// leave second instruction in curr</span>
  curr<span class="Delimiter">.</span>clear<span class="Delimiter">();</span>
  curr<span class="Delimiter">.</span>operation = Recipe_number[<span class="Constant">&quot;init-fake-keyboard&quot;</span>]<span class="Delimiter">;</span>
  curr<span class="Delimiter">.</span>name = <span class="Constant">&quot;init-fake-keyboard&quot;</span><span class="Delimiter">;</span>
  assert<span class="Delimiter">(</span>curr<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>empty<span class="Delimiter">());</span>
  curr<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>reagent<span class="Delimiter">(</span><span class="Constant">&quot;keyboard:address&quot;</span><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>set_value<span class="Delimiter">(</span>KEYBOARD<span class="Delimiter">);</span>
  assert<span class="Delimiter">(</span>curr<span class="Delimiter">.</span>products<span class="Delimiter">.</span>empty<span class="Delimiter">());</span>
  curr<span class="Delimiter">.</span>products<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>reagent<span class="Delimiter">(</span><span class="Constant">&quot;keyboard:address&quot;</span><span class="Delimiter">));</span>
  curr<span class="Delimiter">.</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>set_value<span class="Delimiter">(</span>KEYBOARD<span class="Delimiter">);</span>
<span class="Delimiter">}</span>

<span class="Comment">//: Since we don't yet have a clean way to represent characters like backspace</span>
<span class="Comment">//: in literal strings we can't easily pretend they were typed into the fake</span>
<span class="Comment">//: keyboard. So we'll use special unicode characters in the literal and then</span>
<span class="Comment">//: manually replace them with backspace.</span>
<span class="Delimiter">:(before &quot;End Primitive Recipe Declarations&quot;)</span>
REPLACE_IN_KEYBOARD<span class="Delimiter">,</span>
<span class="Delimiter">:(before &quot;End Primitive Recipe Numbers&quot;)</span>
Recipe_number[<span class="Constant">&quot;replace-in-keyboard&quot;</span>] = REPLACE_IN_KEYBOARD<span class="Delimiter">;</span>
<span class="Delimiter">:(before &quot;End Primitive Recipe Implementations&quot;)</span>
case REPLACE_IN_KEYBOARD: <span class="Delimiter">{</span>
  long long int size = Memory[KEYBOARD]<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>
  assert<span class="Delimiter">(</span>scalar<span class="Delimiter">(</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">1</span><span class="Delimiter">)));</span>
  for <span class="Delimiter">(</span>long long int curr = KEYBOARD+<span class="Constant">1</span><span class="Delimiter">;</span> curr &lt;= KEYBOARD+size<span class="Delimiter">;</span> ++curr<span class="Delimiter">)</span> <span class="Delimiter">{</span>
    if <span class="Delimiter">(</span>Memory[curr] == 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="Delimiter">{</span>
<span class="CommentedCode">//?       cerr &lt;&lt; &quot;replacing\n&quot;; //? 1</span>
      Memory[curr] = ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">1</span><span class="Delimiter">).</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">);</span>
    <span class="Delimiter">}</span>
  <span class="Delimiter">}</span>
  <span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
</pre>
</body>
</html>
<!-- vim: set foldmethod=manual : -->