| \ Number IO |
\ Number IO |
| |
|
| \ Copyright (C) 1995,1996,1997,1998 Free Software Foundation, Inc. |
\ Copyright (C) 1995,1996,1997,1998,2000,2003 Free Software Foundation, Inc. |
| |
|
| \ This file is part of Gforth. |
\ This file is part of Gforth. |
| |
|
| |
|
| \ You should have received a copy of the GNU General Public License |
\ You should have received a copy of the GNU General Public License |
| \ along with this program; if not, write to the Free Software |
\ along with this program; if not, write to the Free Software |
| \ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
\ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA. |
| |
|
| : pad ( -- addr ) \ core-ext |
require ./io.fs |
| |
|
| |
: pad ( -- c-addr ) \ core-ext |
| |
\G @var{c-addr} is the address of a transient region that can be |
| |
\G used as temporary data storage. At least 84 characters of space |
| |
\G is available. |
| here word-pno-size + aligned ; |
here word-pno-size + aligned ; |
| |
|
| \ hold <# #> sign # #s 25jan92py |
\ hold <# #> sign # #s 25jan92py |
| |
|
| : hold ( char -- ) \ core |
: hold ( char -- ) \ core |
| \G Used within @code{<#} and @code{#>}. Append the character char |
\G Used within @code{<#} and @code{#>}. Append the character |
| \G to the pictured numeric output string. |
\G @var{char} to the pictured numeric output string. |
| -1 chars holdptr +! |
-1 chars holdptr +! |
| holdptr @ dup holdbuf u< -&17 and throw |
holdptr @ dup holdbuf u< -&17 and throw |
| c! ; |
c! ; |
| holdbuf-end dup holdptr ! holdend ! ; |
holdbuf-end dup holdptr ! holdend ! ; |
| |
|
| : #> ( xd -- addr u ) \ core number-sign-greater |
: #> ( xd -- addr u ) \ core number-sign-greater |
| \G Complete the pictured numeric output string by |
\G Complete the pictured numeric output string by discarding |
| \G discarding xd and returning addr u; the address and length |
\G @var{xd} and returning @var{addr u}; the address and length of |
| \G of the formatted string. A Standard program may modify characters |
\G the formatted string. A Standard program may modify characters |
| \G within the string. |
\G within the string. |
| 2drop holdptr @ holdend @ over - ; |
2drop holdptr @ holdend @ over - ; |
| |
|
| : <<# ( -- ) \ gforth less-less-number-sign |
: <<# ( -- ) \ gforth less-less-number-sign |
| \G starts a hold area that ends with @code{#>>}. Can be nested in |
\G Start a hold area that ends with @code{#>>}. Can be nested in |
| \G each other and in @code{<#}. Note: if you do not match up the |
\G each other and in @code{<#}. Note: if you do not match up the |
| \G @code{<<#}s with @code{#>>}s, you will eventually run out of |
\G @code{<<#}s with @code{#>>}s, you will eventually run out of |
| \G hold area; you can reset the hold area to empty with @code{<#}. |
\G hold area; you can reset the hold area to empty with @code{<#}. |
| holdptr @ holdend ! ; |
holdptr @ holdend ! ; |
| |
|
| : #>> ( -- ) \ gforth number-sign-greater-greater |
: #>> ( -- ) \ gforth number-sign-greater-greater |
| \G releases the hold area started with @code{<<#}. |
\G Release the hold area started with @code{<<#}. |
| holdend @ dup holdbuf-end u>= -&11 and throw |
holdend @ dup holdbuf-end u>= -&11 and throw |
| count chars bounds holdptr ! holdend ! ; |
count chars bounds holdptr ! holdend ! ; |
| |
|
| : sign ( n -- ) \ core |
: sign ( n -- ) \ core |
| \G Used within @code{<#} and @code{#>}. If n (a @var{single} number) |
\G Used within @code{<#} and @code{#>}. If @var{n} (a @var{single} |
| \G is negative, append the display code for a minus sign to the pictured |
\G number) is negative, append the display code for a minus sign |
| \G numeric output string. Since the string is built up "backwards" |
\G to the pictured numeric output string. Since the string is |
| \G this is usually used immediately prior to @code{#>}, as shown in |
\G built up ``backwards'' this is usually used immediately prior |
| \G the examples below. |
\G to @code{#>}, as shown in the examples below. |
| 0< IF [char] - hold THEN ; |
0< IF [char] - hold THEN ; |
| |
|
| : # ( ud1 -- ud2 ) \ core number-sign |
: # ( ud1 -- ud2 ) \ core number-sign |
| \G Used within @code{<#} and @code{#>}. Add the next least-significant |
\G Used within @code{<#} and @code{#>}. Add the next |
| \G digit to the pictured numeric output string. This is achieved by dividing ud1 |
\G least-significant digit to the pictured numeric output |
| \G by the number in @code{base} to leave quotient ud2 and remainder n; n |
\G string. This is achieved by dividing @var{ud1} by the number in |
| \G is converted to the appropriate display code (eg ASCII code) and appended |
\G @code{base} to leave quotient @var{ud2} and remainder @var{n}; |
| \G to the string. If the number has been fully converted, ud1 will be 0 and |
\G @var{n} is converted to the appropriate display code (eg ASCII |
| \G @code{#} will append a "0" to the string. |
\G code) and appended to the string. If the number has been fully |
| base @ 2 max ud/mod rot 9 over < |
\G converted, @var{ud1} will be 0 and @code{#} will append a ``0'' |
| |
\G to the string. |
| |
base @ ud/mod rot 9 over < |
| IF |
IF |
| [ char A char 9 - 1- ] Literal + |
[ char A char 9 - 1- ] Literal + |
| THEN |
THEN |
| : #s ( ud -- 0 0 ) \ core number-sign-s |
: #s ( ud -- 0 0 ) \ core number-sign-s |
| \G Used within @code{<#} and @code{#>}. Convert all remaining digits |
\G Used within @code{<#} and @code{#>}. Convert all remaining digits |
| \G using the same algorithm as for @code{#}. @code{#s} will convert |
\G using the same algorithm as for @code{#}. @code{#s} will convert |
| \G at least one digit. Therefore, if ud is 0, @code{#s} will append |
\G at least one digit. Therefore, if @var{ud} is 0, @code{#s} will append |
| \G a "0" to the pictured numeric output string. |
\G a ``0'' to the pictured numeric output string. |
| BEGIN |
BEGIN |
| # 2dup or 0= |
# 2dup or 0= |
| UNTIL ; |
UNTIL ; |
| \ print numbers 07jun92py |
\ print numbers 07jun92py |
| |
|
| : d.r ( d n -- ) \ double d-dot-r |
: d.r ( d n -- ) \ double d-dot-r |
| \G Display d right-aligned in a field n characters wide. If more than |
\G Display @var{d} right-aligned in a field @var{n} characters wide. If more than |
| \G n characters are needed to display the number, all digits are displayed. |
\G @var{n} characters are needed to display the number, all digits are displayed. |
| \G If appropriate, n must include a character for a leading "-". |
\G If appropriate, @var{n} must include a character for a leading ``-''. |
| >r tuck dabs <<# #s rot sign #> |
>r tuck dabs <<# #s rot sign #> |
| r> over - spaces type #>> ; |
r> over - spaces type #>> ; |
| |
|
| : ud.r ( ud n -- ) \ gforth u-d-dot-r |
: ud.r ( ud n -- ) \ gforth u-d-dot-r |
| \G Display ud right-aligned in a field n characters wide. If more than |
\G Display @var{ud} right-aligned in a field @var{n} characters wide. If more than |
| \G n characters are needed to display the number, all digits are displayed. |
\G @var{n} characters are needed to display the number, all digits are displayed. |
| >r <<# #s #> r> over - spaces type #>> ; |
>r <<# #s #> r> over - spaces type #>> ; |
| |
|
| : .r ( n1 n2 -- ) \ core-ext dot-r |
: .r ( n1 n2 -- ) \ core-ext dot-r |
| \G Display n1 right-aligned in a field n2 characters wide. If more than |
\G Display @var{n1} right-aligned in a field @var{n2} characters wide. If more than |
| \G n2 characters are needed to display the number, all digits are displayed. |
\G @var{n2} characters are needed to display the number, all digits are displayed. |
| \G If appropriate, n2 must include a character for a leading "-". |
\G If appropriate, @var{n2} must include a character for a leading ``-''. |
| >r s>d r> d.r ; |
>r s>d r> d.r ; |
| |
|
| : u.r ( u n -- ) \ core-ext u-dot-r |
: u.r ( u n -- ) \ core-ext u-dot-r |
| \G Display u right-aligned in a field n characters wide. If more than |
\G Display @var{u} right-aligned in a field @var{n} characters wide. If more than |
| \G n characters are needed to display the number, all digits are displayed. |
\G @var{n} characters are needed to display the number, all digits are displayed. |
| 0 swap ud.r ; |
0 swap ud.r ; |
| |
|
| : d. ( d -- ) \ double d-dot |
: d. ( d -- ) \ double d-dot |
| \G Display (the signed double number) d in free-format. followed by a space. |
\G Display (the signed double number) @var{d} in free-format. followed by a space. |
| 0 d.r space ; |
0 d.r space ; |
| |
|
| : ud. ( ud -- ) \ gforth u-d-dot |
: ud. ( ud -- ) \ gforth u-d-dot |
| \G Display (the signed double number) ud in free-format, followed by a space. |
\G Display (the signed double number) @var{ud} in free-format, followed by a space. |
| 0 ud.r space ; |
0 ud.r space ; |
| |
|
| : . ( n -- ) \ core dot |
: . ( n -- ) \ core dot |
| \G Display (the signed single number) n in free-format, followed by a space. |
\G Display (the signed single number) @var{n} in free-format, followed by a space. |
| s>d d. ; |
s>d d. ; |
| |
|
| : u. ( u -- ) \ core u-dot |
: u. ( u -- ) \ core u-dot |
| \G Display (the unsigned single number) u in free-format, followed by a space. |
\G Display (the unsigned single number) @var{u} in free-format, followed by a space. |
| 0 ud. ; |
0 ud. ; |
| |
|