diff options
author | Kartik K. Agaram <vc@akkartik.com> | 2016-11-13 09:37:50 -0800 |
---|---|---|
committer | Kartik K. Agaram <vc@akkartik.com> | 2016-11-13 09:37:50 -0800 |
commit | 20a7d37f74625962c946f027074031d7cd2cbbcb (patch) | |
tree | aefb3ffc7cf040c1e115a4ab58f95e115b50a22b /html/061text.mu.html | |
parent | a513c19fd61de8c4b253a307ccfaea628b5960ed (diff) | |
download | mu-20a7d37f74625962c946f027074031d7cd2cbbcb.tar.gz |
3673
Diffstat (limited to 'html/061text.mu.html')
-rw-r--r-- | html/061text.mu.html | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/html/061text.mu.html b/html/061text.mu.html index e438b576..08a836b3 100644 --- a/html/061text.mu.html +++ b/html/061text.mu.html @@ -1341,6 +1341,89 @@ body { font-size: 12pt; font-family: monospace; color: #eeeeee; background-color 1:array:character <span class="Special"><-</span> <span class="Constant">[]</span> ] ] + +<span class="muRecipe">def</span> parse-whole-number in:text<span class="muRecipe"> -> </span>out:num, error?:bool [ + <span class="Constant">local-scope</span> + <span class="Constant">load-ingredients</span> + out:num <span class="Special"><-</span> copy<span class="Constant"> 0</span> + result:num <span class="Special"><-</span> copy<span class="Constant"> 0</span> <span class="Comment"># temporary location</span> + i:num <span class="Special"><-</span> copy<span class="Constant"> 0</span> + len:num <span class="Special"><-</span> length *in + <span class="Delimiter">{</span> + done?:bool <span class="Special"><-</span> greater-or-equal i, len + <span class="muControl">break-if</span> done? + c:char <span class="Special"><-</span> index *in, i + x:num <span class="Special"><-</span> character-to-code c + digit:num, error?:bool <span class="Special"><-</span> character-code-to-digit x + <span class="muControl">reply-if</span> error? + result <span class="Special"><-</span> multiply result,<span class="Constant"> 10</span> + result <span class="Special"><-</span> add result, digit + i <span class="Special"><-</span> add i,<span class="Constant"> 1</span> + <span class="muControl">loop</span> + <span class="Delimiter">}</span> + <span class="Comment"># no error; all digits were valid</span> + out <span class="Special"><-</span> copy result +] + +<span class="Comment"># (contributed by Ella Couch)</span> +<span class="muRecipe">recipe</span> character-code-to-digit character-code:number<span class="muRecipe"> -> </span>result:number, error?:boolean [ + <span class="Constant">local-scope</span> + <span class="Constant">load-ingredients</span> + result <span class="Special"><-</span> copy<span class="Constant"> 0</span> + error? <span class="Special"><-</span> lesser-than character-code,<span class="Constant"> 48</span> <span class="Comment"># '0'</span> + <span class="muControl">reply-if</span> error? + error? <span class="Special"><-</span> greater-than character-code,<span class="Constant"> 57</span> <span class="Comment"># '9'</span> + <span class="muControl">reply-if</span> error? + result <span class="Special"><-</span> subtract character-code,<span class="Constant"> 48</span> +] + +<span class="muScenario">scenario</span> character-code-to-digit-contain-only-digit [ + <span class="Constant">local-scope</span> + a:number <span class="Special"><-</span> copy<span class="Constant"> 48</span> <span class="Comment"># character code for '0'</span> + run [ + 10:number/<span class="Special">raw</span>, 11:boolean/<span class="Special">raw</span> <span class="Special"><-</span> character-code-to-digit a + ] + memory-should-contain [ + <span class="Constant"> 10</span> <span class="Special"><-</span><span class="Constant"> 0</span> + <span class="Constant"> 11</span> <span class="Special"><-</span><span class="Constant"> 0</span> <span class="Comment"># no error</span> + ] +] + +<span class="muScenario">scenario</span> character-code-to-digit-contain-only-digit-2 [ + <span class="Constant">local-scope</span> + a:number <span class="Special"><-</span> copy<span class="Constant"> 57</span> <span class="Comment"># character code for '9'</span> + run [ + 1:number/<span class="Special">raw</span>, 2:boolean/<span class="Special">raw</span> <span class="Special"><-</span> character-code-to-digit a + ] + memory-should-contain [ + <span class="Constant"> 1</span> <span class="Special"><-</span><span class="Constant"> 9</span> + <span class="Constant"> 2</span> <span class="Special"><-</span><span class="Constant"> 0</span> <span class="Comment"># no error</span> + ] +] + +<span class="muScenario">scenario</span> character-code-to-digit-handles-codes-lower-than-zero [ + <span class="Constant">local-scope</span> + a:number <span class="Special"><-</span> copy<span class="Constant"> 47</span> + run [ + 10:number/<span class="Special">raw</span>, 11:boolean/<span class="Special">raw</span> <span class="Special"><-</span> character-code-to-digit a + ] + memory-should-contain [ + <span class="Constant"> 10</span> <span class="Special"><-</span><span class="Constant"> 0</span> + <span class="Constant"> 11</span> <span class="Special"><-</span><span class="Constant"> 1</span> <span class="Comment"># error</span> + ] +] + +<span class="muScenario">scenario</span> character-code-to-digit-handles-codes-larger-than-nine [ + <span class="Constant">local-scope</span> + a:number <span class="Special"><-</span> copy<span class="Constant"> 58</span> + run [ + 10:number/<span class="Special">raw</span>, 11:boolean/<span class="Special">raw</span> <span class="Special"><-</span> character-code-to-digit a + ] + memory-should-contain [ + <span class="Constant"> 10</span> <span class="Special"><-</span><span class="Constant"> 0</span> + <span class="Constant"> 11</span> <span class="Special"><-</span><span class="Constant"> 1</span> <span class="Comment"># error</span> + ] +] </pre> </body> </html> |