Macros within IF commands

hippy

Technical Support
Staff member
#1
Due to the way the pre-processor and syntax checking works within PICAXE Editor 6 there are some tricks which may not be immediately obvious but with a bit of lateral thinking can be put to good use.

For example, having macro style function calls in IF commands, for example -

Code:
If Matched( char, "abc" ) Then
  SerTxd( "Character ", char, " is one of 'a', 'b', or 'c'" )
End If
An example of this is below which can be run in the PE6 simulator or on a real PICAXE. It prints the letters "a" through "z" putting square brackets around those which are in the matching list, vowels in this case -

Code:
Symbol char  = b0
Symbol match = b1
{
#Define Matched(n,chars) b0=0 Then                _
                         End If                   _
                         match = $FF              _
                         LookDown n,(chars),match _
                         If match < $FF
}
Do
  For char = "a" To "z"
    If Matched(char,"aeiou") Then
      SerTxd( "[", char, "]" )
    Else
      SerTxd( char )
    End If
  Next
  SerTxd( CR, LF )
  Pause 1000
Loop
Note it's actually a #DEFINE rather than a #MACRO and each line except the last ends with a line continuation "_".

The first two lines complete an IF-ENDIF and is just wasted code ( and wasted memory ), then comes the processing, and at the end the start of an IF command except the THEN which is in the main program.

This does, as noted waste memory, but that can be acceptable when making the main body of program code more readable and aesthetically pleasing. In PE6 one can click on the [-] box next to the "{" and hide the complexity of what's going on under the hood.
 
Last edited:
Top