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


Groups > comp.lang.forth > #17694

Re: Forth 200X words S\" and SUBSTITUTE

From anton@mips.complang.tuwien.ac.at (Anton Ertl)
Newsgroups comp.lang.forth
Subject Re: Forth 200X words S\" and SUBSTITUTE
Date 2012-11-29 13:57 +0000
Organization Institut fuer Computersprachen, Technische Universitaet Wien
Message-ID <2012Nov29.145701@mips.complang.tuwien.ac.at> (permalink)
References <k95rit$jp3$1@dont-email.me>

Show all headers | View raw


Gerry Jackson <gerry@jackson9000.fsnet.co.uk> writes:
>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

What is the search order for the replacements?

Another alternative for this kind of stuff is >STRING-EXECUTE:

: >string-execute ( ... xt -- ... addr u )
    \G execute xt while the standard output (TYPE, EMIT, and everything
    \G that uses them) is redirected to a string.  The resulting string
    \G is addr u, which is in ALLOCATEd memory; it is the
    \G responsibility of the caller of >STRING-EXECUTE to FREE this
    \G string.

Your example could be replaced with

:noname ." blah blah " foo type ." blah" ; >string-execute

>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

This has the problem of most EVALUATE-based approaches: It binds ":"
at run-time, so it requires ":" to be visible at run-time and be the
same ":" as you intended, contrary to the usual Forth approach of
early binding.  EXECUTE-PARSING avoids this problem by passing an xt,
and the name->xt binding can (and usually does) happen early.

>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
>;

Again, this relies on EXECUTE, S", and EVALUATE being visible and the
intended one at run-time, if I understand this correctly.  Take a look
at the lengths that I went to in the Forth-94 implemenation of
EXECUTE-PARSING to ensure that the one word it binds at run-time is
indeed the word I intend to use.

>Which is not be very readable but is far more concise than the usual 
>definition.

Yes, if we skip correctness in corner cases, the Forth-94 definition
will also be more concise.

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

From a later message I gather that you want to refer to non-ASCII
Unicode characters by name.  I think a good way to refer to Unicode
characters is by just putting them in as literal characters.  I.e., if
you want to have "a" in the string, just put an "a" there, not
"\{latin-a}" or whatever; with xchars, you can do it with Unicode
characters, too.  Of course, it requires you to use an editor that can
grok and display Unicode, but that's not a big issue these days.

- anton
-- 
M. Anton Ertl  http://www.complang.tuwien.ac.at/anton/home.html
comp.lang.forth FAQs: http://www.complang.tuwien.ac.at/forth/faq/toc.html
     New standard: http://www.forth200x.org/forth200x.html
   EuroForth 2012: http://www.euroforth.org/ef12/

Back to comp.lang.forth | Previous | NextPrevious in thread | Next 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