Annotation of gforth/kernel/nio.fs, revision 1.24

1.1       anton       1: \ Number IO
                      2: 
1.23      anton       3: \ Copyright (C) 1995,1996,1997,1998,2000,2003,2006,2007 Free Software Foundation, Inc.
1.1       anton       4: 
                      5: \ This file is part of Gforth.
                      6: 
                      7: \ Gforth is free software; you can redistribute it and/or
                      8: \ modify it under the terms of the GNU General Public License
1.22      anton       9: \ as published by the Free Software Foundation, either version 3
1.1       anton      10: \ of the License, or (at your option) any later version.
                     11: 
                     12: \ This program is distributed in the hope that it will be useful,
                     13: \ but WITHOUT ANY WARRANTY; without even the implied warranty of
                     14: \ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     15: \ GNU General Public License for more details.
                     16: 
                     17: \ You should have received a copy of the GNU General Public License
1.22      anton      18: \ along with this program. If not, see http://www.gnu.org/licenses/.
1.1       anton      19: 
1.11      jwilke     20: require ./io.fs
                     21: 
1.10      crook      22: : pad    ( -- c-addr ) \ core-ext
                     23:     \G @var{c-addr} is the address of a transient region that can be
                     24:     \G used as temporary data storage. At least 84 characters of space
                     25:     \G is available.
1.20      pazsan     26:     [ has? flash [IF] ] normal-dp @ [ [ELSE] ] here [ [THEN] ]
                     27:     word-pno-size + aligned ;
1.1       anton      28: 
                     29: \ hold <# #> sign # #s                                 25jan92py
                     30: 
1.19      pazsan     31: has? EC [IF]
                     32:     : hld  ( -- addr )  pad cell - ;
                     33:     : hold  ( char -- )  hld -1 over +! @ c! ;
                     34:     : <#    hld dup ! ;
                     35:     : #>   ( d -- addr +n )  2drop hld dup @ tuck - ;
                     36:     ' <# alias <<#
                     37:     ' noop alias #>>
                     38: [ELSE]
1.1       anton      39: : hold    ( char -- ) \ core
1.10      crook      40:     \G Used within @code{<#} and @code{#>}. Append the character
                     41:     \G @var{char} to the pictured numeric output string.
1.6       anton      42:     -1 chars holdptr +!
                     43:     holdptr @ dup holdbuf u< -&17 and throw
                     44:     c! ;
1.1       anton      45: 
                     46: : <# ( -- ) \ core     less-number-sign
1.5       crook      47:     \G Initialise/clear the pictured numeric output string.
1.7       anton      48:     holdbuf-end dup holdptr ! holdend ! ;
1.1       anton      49: 
                     50: : #>      ( xd -- addr u ) \ core      number-sign-greater
1.13      crook      51:     \G Complete the pictured numeric output string by discarding
                     52:     \G @var{xd} and returning @var{addr u}; the address and length of
                     53:     \G the formatted string. A Standard program may modify characters
1.5       crook      54:     \G within the string.
1.7       anton      55:     2drop holdptr @ holdend @ over - ;
1.6       anton      56: 
1.7       anton      57: : <<# ( -- ) \ gforth  less-less-number-sign
1.9       crook      58:     \G Start a hold area that ends with @code{#>>}. Can be nested in
1.6       anton      59:     \G each other and in @code{<#}.  Note: if you do not match up the
                     60:     \G @code{<<#}s with @code{#>>}s, you will eventually run out of
                     61:     \G hold area; you can reset the hold area to empty with @code{<#}.
1.7       anton      62:     holdend @ holdptr @ - hold
                     63:     holdptr @ holdend ! ;
1.6       anton      64: 
1.7       anton      65: : #>> ( -- ) \ gforth  number-sign-greater-greater
1.9       crook      66:     \G Release the hold area started with @code{<<#}.
1.8       anton      67:     holdend @ dup holdbuf-end u>= -&11 and throw
                     68:     count chars bounds holdptr ! holdend ! ;
1.19      pazsan     69: [THEN]
1.1       anton      70: 
                     71: : sign    ( n -- ) \ core
1.13      crook      72:     \G Used within @code{<#} and @code{#>}. If @var{n} (a @var{single}
                     73:     \G number) is negative, append the display code for a minus sign
                     74:     \G to the pictured numeric output string. Since the string is
                     75:     \G built up ``backwards'' this is usually used immediately prior
                     76:     \G to @code{#>}, as shown in the examples below.
1.1       anton      77:     0< IF  [char] - hold  THEN ;
                     78: 
                     79: : #       ( ud1 -- ud2 ) \ core                number-sign
1.13      crook      80:     \G Used within @code{<#} and @code{#>}. Add the next
                     81:     \G least-significant digit to the pictured numeric output
                     82:     \G string. This is achieved by dividing @var{ud1} by the number in
                     83:     \G @code{base} to leave quotient @var{ud2} and remainder @var{n};
                     84:     \G @var{n} is converted to the appropriate display code (eg ASCII
                     85:     \G code) and appended to the string. If the number has been fully
                     86:     \G converted, @var{ud1} will be 0 and @code{#} will append a ``0''
                     87:     \G to the string.
1.12      anton      88:     base @ ud/mod rot 9 over <
1.1       anton      89:     IF
                     90:        [ char A char 9 - 1- ] Literal +
                     91:     THEN
                     92:     [char] 0 + hold ;
                     93: 
1.5       crook      94: : #s      ( ud -- 0 0 ) \ core number-sign-s
                     95:     \G Used within @code{<#} and @code{#>}. Convert all remaining digits
                     96:     \G using the same algorithm as for @code{#}. @code{#s} will convert
1.13      crook      97:     \G at least one digit. Therefore, if @var{ud} is 0, @code{#s} will append
                     98:     \G a ``0'' to the pictured numeric output string.
1.1       anton      99:     BEGIN
                    100:        # 2dup or 0=
                    101:     UNTIL ;
                    102: 
1.24    ! pazsan    103: : holds ( addr u -- )
        !           104:     BEGIN  dup  WHILE  1- 2dup + c@ hold  REPEAT  2drop ;
        !           105: 
1.1       anton     106: \ print numbers                                        07jun92py
                    107: 
                    108: : d.r ( d n -- ) \ double      d-dot-r
1.9       crook     109:     \G Display @var{d} right-aligned in a field @var{n} characters wide. If more than
                    110:     \G @var{n} characters are needed to display the number, all digits are displayed.
1.13      crook     111:     \G If appropriate, @var{n} must include a character for a leading ``-''.
1.7       anton     112:     >r tuck  dabs  <<# #s  rot sign #>
                    113:     r> over - spaces  type #>> ;
1.1       anton     114: 
                    115: : ud.r ( ud n -- ) \ gforth    u-d-dot-r
1.9       crook     116:     \G Display @var{ud} right-aligned in a field @var{n} characters wide. If more than
                    117:     \G @var{n} characters are needed to display the number, all digits are displayed.
1.7       anton     118:     >r <<# #s #> r> over - spaces type #>> ;
1.1       anton     119: 
                    120: : .r ( n1 n2 -- ) \ core-ext   dot-r
1.9       crook     121:     \G Display @var{n1} right-aligned in a field @var{n2} characters wide. If more than
                    122:     \G @var{n2} characters are needed to display the number, all digits are displayed.
1.13      crook     123:     \G If appropriate, @var{n2} must include a character for a leading ``-''.
1.1       anton     124:     >r s>d r> d.r ;
1.5       crook     125: 
1.1       anton     126: : u.r ( u n -- )  \ core-ext   u-dot-r
1.9       crook     127:     \G Display @var{u} right-aligned in a field @var{n} characters wide. If more than
                    128:     \G @var{n} characters are needed to display the number, all digits are displayed.
1.1       anton     129:     0 swap ud.r ;
                    130: 
                    131: : d. ( d -- ) \ double d-dot
1.9       crook     132:     \G Display (the signed double number) @var{d} in free-format. followed by a space.
1.1       anton     133:     0 d.r space ;
1.5       crook     134: 
1.1       anton     135: : ud. ( ud -- ) \ gforth       u-d-dot
1.9       crook     136:     \G Display (the signed double number) @var{ud} in free-format, followed by a space.
1.1       anton     137:     0 ud.r space ;
                    138: 
                    139: : . ( n -- ) \ core    dot
1.9       crook     140:     \G Display (the signed single number) @var{n} in free-format, followed by a space.
1.1       anton     141:     s>d d. ;
1.5       crook     142: 
1.1       anton     143: : u. ( u -- ) \ core   u-dot
1.9       crook     144:     \G Display (the unsigned single number) @var{u} in free-format, followed by a space.
1.1       anton     145:     0 ud. ;
                    146: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>