about summary refs log blame commit diff stats
path: root/html/076scenario_keyboard_test.mu.html
blob: ec148812d5349142a977927ed6d1948773bf601a (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 - 076scenario_keyboard_test.mu</title>
<meta name="Generator" content="Vim/7.4">
<meta name="plugin-version" content="vim7.4_v1">
<meta name="syntax" content="none">
<meta name="settings" content="use_css,pre_wrap,no_foldcolumn,expand_tabs,prevent_copy=">
<meta name="colorscheme" content="minimal">
<style type="text/css">
<!--
pre { white-space: pre-wrap; font-family: monospace; color: #eeeeee; background-color: #080808; }
body { font-family: monospace; color: #eeeeee; background-color: #080808; }
* { font-size: 1.05em; }
.muScenario { color: #00af00; }
.Comment { color: #9090ff; }
.Constant { color: #00a0a0; }
.Special { color: #ff6060; }
-->
</style>

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

-->
</script>
</head>
<body>
<pre id='vimCodeElement'>
<span class="Comment"># To check our support for keyboards in scenarios, rewrite tests from</span>
<span class="Comment"># scenario_keyboard.mu</span>
<span class="Comment"># Tests for keyboard interface.</span>

<span class="muScenario">scenario</span> read-key-in-mu [
  assume-keyboard <span class="Constant">[abc]</span>
  run [
    1:character, 2:boolean, keyboard:address<span class="Special"> &lt;- </span>read-key keyboard:address
    3:character, 4:boolean, keyboard:address<span class="Special"> &lt;- </span>read-key keyboard:address
    5:character, 6:boolean, keyboard:address<span class="Special"> &lt;- </span>read-key keyboard:address
    7:character, 8:boolean, keyboard:address<span class="Special"> &lt;- </span>read-key keyboard:address
  ]
  memory-should-contain [
    1<span class="Special"> &lt;- </span>97  <span class="Comment"># 'a'</span>
    2<span class="Special"> &lt;- </span>1
    3<span class="Special"> &lt;- </span>98  <span class="Comment"># 'b'</span>
    4<span class="Special"> &lt;- </span>1
    5<span class="Special"> &lt;- </span>99  <span class="Comment"># 'c'</span>
    6<span class="Special"> &lt;- </span>1
    7<span class="Special"> &lt;- </span>0  <span class="Comment"># eof</span>
    8<span class="Special"> &lt;- </span>1
  ]
]
</pre>
</body>
</html>
<!-- vim: set foldmethod=manual : -->
ent.Multiline */ .highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */ .highlight .cpf { color: #888888 } /* Comment.PreprocFile */ .highlight .c1 { color: #888888 } /* Comment.Single */ .highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */ .highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */ .highlight .ge { font-style: italic } /* Generic.Emph */ .highlight .ges { font-weight: bold; font-style: italic } /* Generic.EmphStrong */ .highlight .gr { color: #aa0000 } /* Generic.Error */ .highlight .gh { color: #333333 } /* Generic.Heading */ .highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */ .highlight .go { color: #888888 } /* Generic.Output */ .highlight .gp { color: #555555 } /* Generic.Prompt */ .highlight .gs { font-weight: bold } /* Generic.Strong */ .highlight .gu { color: #666666 } /* Generic.Subheading */ .highlight .gt { color: #aa0000 } /* Generic.Traceback */ .highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */ .highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */ .highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */ .highlight .kp { color: #008800 } /* Keyword.Pseudo */ .highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */ .highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */ .highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */ .highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */ .highlight .na { color: #336699 } /* Name.Attribute */ .highlight .nb { color: #003388 } /* Name.Builtin */ .highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */ .highlight .no { color: #003366; font-weight: bold } /* Name.Constant */ .highlight .nd { color: #555555 } /* Name.Decorator */ .highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */ .highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */ .highlight .nl { color: #336699; font-style: italic } /* Name.Label */ .highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */ .highlight .py { color: #336699; font-weight: bold } /* Name.Property */ .highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */ .highlight .nv { color: #336699 } /* Name.Variable */ .highlight .ow { color: #008800 } /* Operator.Word */ .highlight .w { color: #bbbbbb } /* Text.Whitespace */ .highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */ .highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */ .highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */ .highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */ .highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */ .highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */ .highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */ .highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
//: Reclaiming memory when it's no longer used.

:(scenario new_reclaim)
def main [
  10:&:num <- new number:type
  20:num <- deaddress 10:&:num
  abandon 10:&:num
  30:&:num <- new number:type  # must be same size as abandoned memory to reuse
  40:num <- deaddress 30:&:num
  50:bool <- equal 20:num, 40:num
]
# both allocations should have returned the same address
+mem: storing 1 in location 50

//: When abandoning addresses we'll save them to a 'free list', segregated by size.

//: Before, suppose variable V contains address A which points to payload P:
//:   location V contains an alloc-id N
//:   location V+1 contains A
//:   location A contains alloc-id N
//:   location A+1 onwards contains P
//: Additionally, suppose the head of the free list is initially F.
//: After abandoning:
//:   location V contains invalid alloc-id -1
//:   location V+1 contains 0
//:   location A contains invalid alloc-id N
//:   location A+1 contains the previous head of free-list F

:(before "End routine Fields")
map<int, int> free_list;

:(before "End Primitive Recipe Declarations")
ABANDON,
:(before "End Primitive Recipe Numbers")
put(Recipe_ordinal, "abandon", ABANDON);
:(before "End Primitive Recipe Checks")
case ABANDON: {
  if (!inst.products.empty()) {
    raise << maybe(get(Recipe, r).name) << "'abandon' shouldn't write to any products in '" << to_original_string(inst) << "'\n" << end();
    break;
  }
  for (int i = 0;  i < SIZE(inst.ingredients);  ++i) {
    if (!is_mu_address(inst.ingredients.at(i)))
      raise << maybe(get(Recipe, r).name) << "ingredients of 'abandon' should be addresses, but ingredient " << i << " is '" << to_string(inst.ingredients.at(i)) << '\n' << end();
    break;
  }
  break;
}
:(before "End Primitive Recipe Implementations")
case ABANDON: {
  for (int i = 0;  i < SIZE(current_instruction().ingredients);  ++i) {
    reagent/*copy*/ ingredient = current_instruction().ingredients.at(i);
    canonize(ingredient);
    abandon(get_or_insert(Memory, ingredient.value+/*skip alloc id*/1), payload_size(ingredient));
//?     cerr << "clear after abandon: " << ingredient.value << '\n';
    put(Memory, /*alloc id*/ingredient.value, /*invalid*/-1);
    put(Memory, /*address*/ingredient.value+1, 0);
  }
  break;
}

:(code)
void abandon(int address, int payload_size) {
  put(Memory, address, /*invalid alloc-id*/-1);
//?   cerr << "abandon: " << address << '\n';
  // clear rest of payload
  for (int curr = address+1;  curr < address+payload_size;  ++curr)
    put(Memory, curr, 0);
  // append existing free list to address
  trace("abandon") << "saving " << address << " in free-list of size " << payload_size << end();
  put(Memory, address+/*skip invalid alloc-id*/1, get_or_insert(Current_routine->free_list, payload_size));
  put(Current_routine->free_list, payload_size, address);
}

int payload_size(reagent/*copy*/ x) {
  x.properties.push_back(pair<string, string_tree*>("lookup", NULL));
  lookup_memory_core(x, /*check_for_null*/false);
  return size_of(x)+/*alloc id*/1;
}

:(after "Allocate Special-cases")
if (get_or_insert(Current_routine->free_list, size)) {
  trace("abandon") << "picking up space from free-list of size " << size << end();
  int result = get_or_insert(Current_routine->free_list, size);
  trace("mem") << "new alloc from free list: " << result << end();
  put(Current_routine->free_list, size, get_or_insert(Memory, result+/*skip alloc id*/1));
  // clear 'deleted' tag
  put(Memory, result, 0);
  // clear next pointer
  put(Memory, result+/*skip alloc id*/1, 0);
  for (int curr = result;  curr < result+size;  ++curr) {
    if (get_or_insert(Memory, curr) != 0) {
      raise << maybe(current_recipe_name()) << "memory in free list was not zeroed out: " << curr << '/' << result << "; somebody wrote to us after free!!!\n" << end();
      break;  // always fatal
    }
  }
  return result;
}

:(scenario new_differing_size_no_reclaim)
def main [
  1:&:num <- new number:type
  2:num <- deaddress 1:&:num
  abandon 1:&:num
  3:&:@:num <- new number:type, 2  # different size
  4:num <- deaddress 3:&:@:num
  5:bool <- equal 2:num, 4:num
]
# no reuse
+mem: storing 0 in location 5

:(scenario new_reclaim_array)
def main [
  10:&:@:num <- new number:type, 2
  20:num <- deaddress 10:&:@:num
  abandon 10:&:@:num
  30:&:@:num <- new number:type, 2  # same size
  40:num <- deaddress 30:&:@:num
  50:bool <- equal 20:num, 40:num
]
# both calls to new returned identical addresses
+mem: storing 1 in location 50

:(scenario lookup_of_abandoned_address_raises_error)
% Hide_errors = true;
def main [
  1:&:num <- new num:type
  3:&:num <- copy 1:&:num
  abandon 1:&:num
  5:num/raw <- copy *3:&:num
]
+error: main: address is already abandoned in '5:num/raw <- copy *3:&:num'