albert@spenarnc.xs4all.nl wrote:
This holds Forth back. We absolutely need more than one method to get
anywhere.
A generation ago I explored using defining word to create object
with multiple methods; not an OO attempt, no class but just a useful
object. Such an object was "FPAD:" :
[r] Object Definition
fpad: ( type buffer size --) \ defining word for file pad object
\ type =0, line type
\ type =1, block type
\ buffer =0, use Pad for buffer
\ buffer !=0, use buffer
File Operations (methods)
read ( -- f) \ read to buffer
write ( --) \ write from buffer
wstr ( a u --) \ write from string
clear ( --) \ clear buffer
type ( --) \ print string
dump ( --) \ dump buffer
id ( -- u) \ file id
bfr ( -- a u) \ buffer address and count
$ ( -- a u) \ string address and count
open ( a u atr --) \ open file
stdin ( --) \ open /dev/stdin
create ( a u atr --) \ create file
close ( --) \ close file
pos ( -- du) \ position file
rpo ( du --) \ reposition file
Structure Maintenance
^ ( -- a) \ structure address
@ ( n1 -- n2) \ fetch word at structure cell n
! ( n2 n1 --) \ store word at structure cell n
? ( n1 --) \ print word at structure cell n
[r] Auxiliary words
Example offset usage:
foo ^ ^cnt ? \ Print count value
foo ^ \ returns the pfa of foo
^cnt \ adds the count offset
? \ prints the value
^cnt ( a -- a') add offset to string cnt value +0
^str ( a -- a') add offset to string address +4
^sz ( a -- a') add offset to bfr cnt value +8
^bfr ( a -- a') add offset to bfr address +12
^id ( a -- a') add offset to file id value +16
^off ( a -- a') add offset to pad offset value +20
^read ( a -- a') add offset to read xt +24
^write ( a -- a') add offset to write xt +28
^blank ( a -- a') add offset to erase xt +32
===
Such object was a defining word with case statement for methods.
Method names were just labels (an address for id only) thus
method names could be shared among objects thereby saving memory used
by method naming.
Case statement (of ... endof) contains string of code for the
method. After the case structure, the chosen method was evaluated at
compile time when the method was used. So an object with its method
was a macro that compiled code.
===
[r] Example use of FPAD:
-- -- wb1 is some work buffer-- fp1 will be a line type file object created by fpad:-- 0 wb1 wb1.len fpad: fp1 readit It's convenient to create higher level file word that need only a filepath as an argument:
: readit ( filePath --)
bl parse r/o fp1 open
begin fp1 read while
cr fp1 type
repeat
fp1 close
;
sh ls>/tmp/ipc \ system shell to create some data
readit /tmp/ipc \ Forth read the data
--.
-- me