Language text preprocessor
The preprocessor is a part of the language compiler, that is executed at the first phase
of code compilation, which processes the input text data before the main compilation.
The role of the preprocessor is to remove comments,
apply macros, include subfiles of a source code and analyze various directives.
Comments
A comments are a regions in code, that are skipped by the compiler while reading the code.
Comments in Cind lanugage are like in C/C++ languages, that is,
there is a comment region from characters: /* to the characters: */
and also a comment from characters: // to the end of the line.
(x,y)->x+ /* comment region */ y; /* second comment */
fun(x,y) { return x+y; } // another comment but to the end of the line
Directives
A directive is a kind of preprocessor's instruction, that is inserted into the code.
Each directive starts with hash character ( # )
at the beginning of the line.
The preprocessor analyze meaning of directives and removes their lines
from the code before future compilation.
A directive can be a macro definition, macro undefinition,
file inclusion, beginning of conditional section, etc
(for list of directives see below).
For example, those are 4 directives:
#if (2+2 == 4)
#else
#undef SELECTION_COLOR
#endif
For more about expression used in conditional directive see Expression in preprocessor directives.
List of preprocessor directives
directive |
arguments |
description |
example |
#define |
MACRONAME CODE |
defines macro |
#define pi 3.141592654f |
#define |
MACRONAME(x,y) CODE |
defines macro with parameters |
#define MAX(x,y) ((x>y)?x:y) |
#undef |
MACRONAME |
undefines macro |
#undef pi |
#ifdef |
MACRONAME |
includes the followin region of code if the given macro is defined |
#ifdef ABC |
#ifndef |
MACRONAME |
includes the followin region of code if the given macro is not defined |
#ifndef ABC |
#if |
(condition) |
includes the followin region of code if the given condition is true |
#if (2+3 > 6) |
#else |
|
starts alternative region of code |
|
#endif |
|
ends conditional region of code |
|
#include |
"file name" |
includes code from the other file |
#include "./mylibs/m2.cind" |
Macros
Macro is a set of characters, identified by name, that will be inserted into the code,
in the place of that name.
The preprocessor holds a list of macros and inserts them while processing the code.
To define macro use preprocessor's directive #define.
For example, if macro named y is a string 2+2,
then processing code x+y+z will replace them to: x+2+2+z.
#define y 2+2
x+y+z // --> x+2+2+z
Macro can also has parameters.
For example, applying macro z with parameter p,
which is a string 1+p+3, will change code z(2)+4
to 1+2+3+4.
#define z(p) 1+p+3
z(2)+4 // --> 1+2+3+4
More examples:
#define A
#define B "Hello!"
#define C(x,y) (B+x+y)
To remove macros from preprocessor's list use #undef directive. For example:
#undef C
#undef A
Conditional section
A conditional section consists of a piece of code, that can be included into the main code
depending on some conditional value.
That conditionally included piece of code should be placed between
#if and #endif directives.
For example:
#if (2+2 == 4)
host.println("Hello");
#endif
When expression inside #if directive is true,
then that internal piece of code will be included,
otherwise, when expression is false or incorrect, then that piece of code will be omitted.
For syntax of that expression see Expression in preprocessor directives.
An optional directive #else inside conditional section ends that section
and starts the alternative section
(just like else phrase in conditional instruction if (...) ... else ... ; ).
Example:
#if (COLOR == "red")
host.println("have defined red color");
#else
host.println("red color not defined");
#endif
A beginning directive for a conditional section can be one of:
#if, #ifdef and #ifndef.
A conditional sections can be placed recursively. Example:
#ifndef COLOR
#define COLOR "red"
#else
#if ((COLOR == "blue")||(COLOR == "yellow"))
#undef COLOR
#define COLOR "red"
#endif
#endif
Including files
An #include
directive puts a text code from given file in the place of the directive.
An included file can be any text file, and the preprocessor will also parse its content.
Examples:
#include "abc.cind"
#include "./../mylibs/database.cind"
#include "E:\\public\\api\\gate.cind"
Sticking lines
The preprocessor stickes neighboring lines, when first of them ends with backslash character
( \ ).
It is mostly useful when some semantic needs to be on the same line, but it is too large.
Example:
#define A(x,y) x + \
y + \
z
gives:
#define A(x,y) x + y + z
Combining strings
The preprocessor also glues strings inside code, when they are side by side on the same line.
That may be useful to build string value containing some value from a macro.
Example:
#define W "hello"
"oh " W " the" "re" ---> "oh hello there"
|