With half decent string handling like in lina it is one-cent-flute to
add Ruby inspired, #-formatted strings to Forth.
The brilliant idea is to use # to introduce a Forth word.
This is for the beta version of lina64 with nosecurity.
Your professional Forth probably has no security :-) then
don't bother.
-------------8<---------------------------------------------
WANT 2>R
WANT NO-SECURITY
WANT DH.
NO-SECURITY
\ Basically an alias. Linux 64 has trouble printing strings from
\ $8000,0000,0000,0000 and higher.
: $. PAD $! PAD $@ TYPE ;
\ Print the first part of STRING, up till format sign, leave REMAINDER.
: plain &# $/ $. ;
\ Print X using the first word of STRING, up till blank, leave REMAINDER.
: format BL $/ 2SWAP 2>R EVALUATE 2R> ;
\ Print X1 .. Xn using the format STRING.
: .format BEGIN plain OVER WHILE format OVER WHILE REPEAT THEN 2DROP ;
12345.6788 "ROME" "we go to #$. with #D. cows #CR" .format
: doit
12345.6788 "ROME" "we go to #$. with $#DH. cows #CR" .format
;
: english "we go to #$. with $#DH. cows#CR" ;
: spanish "vamos a #$. con $#DH. vacas#CR" ;
: test 1 ARG[] EVALUATE 0 DO doit LOOP
CR CR
12345.6788 "ROME" 2 ARG[] EVALUATE IF english ELSE spanish THEN .format
;
-------------8<---------------------------------------------
lina64 -c format.frt
printed during compilation
we go to ROME with 123456788 cows
format 5 0 results in:
we go to ROME with $0000,0000,0000,0000,0000,0000,075B,CD14cows
we go to ROME with $0000,0000,0000,0000,0000,0000,075B,CD14cows
we go to ROME with $0000,0000,0000,0000,0000,0000,075B,CD14cows
we go to ROME with $0000,0000,0000,0000,0000,0000,075B,CD14cows
we go to ROME with $0000,0000,0000,0000,0000,0000,075B,CD14cows
vamos a ROME con $0000,0000,0000,0000,0000,0000,075B,CD14 vacas
format 5 1 results in:
we go to ROME with $0000,0000,0000,0000,0000,0000,075B,CD14cows
we go to ROME with $0000,0000,0000,0000,0000,0000,075B,CD14cows
we go to ROME with $0000,0000,0000,0000,0000,0000,075B,CD14cows
we go to ROME with $0000,0000,0000,0000,0000,0000,075B,CD14cows
we go to ROME with $0000,0000,0000,0000,0000,0000,075B,CD14cows
we go to ROME with $0000,0000,0000,0000,0000,0000,075B,CD14cows
If you prefer one liners.
\ Print X1 .. Xn using the format STRING.
: .format BEGIN &# $/ $. OVER WHILE BL $/ 2SWAP 2>R EVALUATE 2R>
OVER WHILE REPEAT THEN 2DROP ;
String-slash $/ has been introduced many times. You can steal it from
ciforth, there is a reference implementation in the library forth.lab.
Beware for counterfeit. If you use your garden variety string SPLIT, you
will be in for nasty surprises w.r.t. empty strings.
Groetjes Albert
Hugh, chalk me up for posting real Forth code.
--
--
Albert van der Horst, UTRECHT,THE NETHERLANDS
Economic growth -- being exponential -- ultimately falters.
albert@spe&ar&
c.xs4all.nl &=n
http://home.hccnet.nl/a.w.m.van.der.horst