[gforth] / gforth / kernel / nio.fs  

gforth: gforth/kernel/nio.fs


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

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help