Slag:Preprocessor Directives

From Plasmaworks

Jump to: navigation, search

Contents

Preprocessor Directives

The following commands are evaluated at compile-time rather than run-time:

[define IDENTIFIER]
[define IDENTIFIER expression]
Defines the given identifier, causing "defined(IDENTIFIER)" to return "true" and replacing IDENTIFIER with the given expression (which can be arbitrary tokens) wherever it's encountered. C-style macros with arguments are not currently supported.
 [if defined(WINDOWS) or defined(MAC)]
   [define CHEAT]
 [endIf]
 [define OP +]
 ...
   method on( KeyEvent e )  # in Plasmacore
 [if defined(CHEAT)]
     if (e.is_press('S')) current_level.skip
 [endIf]
     if (e.is_press('A')) println( 3 OP 5 )  # prints: 8


defined(IDENTIFIER)
Returns "true" if the given IDENTIFIER has been either automatically or explicitly defined with a [define] directive. Can be used by the [if] conditional directive at compile time or by a regular if (etc.) at run-time.
[if]/[elseIf]/[else]/[endIf]
Conditional compile instructions that use defined(id) commands and/or other constant expressions to determine which of several blocks of code to compile.
   method init
     println( "Setting stuff up." )
 [if defined(WINDOWS)]
     do_special_windows_stuff
 [elseIf defined(ANDROID)]
     do_special_android_stuff
 [else]
     do_non_windows_non_android_stuff
 [endIf]
     do_general_stuff
     println( "Done." )
[include "filename.slag"]
Queues the indicated file for compilation as part of the current project. There are no ordering or dependency issues; as long as any given file is included somewhere it will be handled correctly.
[includeDir "path"]
Queues all .slag files in the given folder for include.
[includeDefines "filename.slag"]
Normally an [include] isn't parsed until after the current file is finished parsing. The only time this can be a problem is if you want to keep global directives in a separate file - since they normally won't be parsed until after the current file is finished parsing, you won't have the correct values defined. [includeDirectives] immediately processes all the preprocessor directives from the named file and then continues parsing the original file.
[main ClassName]
Normally the first class encountered in the main .slag file is the main class or start-up class - an object of which is created to launch the program. A [main] directive anywhere in code will set the main class to an arbitrary class.
[requisite all]
Place at the top of a .slag file to mark all classes and all methods in that file as requisite so that unused classes and methods are not culled by the compiler.


Preprocessor Defines

The following parse tokens are either automatically defined or have special meaning if they're defined:

Slag Defines

ASSERT(logical)
Normally, statements of the form ASSERT(result) are compiled out and have no effect. If you define DEBUG, however, any assertion with a "false" argument causes an Error to be thrown.
 [define DEBUG]
 class Test
   method init
     ASSERT(2+2==3)   # throws an error since DEBUG is defined
 endClass
DATE
A literal string with the current year/month/day in YYYY.MM.DD format.
 class Test
   method init
     println( DATE )
       # Prints e.g.: 2011.03.22
 endClass
DEBUG
[define] this at the top of your main source file to enable assertions. See ASSERT, above.

Plasmacore Defines

These defines are defined by the Plasmacore build system on a per-platform basis (e.g. "defined(WINDOWS)" is true if a project is being compiled for Windows):

  • WINDOWS
  • MAC
  • LINUX
  • PLATFORM_IOS
  • ANDROID
  • WP7

(Note: PLATFORM_IOS is used because IOS is already used as the name of the iOS singleton class.)