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


Groups > comp.lang.forth > #42654 > unrolled thread

alternative implementation of `??`

Started byMarcos Cruz <use_the_contact_page@programandala.net>
First post2015-10-16 12:34 +0000
Last post2015-10-19 20:21 +0000
Articles 3 — 2 participants

Back to article view | Back to comp.lang.forth


Contents

  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

#42654 — alternative implementation of `??`

FromMarcos Cruz <use_the_contact_page@programandala.net>
Date2015-10-16 12:34 +0000
Subjectalternative implementation of `??`
Message-ID<mvqqs2$n2r$1@speranza.aioe.org>
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
  
: ??  ( Compilation: "name" -- ) ( Runtime: f -- )
  postpone if
  bl word count evaluate
  postpone then
  ; immediate
---- 

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

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.

Of course `compile-only` or similar is a good idea:

----
: (??)  ( f -- )  0= if  r> cell+ >r  then  ;
: ??  ( -- )  ?compiling postpone (??)  ; immediate
----

Now I feel `??` fully deserves its place among the optional control
structures of the system, not being kind of syntactic sugar anymore. :)

-- 
Marcos Cruz
http://programandala.net

[toc] | [next] | [standalone]


#42701

Fromanton@mips.complang.tuwien.ac.at (Anton Ertl)
Date2015-10-19 09:29 +0000
Message-ID<2015Oct19.112941@mips.complang.tuwien.ac.at>
In reply to#42654
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/

[toc] | [prev] | [next] | [standalone]


#42709

FromMarcos Cruz <use_the_contact_page@programandala.net>
Date2015-10-19 20:21 +0000
Message-ID<n03jcr$ql0$1@speranza.aioe.org>
In reply to#42701
En/Je/On 2015-10-19, Anton Ertl <anton@mips.complang.tuwien.ac.at>
escribió/skribis/wrote:

>>----
>>: ??  ( 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

Thank you for pointing out. This new version (for my system) works with
immediate and parsing words as you say:

----
: ??  ( Compilation: "name" -- ) ( Runtime: f -- )
  postpone if
  parse-name find-name  ( x 0 | cfa 1 | cfa -1 )
  dup 0= -13 ?throw
  1 = if  execute  else  compile,  then
  postpone then
  ; immediate
----

>>----
>>: ??  ( f -- )  0= if  r> cell+ >r  then  ;
>>----
>
> This requires a classical threaded-code implementation.

Yes, that's what it's for.

>>----
>>: (??)  ( 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).

Of course. `compile-only` is on the to-do list... Meanwhile, some words
keep those checks.

-- 
Marcos Cruz
http://programandala.net

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.forth


csiph-web