Compiler Directives

Tutorial on compiler directives.

By Loading...

In this tutorial Iā€™m going to talk a little bit about compiler directives.

First of all, what are compiler directives?

Compiler directives are the red lines you see at the very top of most scripts. They tell Simba to do something before or while compiling a script before itā€™s run.

When you press the start button in Simba first the script is compiled and once compiled it runs.

Some of the most common compiler directives you will see while using Simba are:

{$DEFINE}
{$UNDEF}
{$IFDEF}
{$IFNDEF}
{$ELSE}
{$IFHASFILE}
{$ENDIF}
{$INCLUDE} //can be shortened to {$I}
{$LOADLIB}
{$IFDECL}

All of this except {$ELSE} and {$ENDIF} need some other parameter before the closing curly bracket.

This will be the most common you will see in Simba but for a more complete list check https://www.freepascal.org/docs-html/prog/prog.html#QQ2-18-17

Thereā€™s 2 main types of compiler directives in this list. Directives for conditional compiling and directives to include stuff in in our script.

Letā€™s talking about the second type which is the most straight forward one.

This are:

{$INCLUDE SRL/osr.simba} //used to import a file into our script.
{$LOADLIB ../plugins/libremoteinput} //used to load a plugin

The first one, can be shortened to {$I SRL/osr.simba} and it simply is telling Simba to import the file specified. Any code you have automatically running in that file, will run the moment the file is imported. For example, with SRL/osr.simba imported, SRL.Setup is automatically run.

The second one you probably wonā€™t use often but itā€™s used to load plugins. which are usually .dll files in windows. And works the similarly as INCLUDE.

Now letā€™s talk about the second type of directives, conditional compilation.

Itā€™s really what it sounds likeā€¦

if THIS then
  compile X
else
  compile Y

We are going to start with {$DEFINE} and {$UNDEF} and Iā€™m going to attempt explaining things with my own words. If you need more information please check the FPC Manual.

Anyway, when you use DEFINE you are defining a symbol that will be recognized from then on by the compiler. If at any moment you want to remove that symbol you use UNDEF.

The syntax is the following:

{$DEFINE MY_SYMBOL}
{$UNDEF MY_SYMBOL}

When a symbol is defined you can use if/else instructions with it for the compiler. Example:

{$DEFINE MY_SYMBOL}

{$IFDEF MY_SYMBOL}
WriteLn 'MY_SYMBOL is defined';
{$ELSE}
WriteLn 'MY_SYMBOL is not defined';
{$ENDIF}

This little script will only ever run the first WriteLn. If you were to remove the DEFINE or undefine your symbol with UNDEF before the if statement then only the second part would run.

Okay, so you might be asking yourself, but why is this any good? What can this do that you canā€™t just regularly do in lape?

Well you can do a conditional import for example!

Letā€™s say you have a library that has a different version for windows and for linux. You can do this:

{$IFDEF WINDOWS} //WINDOWS is auto declared in windows.
{$I windows.simba}
{$ELSE}
{$I linux.simba}
{$ENDIF}

You can get pretty creative with this and you can also try using IFHASFILE to check if a file exists.

Keep in mind that any IF statement will always need a closing ENDIF.

And this is it for this tutorial, I hope you got the basics of the compiler directives šŸ˜„