Annotation of gforth/kernel/nio.fs, revision 1.5
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:
21: : pad ( -- addr ) \ core-ext
1.3 pazsan 22: here word-pno-size + aligned ;
1.1 anton 23:
24: \ hold <# #> sign # #s 25jan92py
25:
26: : hold ( char -- ) \ core
1.5 ! crook 27: \G Used within @code{<#} and @code{#>}. Append the character char
! 28: \G to the pictured numeric output string.
1.1 anton 29: pad cell - -1 chars over +! @ c! ;
30:
31: : <# ( -- ) \ core less-number-sign
1.5 ! crook 32: \G Initialise/clear the pictured numeric output string.
1.1 anton 33: pad cell - dup ! ;
34:
35: : #> ( xd -- addr u ) \ core number-sign-greater
1.5 ! crook 36: \G Complete the pictured numeric output string by
! 37: \G discarding xd and returning addr u; the address and length
! 38: \G of the formatted string. A Standard program may modify characters
! 39: \G within the string.
1.1 anton 40: 2drop pad cell - dup @ tuck - ;
41:
42: : sign ( n -- ) \ core
1.5 ! crook 43: \G Used within @code{<#} and @code{#>}. If n (a @var{single} number)
! 44: \G is negative, append the display code for a minus sign to the pictured
! 45: \G numeric output string. Since the string is built up "backwards"
! 46: \G this is usually used immediately prior to @code{#>}, as shown in
! 47: \G the examples below.
1.1 anton 48: 0< IF [char] - hold THEN ;
49:
50: : # ( ud1 -- ud2 ) \ core number-sign
1.5 ! crook 51: \G Used within @code{<#} and @code{#>}. Add the next least-significant
! 52: \G digit to the pictured numeric output string. This is achieved by dividing ud1
! 53: \G by the number in @code{base} to leave quotient ud2 and remainder n; n
! 54: \G is converted to the appropriate display code (eg ASCII code) and appended
! 55: \G to the string. If the number has been fully converted, ud1 will be 0 and
! 56: \G @code{#} will append a "0" to the string.
1.1 anton 57: base @ 2 max ud/mod rot 9 over <
58: IF
59: [ char A char 9 - 1- ] Literal +
60: THEN
61: [char] 0 + hold ;
62:
1.5 ! crook 63: : #s ( ud -- 0 0 ) \ core number-sign-s
! 64: \G Used within @code{<#} and @code{#>}. Convert all remaining digits
! 65: \G using the same algorithm as for @code{#}. @code{#s} will convert
! 66: \G at least one digit. Therefore, if ud is 0, @code{#s} will append
! 67: \G a "0" to the pictured numeric output string.
1.1 anton 68: BEGIN
69: # 2dup or 0=
70: UNTIL ;
71:
72: \ print numbers 07jun92py
73:
74: : d.r ( d n -- ) \ double d-dot-r
1.5 ! crook 75: \G Display d right-aligned in a field n characters wide. If more than
! 76: \G n characters are needed to display the number, all digits are displayed.
! 77: \G If appropriate, n must include a character for a leading "-".
1.1 anton 78: >r tuck dabs <# #s rot sign #>
79: r> over - spaces type ;
80:
81: : ud.r ( ud n -- ) \ gforth u-d-dot-r
1.5 ! crook 82: \G Display ud right-aligned in a field n characters wide. If more than
! 83: \G n characters are needed to display the number, all digits are displayed.
1.1 anton 84: >r <# #s #> r> over - spaces type ;
85:
86: : .r ( n1 n2 -- ) \ core-ext dot-r
1.5 ! crook 87: \G Display n1 right-aligned in a field n2 characters wide. If more than
! 88: \G n2 characters are needed to display the number, all digits are displayed.
! 89: \G If appropriate, n2 must include a character for a leading "-".
1.1 anton 90: >r s>d r> d.r ;
1.5 ! crook 91:
1.1 anton 92: : u.r ( u n -- ) \ core-ext u-dot-r
1.5 ! crook 93: \G Display u right-aligned in a field n characters wide. If more than
! 94: \G n characters are needed to display the number, all digits are displayed.
1.1 anton 95: 0 swap ud.r ;
96:
97: : d. ( d -- ) \ double d-dot
1.5 ! crook 98: \G Display (the signed double number) d in free-format. followed by a space.
1.1 anton 99: 0 d.r space ;
1.5 ! crook 100:
1.1 anton 101: : ud. ( ud -- ) \ gforth u-d-dot
1.5 ! crook 102: \G Display (the signed double number) ud in free-format, followed by a space.
1.1 anton 103: 0 ud.r space ;
104:
105: : . ( n -- ) \ core dot
1.5 ! crook 106: \G Display (the signed single number) n in free-format, followed by a space.
1.1 anton 107: s>d d. ;
1.5 ! crook 108:
1.1 anton 109: : u. ( u -- ) \ core u-dot
1.5 ! crook 110: \G Display (the unsigned single number) u in free-format, followed by a space.
1.1 anton 111: 0 ud. ;
112:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>