Menu

Nested ' fails

2024-07-26
2024-07-27
  • David C. Norris

    David C. Norris - 2024-07-26

    Here's a short repro for a problem I'm now encountering with the circular buffer I posted a few days ago. Whereas the words toX and toY below work as expected interactively, using them inside another word fails. Have I misunderstand something important about '?

    marker -repro
    decimal ram
    
    : point: ( "name" x y -- ) ram create swap , ( x) , ( y) ;
    : xy. ( a-addr -- ) @+ . @ . ;
    
    : toX ( "name" x -- ) ' execute ! ;
    : toY ( "name" y -- ) ' execute 2+ ! ;
    
    2 3 point: A
    A xy.
    4 toX A
    5 toY A
    A xy.
    
    : fine ( x -- ) A ! A xy. ;
    7 fine
    
    : hmm ( x -- ) toX A ;
    \ FF replies '?' to the following:
    8 hmm
    
    -repro
    

    I'm doing this on PIC24.

     
  • Mikael Nordman

    Mikael Nordman - 2024-07-26

    In hmm you would need toX to use ['] for it to work. ' is not state smart.
    Something like this might work.

    : toX state if postpone ['] else ' then execute  ! ; immediate
    
     
    👍
    1
  • Mikael Nordman

    Mikael Nordman - 2024-07-26

    I tend to avoid words that use TICK and have to interpret. So I would do

    4 X A !  \ where X is the index 0
    4 A X!   \ Where X! stores to the index offset
    

    There are also wordsets for structs, but I have not used or defined them for FF.

     
    👍
    1
  • David C. Norris

    David C. Norris - 2024-07-27

    Thank you, Mikael. Appreciating this as a question of state-smartness is most helpful. This looks like an excellent opportunity for me to read Ertl's article, State-smartness — Why it is Evil and How to Exorcise it, cited at the bottom of https://gforth.org/manual/Combined-words.html.

    Although the above repro aimed to crystallize the issue into as simple a package as possible, it may have obscured my underlying motivation a bit. My actual trouble is with the INTO word of abovementioned circular buffer, which I would have hoped to use in the same manner as TO. (Is TO itself state-smart?)

    The Ertl article looks pretty challenging to me, but already I see that I've stumbled on this state-smartness issue in 'the usual way'. In §4.3, he says,

    The most frequent reason for wanting a combined word is parsing words : words that read the input stream.

    A bit earlier, Ertl says,

    The general solution is to provide two words: one for the interpretation semantics and one for the compilation semantics. Examples: ' and ['], char and [char], and s"-int and s"-comp.

    So I could perhaps implement as well an [INTO] word for use during compilation, but that destroys the simplicity of aligning with TO, which needs no companion [TO].

    Another solution might be, if I have defined a CIRCBUF: MYBUF, to define also a word !MYBUF following the same, standard naming convention as your !X above.

    Still, the dangers Ertl highlights (that users might someday TICK or POSTPONE a state-smart word) seems somehow unlikely in the case of INTO. But then again, my first thought upon initial encounter with the concept of state-smartness was, "Oh, I would never write such a word!"


    Addendum: Ha! Looking again at that §6.11.1 of the GForth manual, I see the following:

    This feature was introduced for implementing TO and S". I recommend that you do not define such words, as cute as they may be: they make it hard to get at both parts of the word in some contexts.

    So the design I'll pursue now will require defining a !MYBUF word.

     

    Last edit: David C. Norris 2024-07-27
  • Mikael Nordman

    Mikael Nordman - 2024-07-27

    I usually would write

    3 7 circbuf: mybuffer
    mybuffer >circbuf
    

    when I put data into a circular buffer or fifo, as can be seen from the CQ: character fifos in FF PIC24. The CQ: fifos are used by the UART RX and UART buffered TX.

    In order to survive a restart CIRCBUF: should store persistent data ( like size) in flash and variable data (like current index) in ram.

     
    ❤️
    1

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.