Menu

Conditionnal compilation

PETREMANN
2020-06-23
2020-06-25
  • PETREMANN

    PETREMANN - 2020-06-23

    Hello,

    The word \ is used to mark the rest of the line as a comment.

    The idea is to create a word ?\ which acts as a comment if a false Boolean flag precedes this word. Code of this word:

    :::forth
    : ?\  ( fl --- )
      invert
      if
        ['] \ execute
      then
    ; immediate
    

    Here is how this new word can be used:

    :::forth
    \ Practical example for ARDUINO:
    true  constant NANO
    false constant MEGA
    
    \ require pinsDefinitions.txt
    37 constant PORTB
    flash
    MEGA ?\  PORTB %10000000 defPIN: LED  ( PB7 pin 13 )
    NANO ?\  PORTB %00100000 defPIN: LED  ( PB5 pin 13 )
    ram
    

    Here, the constant NANO is true. When the interpreter meets this sequence NANO ?\ PORTB %00100000 defPIN: LED, the part of code after ?\ is processed by the interpreter.

    With the word ?\, the final executable code would no longer be cluttered with unusable parts of code.

    Whether you are on Arduino NANO or MEGA, we can now test the ignition and extinction of the integrated LED connected to pin 13. Test from the terminal in interpreted mode:

    :::forth
    LED output 
    LED high
    LED low
    

    With conditional compilation, you can write FORTH code adaptable to hardware and options available on the various Arduino boards.
    Link: https://arduino-forth.com/article/FORTH_FlashForth_development_conditionalCompilation

     

    Last edit: PETREMANN 2020-06-23
  • Mikael Nordman

    Mikael Nordman - 2020-06-24

    Great idea.

    I think a more modern version would use 0= and POSTPONE.
    0= always leaves a well formed flag on the stack. INVERT inverts the bits of the cell.
    Also the FF optimiser can optimize the 0= away by inverting the machine level conditional statement.

    : ?\  ( fl --- )
      0=
      if
        postpone \
      then
    ; immediate
    

    I wonder how difficult it would be to implement [IF] [ELSE] [THEN] for more advanced conditional compilation ?

     

Log in to post a comment.