LPC Preprocessor Manual The preprocessor is a front end to the LPC compiler that provides such handy features as:
// Create a 40 cell array of integers and initialize each cell
// to its cell number times 2,
// i.e. stack[0] = 0, stack[1] = 2, stack[2] = 4, etc
#define STACKSIZE 40
#define INITCELL(x) 2*x
int *stack;
create() {
int i;
stack = allocate(STACKSIZE);
for (i = 0; i < STACKSIZE; i++)
stack[i] = INITCELL(i);
}
Lastly, it's sometimes useful to undefine (i.e. make the compiler forget
about) a macro. The following directive is then used:
Syntax: #undef identifier
Note:
It's perfectly acceptable to undefine an identifier that hasn't been
defined yet.
Conditional Compilation
These directives can add flexibility to your code. Based on whether an
identifier is defined (or not defined), variations of the code can be
produced for different effects. Applications include selective admin
logging and support for multiple drivers (or versions of the same driver).
Syntax: #ifdef
#ifndef
#if
#elif
#else
#endif
Note:
// Using #if 0 allows you to comment out a block of code that
// contains comments. One reason to do so may be to keep a copy
// of the old code around in case the new code doesn't work.
#if 0
// In this case, the constant expression evaluates
// (or is) 0, so the code here is not compiled
write(user_name + " has " + total_coins + " coins\n");
#else
// This is the alternate case (non-zero), so the code
// here _is_ compiled
printf("%s has %d coins\n", user_name, total_coins);
#endif
Example 2:
// This example is derived from TMI's /adm/simul_efun/system.c
#ifdef __VERSION
string version() { return "2.7.2"; }
#elif defined(MUDOS_VERSION)
string version() { return MUDOS_VERSION; }
#else
# if defined(VERSION)
string version() { return VERSION; }
# else
string version() { return -1; }
# endif
#endif
Debugging
The '#echo' directive allows you to print messages to the driver's stderr
(STanDard ERRor) stream. This facility is useful for diagnostics and
debugging.
Syntax: #echo This is a message
Note:
The rest of the line (or end-of-file, which ever comes first) is the
message, and is printed verbatim. It's not necessary to enclose text
with quotes.
Compiler Specific
This facility performs implementation-dependent actions.
Syntax: #pragma keyword
At this time the following control keywords are recognized:
Syntax 1: @marker
<... text block ...>
marker
Syntax 2: @@marker
<... text block ...>
marker
Notes:
@ - produces a string suitable for write()
@@ - produces an array of strings, suitable for the body pager
These are used by prepending '@' (or '@@') before an end marker word. This
is followed by your formatted text, as you would have it appear to the user.
The text block is terminated by the end marker word, without the '@'
(or '@@'). With '@', the text block is processed as if it were a single
string surrounded by quotes and '\n' (newlines) in between the lines.
With '@@', the text block is processed as it were an array of strings,
with each line being a string surrounded by quotes.
Example 1:
int help() {
write( @ENDHELP
This is the help text.
It's hopelessly inadequate.
ENDHELP
);
return 1;
}
Is equivalent to:
int help() {
write( "This is the help text\nIt's hopelessly inadequate.\n" );
return 1;
}
Example 2:
int help() {
this_player()->more( @@ENDHELP
This is the help text.
It's hopelessly inadequate.
ENDHELP
, 1);
return 1;
}
Is equivalent to:
int help() {
this_player()->more( ({ "This is the help text.",
"It's hopelessly inadequate." }), 1);
return 1;
}