home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.forth      Forth programmers eat a lot of Bratwurst      117,927 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 117,652 of 117,927   
   Gerry Jackson to dxf   
   Re: Conditional compilation   
   30 Oct 25 16:24:27   
   
   From: do-not-use@swldwa.uk   
      
   On 26/10/2025 23:54, dxf wrote:   
   > On 27/10/2025 1:10 am, Gerry Jackson wrote:   
   >> ...   
   >> Also:   
   >>   
   >> [else] can be used as a   
   >> multi-line comment [then]   
   >   
   > SYNONYM [COMMENT] [ELSE]   
   >   
   Yes but, unfortunately the matching   
   SYNONYM [END-COMMENT] [THEN]   
   to make it more readable and meaningful, won't work with the reference   
   implementation in the ANS Forth standard because string comparisons are   
   carried out when looking for [THEN].   
      
   However with the latest reference implementation of [IF] [ELSE] [THEN]   
   see https://forth-standard.org/standard/tools/BracketELSE   
   A similar synonym for [END-COMMENT] can work   
      
   At that link scroll down to ruv's suggested reference implementation   
   that I've only recently just discovered and which, lower down at that   
   link, has apparently been accepted into the next standard that   
   supersedes Forth 2012.   
      
   The way to do it exploits a feature of [ELSE] that is true for both   
   implementations of [IF] [THEN] [ELSE].   
      
   If the following is typed into the Forth Interpreter   
   [ELSE] 1 [ELSE] 2 [ELSE] 3 [ELSE] 4 [ELSE] 5 [THEN] .S   
   it will display 2 4   
   The same bizarre behaviour is also true for ELSE and is well known I   
   think, but may not be for [ELSE]   
   i.e. the 1 3 and 5 are skipped   
      
   Hence [ELSE] ... [ELSE] can be used as a multi-line comment and the   
   synonyms are:   
      
   synonym [COMMENT] [ELSE]   
   synonym [END-COMMENT] [ELSE]   
      
   This works for both implentations of [IF] [THEN] [ELSE]   
   These synonyms assume that [COMMENT] and [END-COMMENT] inherit   
   the immediacy of [ELSE] and [THEN] so colon definitions may be   
   better   
      
   To demonstrate this the new reference implementation is repeated below   
   with additional code inserted and marked with ***   
      
   \ ---------------   
      
   wordlist dup constant bracket-flow-wl  get-current swap set-current   
      
   : [if]   ( level1 -- level2 ) 1+ ;   
   : [else] ( level1 -- level2 ) dup 1 = if 1- then ;   
   : [then] ( level1 -- level2 ) 1- ;   
      
   \ Note: the [else] above is only executed by the next definition of   
   \ [ELSE] in the Forth wordlist. At that point level1 = 1 so it is   
   \ not DUPing an empty stack. This [ELSE] returns 0 to the Forth WL   
   \ [ELSE] causing it to terminate.   
      
   get-order bracket-flow-wl swap 1+ set-order   \ ***   
   synonym [end-comment] [else]                  \ ***   
   : \ postpone \  ;                             \ *** See note at end   
   previous                                      \ ***   
   set-current   
      
   : [else] ( -- )   
       1 begin begin parse-name dup while   
          bracket-flow-wl search-wordlist if   
             execute dup 0= if drop exit then   
          then   
       repeat 2drop refill 0= until drop   
    immediate   
      
   : [then] ( -- ) ; immediate   
      
   : [if] ( flag -- ) 0= if postpone [else] then ; immediate   
      
   synonym [comment] [else]        \ ***   
      
   \ --------------------   
   cr .( Tests of [comment] and [end-comment] )   
      
   [comment]   
   This is a demonstration of   
   a multi-line comment   
      
   which may have blank lines or Forth words in it   
   but beware of using bare '[if]' '[else]' or '[then]'   
   as they may be executed and may cause you some grief.   
   "[comment]" can not be nested   
   [end-comment]   
      
   : foo 1234   
   [comment] testing immediacy of "[comment]" and "[end-comment]"   
   [end-comment]   
      2* cr .   
      
   foo     \ displays 2468   
      
   cr .( End of tests)   
   \ ------------------------   
      
   Different names can be synonymised with [ELSE] instead of [COMMENT] and   
   [END-COMMENT] which are just used for illustration. Surprisingly (to me   
   at least) the existing multi-line comment using parentheses ( and ) can   
   be synonyms of [ELSE]. The converse isn't true as [ELSE] has more   
   functionality.   
      
   Of course the standard single line comments, \ and ( , still have their   
   place   
      
   Note the the re-definition of \ in the bracket-flow-wl wordlist above   
   will prevent [IF] etc being executed in single line \ comments   
      
   --   
   Gerry   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca