Menu

Case structure

Gary Luke
2024-02-29
2024-03-01
  • Gary Luke

    Gary Luke - 2024-02-29

    Does anyone have an example of a case flow control structure using FF on Scamp3 or any other implementation. A series of dup if then could do it but don't know which runs better.

     
  • Fred Zelders

    Fred Zelders - 2024-02-29

    Here is the build of a case flow I use. I had to modify it slightly in order to use the for ... next construct.

    \ ***********
    \ Case for FlashForth *
    \ Filename: build-case.ff (voorheen case.txt) *
    \ Date: 26.01.2014 *
    \ FF Version: 5.0 *
    \ Copyright: Mikael Nordman *
    \ Author: Mikael Nordman edited by FZ *
    \
    **********
    \ FlashForth is licensed acording to the GNU General Public License

    \
    *************
    \ A case implementation posted by Jenny Brien on c.l.f.
    \ Modified to use for..next instead of do..loop

    -case
    marker -case
    hex ram

    \ of compare
    (of) ( n1 n2 -- n1 flag )
    inline over
    inline -
    0=
    ;
    case ( -- #of )
    0
    ; immediate
    of ( #of -- #of orig )
    postpone (of) ( copy and test case value)
    postpone if ( add orig to control flow stack )
    postpone drop ( discard case value if case is matching )
    ; immediate

    default ( #of -- #of orig )
    postpone true ( Force to take the default branch )
    postpone if ( add orig to control flow stack )
    postpone drop ( discard case value )
    ; immediate

    endof ( orig1 -- orig2 #of )
    postpone else
    swap 1+
    ; immediate

    endcase ( orig1..orign #of -- )
    postpone drop ( discard case value )
    for
    postpone then ( resolve of branches )
    next

    ; immediate

    \ example:
    \ : case-test
    \ case
    \ 11 of noop endof
    \ default endof
    \ endcase
    \ ;

     
  • Gary Luke

    Gary Luke - 2024-02-29

    Fred and Mikael, thank you both.
    As an introduction, I've returned to forth in my retirement after about a twenty year gap not touching microprocessors. Started in the mid 1980s on the Rockwell 6502 RSC chip.

     
    • Mikael Nordman

      Mikael Nordman - 2024-03-01

      Gary, FlashForth was inspired by the RSC Forth chip which I as a newly graduated BSCEE could not afford back in the eighties.
      Hope you enjoy FlashForth !

       

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.