1 \
FLK kernel
words (core
word set
)
3 \
Copyright (C) 1998 Lars Krueger
5 \
This file is
part of FLK.
7 \
FLK 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
.
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.
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.
21 \ $
Id: flkkern
.fs
,v
1.15 1998/09/13 15:42:04 root
Exp $
22 \ $
Log: flkkern
.fs
,v $
23 \
Revision 1.15 1998/09/13 15:42:04 root
24 \ introduced separate
control flow stack
26 \
Revision 1.14 1998/08/30 10:50:59 root
27 \
new optimizing algorithm
29 \
Revision 1.13 1998/07/13 18:08:54 root
30 \ various
optimizations
32 \
Revision 1.12 1998/07/03 09:09:28 root
33 \ support
for level
2 optimizer
35 \
Revision 1.11 1998/06/01 17:51:42 root
36 \
SEE shows
the sourcefile
using VIEW
38 \
Revision 1.10 1998/05/27 18:52:12 root
39 \ \
: commants
added for SEE and HELP
41 \
Revision 1.9 1998/05/16 16:19:24 root
42 \ direct
terminfo access
44 \
Revision 1.8 1998/05/09 21:47:05 root
45 \ moved some
words to flktools
.fs
47 \
Revision 1.7 1998/05/03 12:06:37 root
50 \
Revision 1.6 1998/05/01 18:11:25 root
51 \
GNU license
text added
54 \
Revision 1.5 1998/04/30 09:42:25 root
57 \
Revision 1.4 1998/04/24 20:23:34 root
59 \ char
>digit
case insensitiv
61 \
Revision 1.3 1998/04/24 16:47:39 root
64 \
Revision 1.2 1998/04/09 11:35:03 root
65 \
FM/REM, */MOD etc
. fixed
67 \
Revision 1.1 1998/04/07 20:10:33 root
92 \
Floating point
stack pointer
.
118 \
Contains the allocator state
of the current DO or ?DO.
119 CREATE do-state
11 ALLOT
128 2DUP D0< IF DNEGATE THEN ;
140 : DECIMAL 10 BASE ! ;
144 : MOVE ( from
to len
-- )
146 -ROT 2DUP U< IF ROT CMOVE> ELSE ROT CMOVE THEN ;
149 : -TRAILING ( c
-addr u1
-- c
-addr u2
)
155 BL > IF \ c
-addr u1
-1
165 : /STRING ( a1 n1 n2
-- a2 n3
)
177 \
See standard. Taken from gforth without
inspection.
178 : M*/ ( d1 n2 u3
-- dqout
)
181 S>D R> XOR R> SWAP >R >R DABS ROT TUCK UM* 2SWAP UM*
182 SWAP >R 0 D+ R> -ROT R@ UM/MOD -ROT R> UM/MOD NIP SWAP
185 \
See standard. Taken from
FPC with little
inspection.
186 : FM/MOD ( d1 n1
-- n2 n3
)
188 DUP >R ABS \
Save n1 and make
it +ve
.
189 ROT ROT DUP >R DABS \
Save d1 and make
it +ve
.
190 ROT UM/MOD \
-- n2 n3
)
191 R> R@ SignsDiffer? IF \
If the signs of d1 & n1 differ
...
192 OVER IF \
if the remainder n2
<> 0 ...
193 1+ \ increment
the quotient
n3.
194 R@ ABS ROT - SWAP \ n2
= n2
- n1
198 R> 0< IF \
If n1 -ve
...
199 SWAP NEGATE SWAP \ n2
= -n2
204 : */MOD ( n1 n2
n3 -- n4 n5
) -ROT M* ROT SM/REM ;
208 : */ ( n1 n2
n3 -- n4
) */MOD SWAP DROP ;
212 : /MOD ( n1 n2
-- n3 n4
) SWAP S>D ROT SM/REM ;
216 : WITHIN ( x lo hi
-- flag
)
218 2DUP < INVERT IF SWAP THEN \ x lo hi
225 \
Convert a lower
case letter to upper
case.
226 : >UPPER ( char
-- char2
)
232 \
Case insensitive compare
.
233 : CAPS-COMPARE ( c
-addr1 u1 c
-addr2 u2
-- n
)
235 ROT 2DUP MIN \ c1 c2 u2 u1 um
238 2DUP C@ >UPPER SWAP C@ >UPPER \ c1 c2 ch2 ch1
240 UNLOOP 2R> 2DROP 2DROP
244 UNLOOP 2R> 2DROP 2DROP 1 EXIT
262 : COMPARE ( c
-addr1 u1 c
-addr2 u2
-- n
)
264 ROT 2DUP MIN \ c1 c2 u2 u1 um
267 2DUP C@ SWAP C@ \ c1 c2 ch2 ch1
269 UNLOOP 2R> 2DROP 2DROP
273 UNLOOP 2R> 2DROP 2DROP 1 EXIT
290 \
Multiply a double
by a single
returning a double
. The unused third cell
is
291 \ assumed
to be zero.
292 : UMD* ( ud1l
ud1h u
-- ud2
)
295 UM* \ ud1l
u ud2hl
ud2hh
298 UM* \ ud2hl
ud2lh ud2ll
302 \
Convert a character
using the given base
and return
a failure
flag.
303 : (char
>digit
) ( char base
-- digit
FALSE | TRUE )
305 DUP 10 > IF ( hex etc
. ) \ char base
306 OVER 48 58 WITHIN IF \ char base
309 OVER 65 55 \ c b c
65 55
310 FLOCK + WITHIN IF \ c b
313 OVER 97 87 FLOCK + WITHIN IF \ char base
320 ELSE ( decimal
or less
) \ char base
321 OVER 48 DUP \ c b c
48 48
322 FLOCK + WITHIN IF \ c b
329 \
Convert a character
with the current base
.
330 : char
>digit
BASE @ (char
>digit
) ;
333 : >NUMBER ( ud1 c
-addr1 u1
-- ud2 c
-addr2 u2
)
338 OVER C@ \ ud1 c1 u1 char
340 char
>digit
?EXIT \ ud1 c1 u1 digit
341 -ROT \ ud1 digit
c1 u1
354 0 0 2SWAP >NUMBER DROP ;
361 \
Same as UNUSED but
for code
area.
364 CHERE-LIMIT CHERE - ;
366 #PAD-LEN CONSTANT #PAD-LEN
369 : PAD HERE-LIMIT #PAD-LEN - ;
372 CREATE (SEARCH-RESET-TABLE) 256 CELLS ALLOT
374 \
Initialize the string search table
from the given string.
375 : (setup
-search
-table
) ( caddr
u -- )
377 (SEARCH-RESET-TABLE) 256 CELLS -1 FILL ( fill
reset table
with -1 )
380 CELLS (SEARCH-RESET-TABLE) + \ c2 addr
381 DUP @ -1 = IF \ c2 addr
389 0 VALUE (search
-addr
)
392 \
Boyer/Moore search comparator
word
393 : (search-comp
) ( pa pl
sa -- offs
char FALSE / TRUE )
397 -ROT TUCK \
sae pl pa
pl
398 1- + SWAP \
sae pae pl
401 -ROT 2DUP C@ SWAP C@ \
pl sae pae pc sc
402 TUCK <> IF \
pl sae pae sc
405 DROP CHAR- SWAP CHAR- SWAP \
pl sae+1 pae+1
408 UNTIL 2DROP DROP TRUE
412 : SEARCH ( c
-addr1 u1 c
-addr2 u2
-- c
-addr3 u3
flag )
414 PLUCK OVER OR 0= \
c1 u1 c2 u2 empty
?
415 IF 2DROP FALSE EXIT THEN
416 2DUP (setup
-search-table
)
417 2OVER TO (search-len
)
418 TO (search-addr
) 2SWAP \ c2 u2
c1 u1
420 PLUCK OVER \ c2 u2
c1 u1 u2 u1
423 2OVER FLOCK \ c2 u2
c1 u1 c2 u2
c1
424 (search-comp
) \ c2 u2
c1 u1
( offs
char FALSE / TRUE )
426 2SWAP 2DROP TRUE EXIT
427 THEN \ c2 u2
c1 u1 offs
char
428 CELLS (SEARCH-RESET-TABLE) + @
433 (search-addr
) (search-len
) FALSE ;
435 \
Transfer n items
and n itself
to the return stack. An inlined version must
436 \ not
save the top of return stack.
437 : n
>R ( ... n
-- ) ( r
: -- ... n
)
440 OVER BEGIN \
... n ret
cnt
442 WHILE \
... x n ret
cnt
449 \
Transfer n items
from the return to the data stack. An inlined version must
450 \ not
save the top of return stack.
451 : nR
> ( -- ... n
) ( r
: ... n
-- )
458 R> -TURN \ x ret
n cnt
463 \
The xt of the last defined
word