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

1.1       anton       1: \ Number IO
                      2: 
1.4       anton       3: \ Copyright (C) 1995,1996,1997,1998 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
                      9: \ as published by the Free Software Foundation; either version 2
                     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
                     18: \ along with this program; if not, write to the Free Software
                     19: \ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
                     20: 
1.10    ! crook      21: : pad    ( -- c-addr ) \ core-ext
        !            22:     \G @var{c-addr} is the address of a transient region that can be
        !            23:     \G used as temporary data storage. At least 84 characters of space
        !            24:     \G is available.
1.3       pazsan     25:     here word-pno-size + aligned ;
1.1       anton      26: 
                     27: \ hold <# #> sign # #s                                 25jan92py
                     28: 
                     29: : hold    ( char -- ) \ core
1.10    ! crook      30:     \G Used within @code{<#} and @code{#>}. Append the character
        !            31:     \G @var{char} to the pictured numeric output string.
1.6       anton      32:     -1 chars holdptr +!
                     33:     holdptr @ dup holdbuf u< -&17 and throw
                     34:     c! ;
1.1       anton      35: 
                     36: : <# ( -- ) \ core     less-number-sign
1.5       crook      37:     \G Initialise/clear the pictured numeric output string.
1.7       anton      38:     holdbuf-end dup holdptr ! holdend ! ;
1.1       anton      39: 
                     40: : #>      ( xd -- addr u ) \ core      number-sign-greater
1.5       crook      41:     \G Complete the pictured numeric output string by
                     42:     \G discarding xd and returning addr u; the address and length
                     43:     \G of the formatted string. A Standard program may modify characters
                     44:     \G within the string.
1.7       anton      45:     2drop holdptr @ holdend @ over - ;
1.6       anton      46: 
1.7       anton      47: : <<# ( -- ) \ gforth  less-less-number-sign
1.9       crook      48:     \G Start a hold area that ends with @code{#>>}. Can be nested in
1.6       anton      49:     \G each other and in @code{<#}.  Note: if you do not match up the
                     50:     \G @code{<<#}s with @code{#>>}s, you will eventually run out of
                     51:     \G hold area; you can reset the hold area to empty with @code{<#}.
1.7       anton      52:     holdend @ holdptr @ - hold
                     53:     holdptr @ holdend ! ;
1.6       anton      54: 
1.7       anton      55: : #>> ( -- ) \ gforth  number-sign-greater-greater
1.9       crook      56:     \G Release the hold area started with @code{<<#}.
1.8       anton      57:     holdend @ dup holdbuf-end u>= -&11 and throw
                     58:     count chars bounds holdptr ! holdend ! ;
1.1       anton      59: 
                     60: : sign    ( n -- ) \ core
1.5       crook      61:     \G Used within @code{<#} and @code{#>}. If n (a @var{single} number)
                     62:     \G is negative, append the display code for a minus sign to the pictured
                     63:     \G numeric output string. Since the string is built up "backwards"
                     64:     \G this is usually used immediately prior to @code{#>}, as shown in
                     65:     \G the examples below.
1.1       anton      66:     0< IF  [char] - hold  THEN ;
                     67: 
                     68: : #       ( ud1 -- ud2 ) \ core                number-sign
1.5       crook      69:     \G Used within @code{<#} and @code{#>}. Add the next least-significant
                     70:     \G digit to the pictured numeric output string. This is achieved by dividing ud1
                     71:     \G by the number in @code{base} to leave quotient ud2 and remainder n; n
                     72:     \G is converted to the appropriate display code (eg ASCII code) and appended
                     73:     \G to the string. If the number has been fully converted, ud1 will be 0 and
                     74:     \G @code{#} will append a "0" to the string.
1.1       anton      75:     base @ 2 max ud/mod rot 9 over <
                     76:     IF
                     77:        [ char A char 9 - 1- ] Literal +
                     78:     THEN
                     79:     [char] 0 + hold ;
                     80: 
1.5       crook      81: : #s      ( ud -- 0 0 ) \ core number-sign-s
                     82:     \G Used within @code{<#} and @code{#>}. Convert all remaining digits
                     83:     \G using the same algorithm as for @code{#}. @code{#s} will convert
                     84:     \G at least one digit. Therefore, if ud is 0, @code{#s} will append
                     85:     \G a "0" to the pictured numeric output string.
1.1       anton      86:     BEGIN
                     87:        # 2dup or 0=
                     88:     UNTIL ;
                     89: 
                     90: \ print numbers                                        07jun92py
                     91: 
                     92: : d.r ( d n -- ) \ double      d-dot-r
1.9       crook      93:     \G Display @var{d} right-aligned in a field @var{n} characters wide. If more than
                     94:     \G @var{n} characters are needed to display the number, all digits are displayed.
                     95:     \G If appropriate, @var{n} must include a character for a leading "-".
1.7       anton      96:     >r tuck  dabs  <<# #s  rot sign #>
                     97:     r> over - spaces  type #>> ;
1.1       anton      98: 
                     99: : ud.r ( ud n -- ) \ gforth    u-d-dot-r
1.9       crook     100:     \G Display @var{ud} right-aligned in a field @var{n} characters wide. If more than
                    101:     \G @var{n} characters are needed to display the number, all digits are displayed.
1.7       anton     102:     >r <<# #s #> r> over - spaces type #>> ;
1.1       anton     103: 
                    104: : .r ( n1 n2 -- ) \ core-ext   dot-r
1.9       crook     105:     \G Display @var{n1} right-aligned in a field @var{n2} characters wide. If more than
                    106:     \G @var{n2} characters are needed to display the number, all digits are displayed.
                    107:     \G If appropriate, @var{n2} must include a character for a leading "-".
1.1       anton     108:     >r s>d r> d.r ;
1.5       crook     109: 
1.1       anton     110: : u.r ( u n -- )  \ core-ext   u-dot-r
1.9       crook     111:     \G Display @var{u} right-aligned in a field @var{n} characters wide. If more than
                    112:     \G @var{n} characters are needed to display the number, all digits are displayed.
1.1       anton     113:     0 swap ud.r ;
                    114: 
                    115: : d. ( d -- ) \ double d-dot
1.9       crook     116:     \G Display (the signed double number) @var{d} in free-format. followed by a space.
1.1       anton     117:     0 d.r space ;
1.5       crook     118: 
1.1       anton     119: : ud. ( ud -- ) \ gforth       u-d-dot
1.9       crook     120:     \G Display (the signed double number) @var{ud} in free-format, followed by a space.
1.1       anton     121:     0 ud.r space ;
                    122: 
                    123: : . ( n -- ) \ core    dot
1.9       crook     124:     \G Display (the signed single number) @var{n} in free-format, followed by a space.
1.1       anton     125:     s>d d. ;
1.5       crook     126: 
1.1       anton     127: : u. ( u -- ) \ core   u-dot
1.9       crook     128:     \G Display (the unsigned single number) @var{u} in free-format, followed by a space.
1.1       anton     129:     0 ud. ;
                    130: 

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