Skip to content

Commit

Permalink
Preprocessor docs
Browse files Browse the repository at this point in the history
  • Loading branch information
ollydev committed Oct 27, 2024
1 parent 1cb81a4 commit 0250d64
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 4 deletions.
100 changes: 100 additions & 0 deletions DocGen/source/tutorials/Preprocessor.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#######################
Preprocessor directives
#######################

Simba 2.0 has much improved preprocessor directives allowing constant expressions and useful macros.

----

Basic expressions

.. code-block::
{$DEFINE TEST}
const XYZ = 100;
begin
{$IF XYZ = 100}
WriteLn('XYZ = 100');
{$ENDIF}
{$IF XYZ*2+1 > 200}
WriteLn('XYZ*2 > 200');
{$ENDIF}
{$IF DECLARED(XYZ) and DEFINED(TEST)}
WriteLn('True');
{$ELSE}
WriteLn('False');
{$ENDIF}
{$UNDEF TEST}
{$IF DECLARED(XYZ) and DEFINED(TEST)}
WriteLn('True');
{$ELSE}
WriteLn('False');
{$ENDIF}
end;
----

File

.. code-block::
{$IF FILEEXISTS(Data\settings.ini)}
WriteLn('Settings file exists');
{$ELSE}
WriteLn('Settings file does not exist');
{$ENDIF}
----

Plugins

.. code-block::
{$IF FINDLIB(libremoteinput)}
{$loadlib libremoteinput}
{$ELSE}
WriteLn('Cannot find libremoteinput!);
{$ENDIF}
{$IF LOADEDLIB(libremoteinput)}
WriteLn('libremoteinput is loaded');
{$ELSE}
WriteLn('libremoteinput is *not* loaded');
{$ENDIF}
----

Macros which are inserted constants at compile time.

.. code-block::
procedure Test;
begin
WriteLn('Func=', {$MACRO FUNC}); // current function name
end;
begin
Test();
end;
.. code-block::
begin
WriteLn {$MACRO LINE}; // The line number
WriteLn {$MACRO TICKCOUNT}; // GetTickCount
WriteLn {$MACRO NOW}; // TDateTime.Now()
WriteLn {$MACRO FILE}; // current file
WriteLn {$MACRO DIR}; // directory the current file is in
WriteLn {$MACRO ENV(USERPROFILE)}; // environment variable
WriteLn {$MACRO INCLUDEDFILES}; // all included files (as a TStringArray)
WriteLn {$MACRO LOADEDLIB(libremoteinput)}; // if lib "libremoteinput" is loaded, return the filename of the lib.
WriteLn {$MACRO LOADEDLIBS}; // all loaded libs (as a TStringArray)
WriteLn {$MACRO FINDLIB(libremoteinput)}; // try to find a lib, returning the path
end;
1 change: 1 addition & 0 deletions DocGen/source/tutorials/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ Tutorials
Mouse & Keyboard.rst
Variant Data Type.rst
Properties.rst
Preprocessor.rst

plugins/index.rst
8 changes: 4 additions & 4 deletions Source/script/simba.script.pas
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ function TSimbaScript.DoCompilerPreprocessorFunc(Sender: TLapeCompiler; Name, Ar
Value := '';

case Name of
'LIBLOADED': Value := BoolToStr(FindLoadedPlugin(Argument) <> '', True);
'LIBEXISTS': Value := BoolToStr(SimbaEnv.HasPlugin(Argument, [Sender.Tokenizer.FileName]), True);
'LOADEDLIB': Value := BoolToStr(FindLoadedPlugin(Argument) <> '', True);
'FINDLIB': Value := BoolToStr(SimbaEnv.HasPlugin(Argument, [Sender.Tokenizer.FileName]), True);
end;
end;

Expand Down Expand Up @@ -187,8 +187,8 @@ function TSimbaScript.Compile: Boolean;
FCompiler.addPreprocessorMacro('LOADEDLIB', @DoCompilerMacro);
FCompiler.addPreprocessorMacro('LOADEDLIBS', @DoCompilerMacro);

FCompiler.addPreprocessorFunc('LIBLOADED', @DoCompilerPreprocessorFunc);
FCompiler.addPreprocessorFunc('LIBEXISTS', @DoCompilerPreprocessorFunc);
FCompiler.addPreprocessorFunc('LOADEDLIB', @DoCompilerPreprocessorFunc);
FCompiler.addPreprocessorFunc('FINDLIB', @DoCompilerPreprocessorFunc);

FCompiler.OnFindFile := @DoCompilerFindFile;
FCompiler.OnHint := @DoCompilerHint;
Expand Down

0 comments on commit 0250d64

Please sign in to comment.