Hi,

I have just implemented a rudimentary, but useful, preprocessor in the Gambas 
3 compiler.

Preprocessor commands are lines that begin with a '#' in the source code. The 
indentation spaces are ignored. You can put comments at the end of a 
preprocessor line.

the preprocessor only understands conditional compilation at the moment.

A condition compilation test starts with a "#If ..." line, and ends with a 
"#Endif" line. You can use a "#Else" or "#Else If" line between.

The "#If" test is a *preprocessor expression* that compare a *preprocessor 
constant* with a string value.

The preprocessor understands three constants:

- SYSTEM, that returns the current OS ("Linux", "FreeBSD", "MacOSX"...)

- ARCHITECTURE (or ARCH), that returns the current architecture ("x86", 
"x86_64", "ARM", "PPC", "unknown").

- VERSION (or GAMBAS), that returns the version of the current interpreter 
("3.0").

SYSTEM and ARCHITECTURE must be followed by a "=" or a "<>" operator.

VERSION can be followed by any comparison operator ("=", "<>", ">", "<=", "<", 
">="). The version string must have the form "X.Y", and the comparison works 
as expected.

The preprocessor test understands the "Not", "And" and "Or" keywords, and can 
be imbricated with parenthesis.

The "#Else If" has the same syntax as "#If".

"#If ... #Else ... #Endif" tests can be imbricated up to 16 levels.

Here is a stupid example:

        Public Sub Main()
          
          #If SYSTEM = "BSD" 'Or VERSION = "3"
            Print "BSD"
          #Else If VERSION >= "2.0"
            #If ARCHITECTURE = "x86"
              Print "32 bits"
            #Else If ARCH = "x86_64"
              Print "64 bits!"
            #Else
              Print "?"
            #Endif
          #Endif
          
        End

Note that the compiler does not see the lines excluded by conditional 
compilation (logical, for a preprocessor!). So you can imbricate things like 
that:

#If ARCH = "x86_64"
Public Sub GetValue() As Long
#Else
Public Sub GetValue() As Integer
#Endif
...
End

Of course you can do more weird things that will mix up the compiler. :-)

That feature was needed for the release candidate. 

I plan to release successive versions of Gambas more often, so backward 
compatibility may be sometimes broken, and so conditional compilation based on 
interpreter version is a must.

And conditional compilation on architecture or OS will be useful too, 
especially when dealing with EXTERN function declarations.

Regards,

-- 
Benoît Minisini

------------------------------------------------------------------------------
Lotusphere 2011
Register now for Lotusphere 2011 and learn how
to connect the dots, take your collaborative environment
to the next level, and enter the era of Social Business.
http://p.sf.net/sfu/lotusphere-d2d
_______________________________________________
Gambas-user mailing list
Gambas-user@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/gambas-user

Reply via email to