Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.forth > #17649

Forth 200X words S\" and SUBSTITUTE

From Gerry Jackson <gerry@jackson9000.fsnet.co.uk>
Newsgroups comp.lang.forth
Subject Forth 200X words S\" and SUBSTITUTE
Date 2012-11-28 20:18 +0000
Organization A noiseless patient Spider
Message-ID <k95rit$jp3$1@dont-email.me> (permalink)

Show all headers | View raw


I've just added the Forth 200X words S\" and SUBSTITUTE to my Forth 
system with an extension that I've found useful in other contexts. This 
extension is the ability to execute a named Forth word in the string 
being processed by  S\" and SUBSTITUTE. Such Forth words return a (caddr 
u) string that is inserted into the result string. At the point the 
Forth word is executed the stack is cleared of the S\" and SUBSTITUTE 
working data so that the Forth word has access to user data on the 
stack. This provides the ability to convert numbers into particular 
formats and insert them into the resulting string.

In the case of SUBSTITUTE this requires no additional syntax, the name 
of the Forth word is bracketed by % characters. The implementation has 
to be extended to execute any Forth word, not just replacement names as 
well as ensuring the stack is relatively empty. e.g.

: foo s" bar" ;
s" blah blah %foo% blah" buf 128 substitute
will insert the string "bar" into the resulting string.

This reduces the need for the word REPLACES

Another example shows that it provides an alternative to EXECUTE-PARSING 
e.g.

: noop ( -- ) ;
: $define  ( caddr1 u1 caddr2 u2 -- )
    s" %noop% %noop%" buf 80 substitute if evaluate then
;
: $: s" :" $define ;   \ $: same as :name
s" bar" $: 456 . ;
bar
displays 456

Of course this can also be done with the standard definition of 
SUBSTITUTE, but is awkward as REPLACES has to be used.

Moving on to S\" the extension requires:
- an extension to the syntax, I use an extra escape e.g. \{foo}
- S\" must be usable in interpretation mode and at run-time
- user arguments to be made available to the called word
- preferably words affecting transient areas do not affect the S\" 
buffer and vice versa
e.g.

: foo s" xyz" ;
s\" abc\{foo}def" type
displays abcxyzdef

As with SUBSTITUTE it can be an alternative to EXECUTE-PARSING e.g.

: $define  ( caddr1 u1 caddr2 u2 -- )
    s\" s\\\" \\{noop} \\{noop}\" evaluate" evaluate
;
: $: s" :" $define ;
s" foo" $: 123 . ;

defines foo which, when executed, displays 123

It is not the intention of this post to belittle EXECUTE-PARSING, I'm 
just using examples to show the versatility of the extension. In fact 
EXECUTE-PARSING can even be defined using this extended S\" e.g.

: execute-parsing  ( ... caddr u xt -- )
    s\" -rot s\\\" execute \\{noop}\" evaluate" evaluate
;

Which is not be very readable but is far more concise than the usual 
definition. Usage e.g.

1234 s" bar" ' constant execute-parsing
bar .
displays 1234

To make the definitions like EXECUTE-PARSING more readable and easier to 
write it may be worth adding two more escapes to S\" e.g.
\[...]  to copy a literal string
\s to use the string on top of the stack, this replaces \{noop}

Then the above becomes (untested)
: execute-parsing  ( ... caddr u xt -- )
    s\" -rot \[s\" execute \s"] evaluate" evaluate
;

Other uses might be to insert UTF-8 sequences into strings

My conclusion is that trivial extensions to SUBSTITUTE and S\" make them 
far more versatile and that those to S\" probably remove the need for 
SUBSTITUTE and friends entirely.

Opinions please.

-- 
Gerry

Back to comp.lang.forth | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Forth 200X words S\" and SUBSTITUTE Gerry Jackson <gerry@jackson9000.fsnet.co.uk> - 2012-11-28 20:18 +0000
  Re: Forth 200X words S\" and SUBSTITUTE Alex McDonald <blog@rivadpm.com> - 2012-11-28 12:41 -0800
    Re: Forth 200X words S\" and SUBSTITUTE Gerry Jackson <gerry@jackson9000.fsnet.co.uk> - 2012-11-28 22:59 +0000
  Re: Forth 200X words S\" and SUBSTITUTE anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2012-11-29 13:57 +0000
    Re: Forth 200X words S\" and SUBSTITUTE Gerry Jackson <gerry@jackson9000.fsnet.co.uk> - 2012-11-29 17:12 +0000

csiph-web