Rocksolid Light

Welcome to Rocksolid Light

mail  files  register  newsreader  groups  login

Message-ID:  

The trouble with computers is that they do what you tell them, not what you want. -- D. Cohen


devel / comp.lang.forth / Interpretive if-then

SubjectAuthor
o Interpretive if-thenRuvim

1
Interpretive if-then

<ut0s7o$25hll$1@dont-email.me>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=26403&group=comp.lang.forth#26403

  copy link   Newsgroups: comp.lang.forth
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: ruvim.pinka@gmail.com (Ruvim)
Newsgroups: comp.lang.forth
Subject: Interpretive if-then
Date: Fri, 15 Mar 2024 11:09:45 +0400
Organization: A noiseless patient Spider
Lines: 80
Message-ID: <ut0s7o$25hll$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 15 Mar 2024 07:09:44 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="e2d6d734356c505825ad13d06f782bb5";
logging-data="2279093"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/12mEFB0dG9tuIOg4JfRzv"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:kyt8WHgttzjEtrNyYwW3HapABKY=
Content-Language: en-US
 by: Ruvim - Fri, 15 Mar 2024 07:09 UTC

For interactive testing a definition body, it's convenient to have
appropriate interpretation semantics for all used words.

Recently I came up with an idea how to easily implement such semantics
for the if-then construct. A usual problem is that it's difficult to
correctly skip the false branch in all edge cases [1,2].

My idea is that the false branch is compiled into an unnamed definition
and then discarded. Therefore, such if-then can contain only phrases
that are usable regardless of the state.

An implementation (PoC) is following.

===== begin of "cond-interpretive.fth"

[undefined] compilation [if]
: compilation ( -- flag ) state @ 0<> ;
[then]

[undefined] exch-current [if]
: exch-current ( wid1 -- wid2 )
get-current swap set-current
; [then]

[undefined] >order [if]
: >order ( wid -- ) >r get-order r> swap 1+ set-order ;
[then]
[undefined] order> [if]
: order> ( -- wid ) get-order swap >r 1- set-order r> ;
[then]

wordlist constant cond-interpretive
wordlist constant cond-interpretive-private

cond-interpretive-private dup >order exch-current

0 value n \ nesting level of "if"
: inc-n ( -- ) 1 n + to n ;
: dec-n ( -- ) -1 n + to n ;
: ?n0 ( -- ) n if 0 to n -22 throw then ;

cond-interpretive set-current
\ NB: now the wordlist "cond-interpretive" is compilation word list,
\ and it's absent in the search order.

: if ( x -- | -- ) ( C: -- | -- orig )
compilation if inc-n postpone if exit then
?n0 ( x ) if exit then :noname
; immediate

: else ( orig1 -- orig2 )
compilation if
n if postpone else exit then
postpone ; drop exit
then ?n0 :noname
; immediate

: then ( orig -- )
compilation if
n if dec-n postpone then exit then
postpone ; drop exit
then ?n0
; immediate

order> drop exch-current >order
\ NB: now the wordlist "cond-interpretive" is in the search order top.

===== end of "cond-interpretive.fth"

[1] https://forth-standard.org/standard/tools/BracketELSE#contribution-74
[2] https://github.com/ForthHub/discussion/discussions/143

--
Ruvim

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor