Annotation of gforth/kernel/basics.fs, revision 1.33
1.1 anton 1: \ kernel.fs GForth kernel 17dec92py
2:
1.28 anton 3: \ Copyright (C) 1995,1998,2000 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
1.29 anton 19: \ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
1.1 anton 20:
21: \ Idea and implementation: Bernd Paysan (py)
22:
1.16 jwilke 23: \ Needs:
24:
25: require ./vars.fs
1.32 anton 26: require ../compat/strcomp.fs
1.16 jwilke 27:
28: hex
1.1 anton 29:
30: \ labels for some code addresses
31:
32: \- NIL NIL AConstant NIL \ gforth
33:
34: \ Aliases
35:
1.4 jwilke 36: [IFUNDEF] r@
1.1 anton 37: ' i Alias r@ ( -- w ; R: w -- w ) \ core r-fetch
1.4 jwilke 38: [THEN]
1.1 anton 39:
40: \ !! this is machine-dependent, but works on all but the strangest machines
41:
1.23 anton 42: : maxaligned ( addr1 -- addr2 ) \ gforth
43: \G @i{addr2} is the first address after @i{addr1} that satisfies
44: \G all alignment restrictions.
1.1 anton 45: [ /maxalign 1 - ] Literal + [ 0 /maxalign - ] Literal and ;
1.23 anton 46: \ !! machine-dependent and won't work if "0 >body" <> "0 >body
47: \G maxaligned"
1.1 anton 48: ' maxaligned Alias cfaligned ( addr1 -- addr2 ) \ gforth
1.23 anton 49: \G @i{addr2} is the first address after @i{addr1} that is aligned for
50: \G a code field (i.e., such that the corresponding body is maxaligned).
1.1 anton 51:
52: : chars ( n1 -- n2 ) \ core
1.23 anton 53: \G @i{n2} is the number of address units of @i{n1} chars.""
1.1 anton 54: ; immediate
55:
56:
57: \ : A! ( addr1 addr2 -- ) \ gforth
58: \ dup relon ! ;
59: \ : A, ( addr -- ) \ gforth
60: \ here cell allot A! ;
61: ' ! alias A! ( addr1 addr2 -- ) \ gforth
62:
1.2 anton 63: \ UNUSED 17may93jaw
64:
1.4 jwilke 65: has? ec
66: [IF]
1.30 jwilke 67: unlock ram-dictionary borders nip lock
68: AConstant dictionary-end
1.4 jwilke 69: [ELSE]
1.2 anton 70: : dictionary-end ( -- addr )
1.31 jwilke 71: forthstart [ 3 cells image-header + ] Aliteral @ + ;
1.4 jwilke 72: [THEN]
1.2 anton 73:
1.14 anton 74: : usable-dictionary-end ( -- addr )
75: dictionary-end [ word-pno-size pad-minsize + ] Literal - ;
76:
1.2 anton 77: : unused ( -- u ) \ core-ext
1.13 crook 78: \G Return the amount of free space remaining (in address units) in
79: \G the region addressed by @code{here}.
1.14 anton 80: usable-dictionary-end here - ;
1.2 anton 81:
1.1 anton 82: \ here is used for pad calculation!
83:
84: : dp ( -- addr ) \ gforth
85: dpp @ ;
1.13 crook 86: : here ( -- addr ) \ core
87: \G Return the address of the next free location in data space.
1.1 anton 88: dp @ ;
89:
90: \ on off 23feb93py
91:
1.4 jwilke 92: \ on is used by docol:
1.15 crook 93: : on ( a-addr -- ) \ gforth
94: \G Set the (value of the) variable at @i{a-addr} to @code{true}.
1.1 anton 95: true swap ! ;
1.15 crook 96: : off ( a-addr -- ) \ gforth
97: \G Set the (value of the) variable at @i{a-addr} to @code{false}.
1.1 anton 98: false swap ! ;
99:
100: \ dabs roll 17may93jaw
101:
1.24 anton 102: : dabs ( d -- ud ) \ double d-abs
1.1 anton 103: dup 0< IF dnegate THEN ;
104:
105: : roll ( x0 x1 .. xn n -- x1 .. xn x0 ) \ core-ext
106: dup 1+ pick >r
107: cells sp@ cell+ dup cell+ rot move drop r> ;
108:
109: \ place bounds 13feb93py
110:
111: : place ( addr len to -- ) \ gforth
112: over >r rot over 1+ r> move c! ;
1.27 anton 113: : bounds ( addr u -- addr+u addr ) \ gforth
114: \G Given a memory block represented by starting address @i{addr}
115: \G and length @i{u} in aus, produce the end address @i{addr+u} and
116: \G the start address in the right order for @code{u+do} or
117: \G @code{?do}.
1.1 anton 118: over + swap ;
119:
120: \ (word) 22feb93py
121:
122: : scan ( addr1 n1 char -- addr2 n2 ) \ gforth
123: \ skip all characters not equal to char
124: >r
125: BEGIN
126: dup
127: WHILE
128: over c@ r@ <>
129: WHILE
130: 1 /string
131: REPEAT THEN
132: rdrop ;
133: : skip ( addr1 n1 char -- addr2 n2 ) \ gforth
134: \ skip all characters equal to char
135: >r
136: BEGIN
137: dup
138: WHILE
139: over c@ r@ =
140: WHILE
141: 1 /string
142: REPEAT THEN
143: rdrop ;
144:
145: \ digit? 17dec92py
146:
147: : digit? ( char -- digit true/ false ) \ gforth
148: base @ $100 =
149: IF
150: true EXIT
151: THEN
152: toupper [char] 0 - dup 9 u> IF
1.16 jwilke 153: [ char A char 9 1 + - ] literal -
1.1 anton 154: dup 9 u<= IF
155: drop false EXIT
156: THEN
157: THEN
158: dup base @ u>= IF
159: drop false EXIT
160: THEN
161: true ;
162:
163: : accumulate ( +d0 addr digit - +d1 addr )
164: swap >r swap base @ um* drop rot base @ um* d+ r> ;
165:
1.18 crook 166: : >number ( ud1 c-addr1 u1 -- ud2 c-addr2 u2 ) \ core to-number
1.22 anton 167: \G Attempt to convert the character string @var{c-addr1 u1} to an
1.13 crook 168: \G unsigned number in the current number base. The double
169: \G @var{ud1} accumulates the result of the conversion to form
170: \G @var{ud2}. Conversion continues, left-to-right, until the whole
171: \G string is converted or a character that is not convertable in
172: \G the current number base is encountered (including + or -). For
173: \G each convertable character, @var{ud1} is first multiplied by
174: \G the value in @code{BASE} and then incremented by the value
175: \G represented by the character. @var{c-addr2} is the location of
176: \G the first unconverted character (past the end of the string if
177: \G the whole string was converted). @var{u2} is the number of
178: \G unconverted characters in the string. Overflow is not detected.
1.1 anton 179: 0
180: ?DO
181: count digit?
182: WHILE
183: accumulate
184: LOOP
185: 0
186: ELSE
187: 1- I' I -
188: UNLOOP
189: THEN ;
190:
191: \ s>d um/mod 21mar93py
192:
193: : s>d ( n -- d ) \ core s-to-d
194: dup 0< ;
195:
196: : ud/mod ( ud1 u2 -- urem udquot ) \ gforth
197: >r 0 r@ um/mod r> swap >r
198: um/mod r> ;
199:
200: \ catch throw 23feb93py
201:
1.5 jwilke 202: has? glocals [IF]
1.12 crook 203: : lp@ ( -- addr ) \ gforth lp-fetch
1.1 anton 204: laddr# [ 0 , ] ;
205: [THEN]
206:
1.17 anton 207: defer catch ( x1 .. xn xt -- y1 .. ym 0 / z1 .. zn error ) \ exception
1.24 anton 208: \G @code{Executes} @i{xt}. If execution returns normally,
209: \G @code{catch} pushes 0 on the stack. If execution returns through
210: \G @code{throw}, all the stacks are reset to the depth on entry to
211: \G @code{catch}, and the TOS (the @i{xt} position) is replaced with
212: \G the throw code.
213:
1.17 anton 214: :noname ( ... xt -- ... 0 )
215: execute 0 ;
216: is catch
1.1 anton 217:
1.24 anton 218: defer throw ( y1 .. ym nerror -- y1 .. ym / z1 .. zn error ) \ exception
219: \G If @i{nerror} is 0, drop it and continue. Otherwise, transfer
220: \G control to the next dynamically enclosing exception handler, reset
221: \G the stacks accordingly, and push @i{nerror}.
222:
223: :noname ( y1 .. ym error -- y1 .. ym / z1 .. zn error )
1.19 anton 224: ?dup if
1.31 jwilke 225: [ has? ec 0= [IF] here image-header 9 cells + ! [THEN] ]
1.21 pazsan 226: cr .error cr
227: [ has? file [IF] ] script? IF 1 (bye) ELSE quit THEN
228: [ [ELSE] ] quit [ [THEN] ]
1.19 anton 229: then ;
230: is throw
231:
1.1 anton 232: \ (abort")
233:
1.33 ! anton 234: : c(abort") ( c-addr -- )
! 235: "error ! -2 throw ;
! 236:
1.1 anton 237: : (abort")
238: "lit >r
239: IF
240: r> "error ! -2 throw
241: THEN
242: rdrop ;
1.6 pazsan 243:
244: : abort ( ?? -- ?? ) \ core,exception-ext
1.12 crook 245: \G @code{-1 throw}.
1.6 pazsan 246: -1 throw ;
1.1 anton 247:
248: \ ?stack 23feb93py
249:
250: : ?stack ( ?? -- ?? ) \ gforth
1.3 jwilke 251: sp@ sp0 @ u> IF -4 throw THEN
1.5 jwilke 252: [ has? floating [IF] ]
1.3 jwilke 253: fp@ fp0 @ u> IF -&45 throw THEN
1.1 anton 254: [ [THEN] ]
255: ;
256: \ ?stack should be code -- it touches an empty stack!
257:
258: \ DEPTH 9may93jaw
259:
1.9 crook 260: : depth ( -- +n ) \ core depth
1.12 crook 261: \G @var{+n} is the number of values that were on the data stack before
262: \G @var{+n} itself was placed on the stack.
1.3 jwilke 263: sp@ sp0 @ swap - cell / ;
1.9 crook 264:
265: : clearstack ( ... -- ) \ gforth clear-stack
266: \G remove and discard all/any items from the data stack.
1.3 jwilke 267: sp0 @ sp! ;
1.1 anton 268:
269: \ Strings 22feb93py
270:
271: : "lit ( -- addr )
272: r> r> dup count + aligned >r swap >r ;
273:
274: \ */MOD */ 17may93jaw
275:
276: \ !! I think */mod should have the same rounding behaviour as / - anton
277: : */mod ( n1 n2 n3 -- n4 n5 ) \ core star-slash-mod
1.24 anton 278: \G n1*n2=n3*n5+n4, with the intermediate result (n1*n2) being double.
1.1 anton 279: >r m* r> sm/rem ;
280:
281: : */ ( n1 n2 n3 -- n4 ) \ core star-slash
1.23 anton 282: \G n4=(n1*n2)/n3, with the intermediate result being double.
1.1 anton 283: */mod nip ;
284:
285: \ HEX DECIMAL 2may93jaw
286:
287: : decimal ( -- ) \ core
1.26 anton 288: \G Set @code{base} to &10 (decimal).
1.1 anton 289: a base ! ;
290: : hex ( -- ) \ core-ext
1.26 anton 291: \G Set @code{base} to &16 (hexadecimal).
1.1 anton 292: 10 base ! ;
293:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>