about summary refs log blame commit diff stats
path: root/023boolean.cc
blob: 6d13325f66a249f5c163e8de7b1d1b5c3d68178b (plain) (tree)
795f5244 ^<
<!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 - http-server.mu</title>
<meta name="Generator" content="Vim/7.4">
<meta name="plugin-version" content="vim7.4_v2">
<meta name="syntax" content="none">
<meta name="settings" content="number_lines,use_css,pre_wrap,no_foldcolumn,expand_tabs,line_ids,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; }
a { text-decoration: none; }
a:hover { text-decoration: underline; }
* { font-size: 12pt; font-size: 1em; }
.muRecipe { color: #ff8700; }
.Special { color: #c00000; }
.Comment { color: #9090ff; }
.Constant { color: #00a0a0; }
.LineNr { color: #444444; }
.muControl { color: #c0a020; }
-->
</style>

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

/* function to open any folds containing a jumped-to line before jumping to it */
function JumpToLine()
{
  var lineNum;
  lineNum = window.location.hash;
  lineNum = lineNum.substr(1); /* strip off '#' */

  if (lineNum.indexOf('L') == -1) {
    lineNum = 'L'+lineNum;
  }
  lineElem = document.getElementById(lineNum);
  /* Always jump to new location even if the line was hidden inside a fold, or
   * we corrected the raw number to a line ID.
   */
  if (lineElem) {
    lineElem.scrollIntoView(true);
  }
  return true;
}
if ('onhashchange' in window) {
  window.onhashchange = JumpToLine;
}

-->
</script>
</head>
<body onload='JumpToLine();'>
<pre id='vimCodeElement'>
<span id="L1" class="LineNr"> 1 </span><span class="Comment"># example program: a single-request HTTP server</span>
<span id="L2" class="LineNr"> 2 </span><span class="Comment">#   listen for connections from clients on a server socket</span>
<span id="L3" class="LineNr"> 3 </span><span class="Comment">#   when a connection occurs, transfer it to a session socket</span>
<span id="L4" class="LineNr"> 4 </span><span class="Comment">#   read from it using channels</span>
<span id="L5" class="LineNr"> 5 </span><span class="Comment">#   write to it directly</span>
<span id="L6" class="LineNr"> 6 </span><span class="Comment">#</span>
<span id="L7" class="LineNr"> 7 </span><span class="Comment"># After running it, navigate to localhost:8080. Your browser should display</span>
<span id="L8" class="LineNr"> 8 </span><span class="Comment"># &quot;SUCCESS!&quot; and the server will terminate after one connection.</span>
<span id="L9" class="LineNr"> 9 </span>
<span id="L10" class="LineNr">10 </span><span class="muRecipe">def</span> <a href='http-server.mu.html#L10'>main</a> [
<span id="L11" class="LineNr">11 </span>  <span class="Constant">local-scope</span>
<span id="L12" class="LineNr">12 </span>  socket:num <span class="Special">&lt;-</span> $open-server-socket <span class="Constant">8080/port</span>
<span id="L13" class="LineNr">13 </span>  $print <span class="Constant">[Mu socket creation returned ]</span>, socket, <span class="Constant">10/newline</span>
<span id="L14" class="LineNr">14 </span>  <span class="muControl">return-unless</span> socket
<span id="L15" class="LineNr">15 </span>  session:num <span class="Special">&lt;-</span> $accept socket
<span id="L16" class="LineNr">16 </span>  contents:&amp;:source:char, sink:&amp;:sink:char <span class="Special">&lt;-</span> <a href='075channel.mu.html#L51'>new-channel</a><span class="Constant"> 30</span>
<span id="L17" class="LineNr">17 </span>  sink <span class="Special">&lt;-</span> start-running <a href='092socket.mu.html#L104'>receive-from-socket</a> session, sink
<span id="L18" class="LineNr">18 </span>  query:text <span class="Special">&lt;-</span> <a href='075channel.mu.html#L487'>drain</a> contents
<span id="L19" class="LineNr">19 </span>  $print <span class="Constant">[Done reading from socket.]</span>, <span class="Constant">10/newline</span>
<span id="L20" class="LineNr">20 </span>  <a href='092socket.mu.html#L132'>write-to-socket</a> session, <span class="Constant">[HTTP/1.0 200 OK</span>
<span id="L21" class="LineNr">21 </span><span class="Constant">Content-type: text/plain</span>
<span id="L22" class="LineNr">22 </span>
<span id="L23" class="LineNr">23 </span><span class="Constant">SUCCESS!</span>
<span id="L24" class="LineNr">24 </span><span class="Constant">]</span>
<span id="L25" class="LineNr">25 </span>  $print <span class="Constant">10/newline</span>, <span class="Constant">[Wrote to and closing socket...]</span>, <span class="Constant">10/newline</span>
<span id="L26" class="LineNr">26 </span>  session <span class="Special">&lt;-</span> $close-socket session
<span id="L27" class="LineNr">27 </span>  socket <span class="Special">&lt;-</span> $close-socket socket
<span id="L28" class="LineNr">28 </span>]
</pre>
</body>
</html>
<!-- vim: set foldmethod=manual : -->
mp;id=94fa5c95ad9c8beead183bb7c4b88c7c2c7ca6ec'>^
051c4738 ^
1ead3562 ^
dd660682 ^
0487a30e ^


1848b18f ^

d7e11237 ^
795f5244 ^
e236973b ^
4e49b29e ^
67a180e3 ^
2b250717 ^
4e49b29e ^

6c96a437 ^
23d3a022 ^

4e49b29e ^


6c96a437 ^
bff0fa45 ^

9dcbec39 ^
bff0fa45 ^


4e49b29e ^

d7e11237 ^

e4630643 ^
6c96a437 ^
23d3a022 ^
0487a30e ^
d7e11237 ^


88be3dbc ^
1ead3562 ^
dd660682 ^
d52406cc ^
d7e11237 ^
57699011 ^
0487a30e ^

1ead3562 ^
dd660682 ^
0487a30e ^



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
                      
 
                                             
    
                                        
                                
                                       
           
                                                      
                                                
                                                                                                                                                         


                                       
                                
                                                                                                                                 


                                                                                                        
                                                                                                                                              

          

        

                                                
                     
                                               
                                                         
                     
                                   

        





                                                                             
 
               
          

                      
                              
 
                             
 
                 
          
                          



                             
          
                                 


                             
                          
          
                                


                             

                                             
                                        
                              
                                       
          
                                                      
                                                
                                                                                                                                                         


                                       
                                
                                                                                                                                


                                                                                                        
                                                                                                                                             

          

        

                                                
                      
                                               
                                                         
                     
                                   


        
              
          

                      
                             
 
                             
 
                
          
                           



                             
          
                                  


                             
                         
          
                                 


                             

                                             
                                        
                                
                                       
           
                                                      
                                                                                                        

          
                                                      

                                                                                                                                                                                    


                                       
                                                   

                                                
                                                                                                                                                


                                       

        

                                                
                                     
                                                 
                                                                 
   


        
               
          
                     
                      
 
                             

                        
          
                                                 



                             
//: Boolean primitives

:(before "End Primitive Recipe Declarations")
AND,
:(before "End Primitive Recipe Numbers")
put(Recipe_ordinal, "and", AND);
:(before "End Primitive Recipe Checks")
case AND: {
  for (int i = 0;  i < SIZE(inst.ingredients);  ++i) {
    if (!is_mu_scalar(inst.ingredients.at(i))) {
      raise << maybe(get(Recipe, r).name) << "'and' requires boolean ingredients, but got '" << inst.ingredients.at(i).original_string << "'\n" << end();
      goto finish_checking_instruction;
    }
  }
  if (SIZE(inst.products) > 1) {
    raise << maybe(get(Recipe, r).name) << "'and' yields exactly one product in '" << to_original_string(inst) << "'\n" << end();
    break;
  }
  if (!inst.products.empty() && !is_dummy(inst.products.at(0)) && !is_mu_boolean(inst.products.at(0))) {
    raise << maybe(get(Recipe, r).name) << "'and' should yield a boolean, but got '" << inst.products.at(0).original_string << "'\n" << end();
    break;
  }
  break;
}
:(before "End Primitive Recipe Implementations")
case AND: {
  bool result = true;
  for (int i = 0;  i < SIZE(ingredients);  ++i)
    result = result && scalar_ingredient(ingredients, i);
  products.resize(1);
  products.at(0).push_back(result);
  break;
}
:(code)
double scalar_ingredient(const vector<vector<double> >& ingredients, int i) {
  if (is_mu_address(current_instruction().ingredients.at(i)))
    return ingredients.at(i).at(/*skip alloc id*/1);
  return ingredients.at(i).at(0);
}

:(scenario and)
def main [
  1:bool <- copy true
  2:bool <- copy false
  3:bool <- and 1:bool, 2:bool
]
+mem: storing 0 in location 3

:(scenario and_2)
def main [
  1:bool <- and true, true
]
+mem: storing 1 in location 1

:(scenario and_multiple)
def main [
  1:bool <- and true, true, false
]
+mem: storing 0 in location 1

:(scenario and_multiple_2)
def main [
  1:bool <- and true, true, true
]
+mem: storing 1 in location 1

:(before "End Primitive Recipe Declarations")
OR,
:(before "End Primitive Recipe Numbers")
put(Recipe_ordinal, "or", OR);
:(before "End Primitive Recipe Checks")
case OR: {
  for (int i = 0;  i < SIZE(inst.ingredients);  ++i) {
    if (!is_mu_scalar(inst.ingredients.at(i))) {
      raise << maybe(get(Recipe, r).name) << "'and' requires boolean ingredients, but got '" << inst.ingredients.at(i).original_string << "'\n" << end();
      goto finish_checking_instruction;
    }
  }
  if (SIZE(inst.products) > 1) {
    raise << maybe(get(Recipe, r).name) << "'or' yields exactly one product in '" << to_original_string(inst) << "'\n" << end();
    break;
  }
  if (!inst.products.empty() && !is_dummy(inst.products.at(0)) && !is_mu_boolean(inst.products.at(0))) {
    raise << maybe(get(Recipe, r).name) << "'or' should yield a boolean, but got '" << inst.products.at(0).original_string << "'\n" << end();
    break;
  }
  break;
}
:(before "End Primitive Recipe Implementations")
case OR: {
  bool result = false;
  for (int i = 0;  i < SIZE(ingredients);  ++i)
    result = result || scalar_ingredient(ingredients, i);
  products.resize(1);
  products.at(0).push_back(result);
  break;
}

:(scenario or)
def main [
  1:bool <- copy true
  2:bool <- copy false
  3:bool <- or 1:bool, 2:bool
]
+mem: storing 1 in location 3

:(scenario or_2)
def main [
  1:bool <- or false, false
]
+mem: storing 0 in location 1

:(scenario or_multiple)
def main [
  1:bool <- or false, false, false
]
+mem: storing 0 in location 1

:(scenario or_multiple_2)
def main [
  1:bool <- or false, false, true
]
+mem: storing 1 in location 1

:(before "End Primitive Recipe Declarations")
NOT,
:(before "End Primitive Recipe Numbers")
put(Recipe_ordinal, "not", NOT);
:(before "End Primitive Recipe Checks")
case NOT: {
  if (SIZE(inst.products) != SIZE(inst.ingredients)) {
    raise << "ingredients and products should match in '" << to_original_string(inst) << "'\n" << end();
    break;
  }
  for (int i = 0;  i < SIZE(inst.ingredients);  ++i) {
    if (!is_mu_scalar(inst.ingredients.at(i)) && !is_mu_address(inst.ingredients.at(i))) {
      raise << maybe(get(Recipe, r).name) << "'not' requires ingredients that can be interpreted as boolean, but got '" << inst.ingredients.at(i).original_string << "'\n" << end();
      goto finish_checking_instruction;
    }
  }
  for (int i = 0;  i < SIZE(inst.products);  ++i) {
    if (is_dummy(inst.products.at(i))) continue;
    if (!is_mu_boolean(inst.products.at(i))) {
      raise << maybe(get(Recipe, r).name) << "'not' should yield a boolean, but got '" << inst.products.at(i).original_string << "'\n" << end();
      goto finish_checking_instruction;
    }
  }
  break;
}
:(before "End Primitive Recipe Implementations")
case NOT: {
  products.resize(SIZE(ingredients));
  for (int i = 0;  i < SIZE(ingredients);  ++i) {
    products.at(i).push_back(!scalar_ingredient(ingredients, i));
  }
  break;
}

:(scenario not)
def main [
  1:bool <- copy true
  2:bool <- not 1:bool
]
+mem: storing 0 in location 2

:(scenario not_multiple)
def main [
  1:bool, 2:bool, 3:bool <- not true, false, true
]
+mem: storing 0 in location 1
+mem: storing 1 in location 2
+mem: storing 0 in location 3