about summary refs log blame commit diff stats
path: root/html/017parse_tree.cc.html
blob: 015ffe603067c824211c1913d254fbf21af40f8c (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 - 017parse_tree.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; }
.traceContains { 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">// So far instructions can only contain linear lists of properties. Now we add</span>
<span class="Comment">// support for more complex trees of properties in dilated reagents. This will</span>
<span class="Comment">// come in handy later for expressing complex types, like &quot;a dictionary from</span>
<span class="Comment">// (address to array of charaters) to (list of numbers)&quot;.</span>

<span class="Delimiter">:(scenarios load)</span>
<span class="Delimiter">:(scenario dilated_reagent_with_nested_brackets)</span>
def main [
  <span class="Delimiter">{</span><span class="Constant">1</span>: number<span class="Delimiter">,</span> foo: <span class="Delimiter">(</span>bar <span class="Delimiter">(</span>baz quux<span class="Delimiter">))}</span><span class="Special"> &lt;- </span>copy <span class="Constant">34</span>
]
<span class="traceContains">+parse:   product: {1: &quot;number&quot;, &quot;foo&quot;: (&quot;bar&quot; (&quot;baz&quot; &quot;quux&quot;))}</span>

<span class="Delimiter">:(before &quot;End Parsing Reagent Property(value)&quot;)</span>
value = parse_string_tree<span class="Delimiter">(</span>value<span class="Delimiter">);</span>
<span class="Delimiter">:(before &quot;End Parsing Reagent Type Property(value)&quot;)</span>
value = parse_string_tree<span class="Delimiter">(</span>value<span class="Delimiter">);</span>

<span class="Delimiter">:(code)</span>
string_tree* parse_string_tree<span class="Delimiter">(</span>string_tree* s<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  assert<span class="Delimiter">(</span>!s<span class="Delimiter">-&gt;</span>left &amp;&amp; !s<span class="Delimiter">-&gt;</span>right<span class="Delimiter">);</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>s<span class="Delimiter">-&gt;</span>value<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">)</span> != <span class="Constant">'('</span><span class="Delimiter">)</span> <span class="Identifier">return</span> s<span class="Delimiter">;</span>
  string_tree* result = parse_string_tree<span class="Delimiter">(</span>s<span class="Delimiter">-&gt;</span>value<span class="Delimiter">);</span>
  <span class="Normal">delete</span> s<span class="Delimiter">;</span>
  <span class="Identifier">return</span> result<span class="Delimiter">;</span>
<span class="Delimiter">}</span>

string_tree* parse_string_tree<span class="Delimiter">(</span><span class="Normal">const</span> string&amp; s<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  istringstream in<span class="Delimiter">(</span>s<span class="Delimiter">);</span>
  in &gt;&gt; std::noskipws<span class="Delimiter">;</span>
  <span class="Identifier">return</span> parse_string_tree<span class="Delimiter">(</span>in<span class="Delimiter">);</span>
<span class="Delimiter">}</span>

string_tree* parse_string_tree<span class="Delimiter">(</span>istream&amp; in<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  skip_whitespace_but_not_newline<span class="Delimiter">(</span>in<span class="Delimiter">);</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>!has_data<span class="Delimiter">(</span>in<span class="Delimiter">))</span> <span class="Identifier">return</span> <span class="Constant">NULL</span><span class="Delimiter">;</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>in<span class="Delimiter">.</span>peek<span class="Delimiter">()</span> == <span class="Constant">')'</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
    in<span class="Delimiter">.</span>get<span class="Delimiter">();</span>
    <span class="Identifier">return</span> <span class="Constant">NULL</span><span class="Delimiter">;</span>
  <span class="Delimiter">}</span>
  <span class="Normal">if</span> <span class="Delimiter">(</span>in<span class="Delimiter">.</span>peek<span class="Delimiter">()</span> != <span class="Constant">'('</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
    string_tree* result = <span class="Normal">new</span> string_tree<span class="Delimiter">(</span>next_word<span class="Delimiter">(</span>in<span class="Delimiter">));</span>
    <span class="Identifier">return</span> result<span class="Delimiter">;</span>
  <span class="Delimiter">}</span>
  in<span class="Delimiter">.</span>get<span class="Delimiter">();</span>  <span class="Comment">// skip '('</span>
  string_tree* result = <span class="Constant">NULL</span><span class="Delimiter">;</span>
  string_tree** curr = &amp;result<span class="Delimiter">;</span>
  <span class="Normal">while</span> <span class="Delimiter">(</span>in<span class="Delimiter">.</span>peek<span class="Delimiter">()</span> != <span class="Constant">')'</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
    assert<span class="Delimiter">(</span>has_data<span class="Delimiter">(</span>in<span class="Delimiter">));</span>
    *curr = <span class="Normal">new</span> string_tree<span class="Delimiter">(</span><span class="Constant">&quot;&quot;</span><span class="Delimiter">);</span>
    skip_whitespace_but_not_newline<span class="Delimiter">(</span>in<span class="Delimiter">);</span>
    <span class="Normal">if</span> <span class="Delimiter">(</span>in<span class="Delimiter">.</span>peek<span class="Delimiter">()</span> == <span class="Constant">'('</span><span class="Delimiter">)</span>
      <span class="Delimiter">(</span>*curr<span class="Delimiter">)-&gt;</span>left = parse_string_tree<span class="Delimiter">(</span>in<span class="Delimiter">);</span>
    <span class="Normal">else</span>
      <span class="Delimiter">(</span>*curr<span class="Delimiter">)-&gt;</span>value = next_word<span class="Delimiter">(</span>in<span class="Delimiter">);</span>
    curr = &amp;<span class="Delimiter">(</span>*curr<span class="Delimiter">)-&gt;</span>right<span class="Delimiter">;</span>
  <span class="Delimiter">}</span>
  in<span class="Delimiter">.</span>get<span class="Delimiter">();</span>  <span class="Comment">// skip ')'</span>
  <span class="Identifier">return</span> result<span class="Delimiter">;</span>
<span class="Delimiter">}</span>

<span class="Delimiter">:(scenario dilated_reagent_with_type_tree)</span>
<span class="Special">% Hide_errors = true;  // 'map' isn't defined yet</span>
def main [
  <span class="Delimiter">{</span><span class="Constant">1</span>: <span class="Delimiter">(</span>foo <span class="Delimiter">(</span>address array character<span class="Delimiter">)</span> <span class="Delimiter">(</span>bar number<span class="Delimiter">))}</span><span class="Special"> &lt;- </span>copy <span class="Constant">34</span>
]
<span class="Comment"># just to avoid errors</span>
container foo [
]
container bar [
]
<span class="traceContains">+parse:   product: {1: (&quot;foo&quot; (&quot;address&quot; &quot;array&quot; &quot;character&quot;) (&quot;bar&quot; &quot;number&quot;))}</span>
</pre>
</body>
</html>
<!-- vim: set foldmethod=manual : -->