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


Groups > comp.lang.forth > #42701

Re: alternative implementation of `??`

From anton@mips.complang.tuwien.ac.at (Anton Ertl)
Newsgroups comp.lang.forth
Subject Re: alternative implementation of `??`
Date 2015-10-19 09:29 +0000
Organization Institut fuer Computersprachen, Technische Universitaet Wien
Message-ID <2015Oct19.112941@mips.complang.tuwien.ac.at> (permalink)
References <mvqqs2$n2r$1@speranza.aioe.org>

Show all headers | View raw


Marcos Cruz <use_the_contact_page@programandala.net> writes:
>In order to implement Wil Baden's `??` in a new 8-bit Forth system for
>Z80 I've written an alternative implementation that I'd like to share.
>
>The original code by Wil Baden was written two ways:
>
>---- 
>: ??  ( Compilation: "name" -- ) ( Runtime: f -- )
>  s" if" evaluate
>  bl word count evaluate
>  s" then" evaluate
>  ; immediate

This one is bad, because there is no guarantee that IF and THEN are in
the search order and have the intended meaning.

>: ??  ( Compilation: "name" -- ) ( Runtime: f -- )
>  postpone if
>  bl word count evaluate
>  postpone then
>  ; immediate

This one is ok, because the EVALUATE evaluates "name" in exactly the
context in which it appears.  Of course, in Forth-2012 you can write
PARSE-NAME instead of BL WORD COUNT.

>I don't like compiling with `evaluate` without a good reason, but anyway
>I have not yet implemented it in my system,  so first I wrote this
>alternative:
>
>----
>: ??  ( Compilation: "name" -- ) ( Runtime: f -- )
>  postpone if  ' compile,  postpone then
>  ; immediate
>----

This one does not deal with immediate words as intended.  E.g., in
Gforth,

?? LEAVE

would not work as intended.

However, since you actually want to execute the compilation semantics,
you can write the following in Gforth to express this:

: ??  ( Compilation: "name" -- ) ( Runtime: f -- )
  postpone if comp' execute postpone then ; immediate

And the cool thing is that this works even for parsing words:

: foo ?? ." bla bla" ;
true foo  \ prints "bla bla"
false foo \ prints nothing

The EVALUATE-based version above does not, because the EVALUATEd
string does not include the additional parsed string.

>But then I realized there's no need to compile an `if` every time,
>wasting precious memory in an 8-bit system, so I wrote a simpler version
>with a different approach, though not so portable:
>
>----
>: ??  ( f -- )  0= if  r> cell+ >r  then  ;
>----
>
>A difference is immediate words must be "postponed" explicitly after
>`??`, but I find this even more "natural" than the original behaviour.

This requires a classical threaded-code implementation.  It does not
even work with the threaded-code-based Gforth except if "name" is a
primitive.

>Of course `compile-only` or similar is a good idea:
>
>----
>: (??)  ( f -- )  0= if  r> cell+ >r  then  ;
>: ??  ( -- )  ?compiling postpone (??)  ; immediate

Why spoil an ok word with STATE-smartness?  If you want it to be
compile-only, implement a proper COMPILE-ONLY in the system (where the
text-interpreter checks, not the word).

- 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 2015: http://www.rigwit.co.uk/EuroForth2015/

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


Thread

alternative implementation of `??` Marcos Cruz <use_the_contact_page@programandala.net> - 2015-10-16 12:34 +0000
  Re: alternative implementation of `??` anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2015-10-19 09:29 +0000
    Re: alternative implementation of `??` Marcos Cruz <use_the_contact_page@programandala.net> - 2015-10-19 20:21 +0000

csiph-web