home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Power-Programmierung
/
CD1.mdf
/
forth
/
compiler
/
fpc
/
source
/
tforth.seq
< prev
next >
Wrap
Text File
|
1990-04-24
|
9KB
|
258 lines
\ FRTH.SEQ Interpretive Forth Experiment by Tom Zimmer
0 value headseg
handle headhndl
0 value headlen
0 value headdp
0 value wordcnt
: read_1line ( a1 n1 -- )
headseg -rot ?ds: tib rot 1- dup #tib ! cmovel >in off
?ds: sseg !
bl word number? nip 0=
if drop
0 headseg headdp !L exit
then
headseg headdp !L
2 +!> headdp
bl word 1+ c@ 128 and headseg headdp c!L
incr> headdp
here 1+ dup c@ 127 and swap c!
?ds: here dup c@ 1+ headseg headdp rot cmovel
here c@ 1+ +!> headdp ;
: read_symtbl ( -- )
$1000 alloc 8 =
abort" Could not allocate space for symbols" nip =: headseg
me@ me$ headhndl $>handle
" SYM" ">$ headhndl $>ext
headhndl hopen
if cr ." Couldn't open symbol file "
headhndl count type abort
then
0 $FFF0 headhndl headseg exhread =: headlen
off> headdp
save> base hex
0 headlen
begin headseg sseg !
2dup $0A scan 2dup 2>r nip - dup
while read_1line
2r> 1 /string
repeat 2drop 2r> 2drop
restore> base
?ds: sseg ! ;
: FIND ( adr -- cfa flag )
dup c@ 0= if false exit then
headseg save!> sseg
>r 0
begin headseg over 2dup @L 0<> >r
3 + 1 rpick swap over c@ 1+ compare r> and
while over 3 + c@L 4 + +
repeat swap 2dup @L ?dup
if -rot 2+ c@L ?dup 0= if 1 then
else 2drop here false
then r>drop
restore> sseg ;
: DEFINED ( -- here 0 | cfa [ -1 | 1 ] )
BL WORD ?UPPERCASE FIND ;
: ltype ( seg a1 n1 -- )
bounds
?do dup i c@L emit
?keypause
loop drop ;
: .1name ( a1 -- )
save> base hex
>r
headseg r@ @L 4 u.r space
headseg r@ 4 + headseg r> 3 + c@L ltype tab ?cr
restore> base ;
: words ( -- )
cr
off> wordcnt
20 save!> tabsize
65 save!> rmargin
0 >r
begin headseg r@ @L 0<>
while r@ .1name
incr> wordcnt
r> headseg over 3 + c@L 4 + + >r
repeat r>drop
restore> rmargin
restore> tabsize
cr wordcnt . ." Total words " ;
\ These seemingly silly definitions, make library macros available to
\ the interpretive Forth environment.
\ While these are techically re-definitions, any references to these words
\ either before they are defined, or after, will still use the library
\ defined macro.
: @ ( a1 -- n1 ) @ ;
: ! ( n1 a1 -- ) ! ;
: C@ ( a1 -- n1 ) C@ ;
: C! ( n1 a1 -- ) C! ;
: @-T ( a1 -- n1 ) ?cs: swap @L ;
: !-T ( n1 a1 -- ) ?cs: swap !L ;
: C@-T ( a1 -- n1 ) ?cs: swap C@L ;
: C!-T ( n1 a1 -- ) ?cs: swap C!L ;
: DP ( -- a1 ) DP ;
: HERE ( -- a1 ) DP @ ;
: DECIMAL ( -- ) DECIMAL ;
: HEX ( -- ) HEX ;
: DUP ( ? ) DUP ;
: DROP ( ? ) DROP ;
: OVER ( ? ) OVER ;
: SWAP ( ? ) SWAP ;
: 2DROP ( ? ) 2DROP ;
: EXECUTE ( N1 -- ) EXECUTE ;
: ?CS: ( ? ) ?CS: ;
: ?DS: ( ? ) ?DS: ;
: TIB ( ? ) TIB ;
: + ( ? ) + ;
: - ( ? ) - ;
: * ( ? ) * ;
: / ( ? ) / ;
: MOD ( ? ) MOD ;
: +! ( ? ) +! ;
: 0= ( ? ) 0= ;
: 1+ ( ? ) 1+ ;
: 2+ ( ? ) 2+ ;
: INCR ( ? ) INCR ;
: DECR ( ? ) DECR ;
: RP@ ( ? ) RP@ 2+ ;
: SP@ ( ? ) SP@ ;
: ?do_execute ( n1 f1 -- )
128 =
if execute
then ;
: ?missing ( F1 -- )
if here count type true abort" ?"
then ;
: .depth ( -- )
depth 10 umin 0
?do ." ." loop ;
: tnumber? ( a1 -- d1 f1 )
dup 1+ c@ '$' = \ if first char is a $
if save> base hex
dup>r count 1- over c! \ remove the $ symbol
number? \ attempt number conversion
'$' r> 1+ c! \ restore the $ symbol
restore> base
else number?
then ;
: number ( a1 -- n1 )
tnumber? nip 0= ?missing ;
: dummy ( -- ) \ make sure a bunch of words are
exit \ included in target
u. .r h. h.r dump ;
: ?stack ( -- )
depth 0< abort" Stack Underflow!" ;
: interpret ( -- )
begin ?stack defined here c@
while ?dup
if ?do_execute
else number
then
repeat 2drop ;
: ' ( | <name> -- a1 )
defined 0= ?missing ;
: >name ( adr -- adr2 )
0 >r
begin headseg r@ @L over <>
headseg r@ @L 0<> and
while r> headseg over 3 + c@L 4 + + >r
repeat drop r> headseg over @L 0=
if drop false
then ;
: .id ( a1 -- )
dup 0= if drop exit then
headseg swap 3 + 2dup c@L ?ds: pad rot 1+ cmovel
pad count type space ;
?DIS #IF
: dis.symbol ( a1 -- )
dup >name ?dup
if .id
else dup H.
then drop ;
32 array disname
: dis?symbol ( a1 -- <a2 n1> f1 )
>name dup
if headseg swap 3 + 2dup c@L ?ds: disname rot 1+ cmovel
disname count true
then ;
: see ( | <name> -- )
['] dis.symbol is .symbol
['] dis?symbol is ?symbol
' dis ;
: dis ( a1 -- )
['] dis.symbol is .symbol
['] dis?symbol is ?symbol
dis ;
#ENDIF
?DBG #IF
: dbg ( | <name> -- )
['] dis.symbol is .symbol
['] dis?symbol is ?symbol
' $trace ;
: debug ( | <name> -- )
['] dis.symbol is .symbol
['] dis?symbol is ?symbol
['] interpret =: interp
' $breakat ;
: $trace ( a1 -- )
['] dis.symbol is .symbol
['] dis?symbol is ?symbol
$trace ;
: $breakat ( a1 -- )
['] dis.symbol is .symbol
['] dis?symbol is ?symbol
['] interpret =: interp
$breakat ;
#ENDIF
: QUIT ( -- )
sp0 @ sp! \ reset data stack
tib0 @ 'tib ! \ reset TIB
begin rp0 @ rp! \ reset return stack
cr query space interpret ." ok " .depth
again ;
: cold ( -- )
." 80x86 Forth environment for TCOM "
0 =: abort_func read_symtbl \ error here, does BYE
['] quit is abort_func \ don't leave on error
quit
;