Previous chapter To contents Next chapter

Chapter 15, The preprocessor

Pike has a builtin C-style preprocessor. The preprocessor reads the source before it is compiled and removes comments and expands macros. The preprocessor can also remove code depending on an expression that is evaluated when you compile the program. The preprocessor helps to keep your programming abstract by using defines instead of writing the same constant everywhere.

It currently works similar to old C preprocessors but has a few extra features. This chapter describes the different preprocessor directives. This is what it can do:

DIRECTIVE
#!

DESCRIPTION
This directive is in effect a comment statement, since the preprocessor will ignore everything to the end of the line. This is used to write Unix type scripts in Pike by starting the script with

DIRECTIVE
#""

DESCRIPTION
Makes it possible to write strings with newline in them.
#"Foo
bar"
is the same as
"Foo\nbar"

DIRECTIVE
#define

DESCRIPTION
The simplest way to use define is to write

BUGS
Note that it is not a good idea to do something like this:

EXAMPLE
#define A "test"
#define B 17
#define C(X) (X)+(B)+"\n"
#define W write
#define Z Stdio.stdout
int main()
{
    Z->W(C(A));
}

DIRECTIVE
#undef

DESCRIPTION
This removes the effect of a #define, all subsequent occurrences of the undefined identifier will not be replaced by anything. Note that when undefining a macro, you just give the identifier, not the arguments.

EXAMPLES
#define foo bar
#undef foo
#define foo(bar) gazonk bar
#undef foo

DIRECTIVE
#if
#elif
#elseif
#else
#endif

DESCRIPTION
The #if directive causes conditional compiling of code depending on the expression after the #if directive. That is, if the expression is true, the code up to the next #else, #elif, #elseif or #endif is compiled. If the expression is false, that code will be skipped. If the skip leads up to a #else, the code after the else will be compiled. #elif and #elseif are equivalent and causes the code that follow them to be compiled if the previous #if or #elif evaluated false and the expression after the #elif evaluates true.

EXAMPLES
#if 1
#else
#endif

#elif defined(BAR)
#else
#endif

DIRECTIVE
#error

DESCRIPTION
This directive causes a compiler error, it can be used to notify the user that certain functions are missing and similar things.

EXAMPLES
#if !constant(write_file)
#error write_file function is missing
#endif

DIRECTIVE
#include

DESCRIPTION
This directive should be given a file as argument, it will then be compiled as if all those lines were written at the #include line. The compiler then continues to compile this file.

EXAMPLES
#include "foo.h"

DIRECTIVE
#line

DESCRIPTION
This directive tells the compiler what line and file we are compiling. This is for instance used by the #include directive to tell the compiler that we are compiling another file. The directive takes the line number first, and optionally, the file afterwards.

EXAMPLES
#line 4 "foo.cf" /* The next line was generated from 4 in foo.cf */

DIRECTIVE
#pragma

DESCRIPTION
This is a generic directive for flags to the compiler. Currently, the only flag available is 'all_inline' which is the same as adding the modifier 'inline' to all functions that follows.

DIRECTIVE
#string

DESCRIPTION
This directive works as #include but makes the included file a string.
string html_doc=#string "index.html";
This puts the file index.html into the string html_doc.


Previous chapter To contents Next chapter