about summary refs log blame commit diff stats
path: root/LICENSE
blob: 67e22c37c3a994e66539612cb02de910eca91baa (plain) (tree)
1
2
3
4


                                                           
                                                                       

















                                                                           
MIT/X Consortium License

(C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
(C)opyright MMVI Sander van Dijk <a dot h dot vandijk at gmail dot com>

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the 
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in 
all copies or substantial portions of the Software. 

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL 
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
DEALINGS IN THE SOFTWARE.
ram <vc@akkartik.com> 2015-05-25 22:27:19 -0700 committer Kartik K. Agaram <vc@akkartik.com> 2015-05-25 22:27:19 -0700 1459' href='/akkartik/mu/commit/html/014literal_noninteger.cc.html?h=main&id=c5ffb6e1cc9c5ff880d037c53b8ebc8562be0008'>c5ffb6e1 ^
c5ffb6e1 ^


4690ce81 ^
c5ffb6e1 ^

a654e4ec ^
c5ffb6e1 ^
9570363a ^
4690ce81 ^
9570363a ^
4690ce81 ^
9570363a ^


c5ffb6e1 ^

4690ce81 ^
dcc060c7 ^



c5ffb6e1 ^

4690ce81 ^

c5ffb6e1 ^
4690ce81 ^
c5ffb6e1 ^


76755b28 ^
4690ce81 ^
76755b28 ^






dcc060c7 ^




76755b28 ^
c5ffb6e1 ^


a654e4ec ^
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



                                                                                          
                                            

                                                
                                  

                                                                                         

                       
                                                                                                 
                                                                                            

                                      
                             
                                  
                            



                                                                           

        





                               

       
                         
                                                              


                                                             
          

                                                                                                                      
                                                                                                         
 
                                                                             
                                                                                                                                                                               
                                          
                                                                                                                                                                                                                                                      


                                                                                                                       

                                      
                                                                                                                                                                                              



                                                                                                                                                                                                                                                                                                                                                                                                          

                                

                                                                                                                                                     
                                                                                                 
                                                                                                                                                                                                      


                                                                                                                   
 
                                                                                                                     






                                                                                                                                                                     




                                                                                                                                                                     
                                


       
                                     
<!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 - 015literal_noninteger.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; }
.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">//: Support literal non-integers.</span>

<span class="Delimiter">:(scenarios load)</span>
<span class="Delimiter">:(scenario noninteger_literal)</span>
def main [
  <span class="Constant">1</span>:number<span class="Special"> &lt;- </span>copy <span class="Constant">3.14159</span>
]
<span class="traceContains">+parse:   ingredient: {3.14159: &quot;literal-fractional-number&quot;}</span>

<span class="Delimiter">:(after &quot;Parsing reagent(string s)&quot;)</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>is_noninteger<span class="Delimiter">(</span>s<span class="Delimiter">))</span> <span class="Delimiter">{</span>
  name = s<span class="Delimiter">;</span>
  type = <span class="Normal">new</span> type_tree<span class="Delimiter">(</span><span class="Constant">&quot;literal-fractional-number&quot;</span><span class="Delimiter">,</span> <span class="Constant">0</span><span class="Delimiter">);</span>
  set_value<span class="Delimiter">(</span>to_double<span class="Delimiter">(</span>s<span class="Delimiter">));</span>
  <span class="Identifier">return</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>

<span class="Delimiter">:(code)</span>
<span class="Normal">bool</span> is_noninteger<span class="Delimiter">(</span><span class="Normal">const</span> string&amp; s<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  <span class="Identifier">return</span> s<span class="Delimiter">.</span>find_first_not_of<span class="Delimiter">(</span><span class="Constant">&quot;0123456789-.&quot;</span><span class="Delimiter">)</span> == string::npos  <span class="Comment">// no other characters</span>
      &amp;&amp; s<span class="Delimiter">.</span>find_first_of<span class="Delimiter">(</span><span class="Constant">&quot;0123456789&quot;</span><span class="Delimiter">)</span> != string::npos  <span class="Comment">// at least one digit</span>
      &amp;&amp; s<span class="Delimiter">.</span>find<span class="Delimiter">(</span><span class="Constant">'-'</span><span class="Delimiter">,</span> <span class="Constant">1</span><span class="Delimiter">)</span> == string::npos  <span class="Comment">// '-' only at first position</span>
      &amp;&amp; std::count<span class="Delimiter">(</span>s<span class="Delimiter">.</span>begin<span class="Delimiter">(),</span> s<span class="Delimiter">.</span>end<span class="Delimiter">(),</span> <span class="Constant">'.'</span><span class="Delimiter">)</span> == <span class="Constant">1</span><span class="Delimiter">;</span>  <span class="Comment">// exactly one decimal point</span>
<span class="Delimiter">}</span>

<span class="Normal">double</span> to_double<span class="Delimiter">(</span>string n<span class="Delimiter">)</span> <span class="Delimiter">{</span>
  <span class="Normal">char</span>* end = <span class="Constant">NULL</span><span class="Delimiter">;</span>
  <span class="Comment">// safe because string.c_str() is guaranteed to be null-terminated</span>
  <span class="Normal">double</span> result = strtod<span class="Delimiter">(</span>n<span class="Delimiter">.</span>c_str<span class="Delimiter">(),</span> &amp;end<span class="Delimiter">);</span>
  assert<span class="Delimiter">(</span>*end == <span class="cSpecial">'\0'</span><span class="Delimiter">);</span>
  <span class="Identifier">return</span> result<span class="Delimiter">;</span>
<span class="Delimiter">}</span>

<span class="Normal">void</span> test_is_noninteger<span class="Delimiter">()</span> <span class="Delimiter">{</span>
  CHECK<span class="Delimiter">(</span>!is_noninteger<span class="Delimiter">(</span><span class="Constant">&quot;1234&quot;</span><span class="Delimiter">));</span>
  CHECK<span class="Delimiter">(</span>!is_noninteger<span class="Delimiter">(</span><span class="Constant">&quot;1a2&quot;</span><span class="Delimiter">));</span>
  CHECK<span class="Delimiter">(</span>is_noninteger<span class="Delimiter">(</span><span class="Constant">&quot;234.0&quot;</span><span class="Delimiter">));</span>
  CHECK<span class="Delimiter">(</span>!is_noninteger<span class="Delimiter">(</span><span class="Constant">&quot;...&quot;</span><span class="Delimiter">));</span>
  CHECK<span class="Delimiter">(</span>!is_noninteger<span class="Delimiter">(</span><span class="Constant">&quot;.&quot;</span><span class="Delimiter">));</span>
  CHECK<span class="Delimiter">(</span>is_noninteger<span class="Delimiter">(</span><span class="Constant">&quot;2.&quot;</span><span class="Delimiter">));</span>
  CHECK<span class="Delimiter">(</span>is_noninteger<span class="Delimiter">(</span><span class="Constant">&quot;.2&quot;</span><span class="Delimiter">));</span>
  CHECK<span class="Delimiter">(</span>is_noninteger<span class="Delimiter">(</span><span class="Constant">&quot;-.2&quot;</span><span class="Delimiter">));</span>
  CHECK<span class="Delimiter">(</span>is_noninteger<span class="Delimiter">(</span><span class="Constant">&quot;-2.&quot;</span><span class="Delimiter">));</span>
  CHECK<span class="Delimiter">(</span>!is_noninteger<span class="Delimiter">(</span><span class="Constant">&quot;--.2&quot;</span><span class="Delimiter">));</span>
  CHECK<span class="Delimiter">(</span>!is_noninteger<span class="Delimiter">(</span><span class="Constant">&quot;.-2&quot;</span><span class="Delimiter">));</span>
  CHECK<span class="Delimiter">(</span>!is_noninteger<span class="Delimiter">(</span><span class="Constant">&quot;..2&quot;</span><span class="Delimiter">));</span>
<span class="Delimiter">}</span>
</pre>
</body>
</html>
<!-- vim: set foldmethod=manual : -->