Preprocessor & macros
A preprocessor is a program that transforms source text into other source text before the compiler proper sees it. TryC's is called the precompiler: it runs first, understands nothing about types or expressions, and only handles the lines that start with # (directives) plus inline def. Its output is a single file with every include resolved and every macro expanded, which is what the compiler compiles; --dbi lets you see it.
What the precompiler does
| Directive | Effect |
|---|---|
#include <...> | pastes another file here, once per program |
#define, #undef | textual macros, with or without parameters |
#ifdef, #ifndef, #else, #endif | keep or drop a block depending on whether a macro is defined |
#for, #endfor | repeat a block at compile time |
#pragma message|warning|error | print a message during compilation, optionally stopping it |
inline def | copy a function body at its call sites |
Because the substitution is textual, a macro can stand for anything (a number, a string, a piece of code), and a mistake produces a compiler error on the expanded line, not on the macro: keep the .i file at hand.
#include
#include <libstd/stdio> // standard module
#include <./helpers> // relative to the including file, no extension
A file is included once per program even if several modules include it; the second inclusion is dropped (so libstd modules can include each other freely). Modules included by an included file are visible to you as well: libstd/stdio brings in string and list.
#define
#define MAX 64
#define GREETING "hello"
#define lorem_ipsum lorem ipsum dolor // any text, substituted verbatim
arr = new int[MAX]
print(GREETING)
A macro is a textual substitution of a whole identifier. Macros are visible after their definition, in every file that follows the include. #undef NAME removes one.
Macros with parameters
#define SQUARE(x) ((x) * (x))
#define ADD(a, b) ((a) + (b))
#define SUM_OF_SQUARES(a, b) (SQUARE(a) + SQUARE(b)) // macros can call macros
// multi-line: '\' continues the definition
#define GREET(name) \
print("Hello, " + name + "!\n")
val = SUM_OF_SQUARES(2, 3) // 13
GREET("world")
As in C, the substitution is purely textual: put parentheses around parameters in the body, the precompiler knows nothing about operator precedence. The macro name must be immediately followed by (; with a space in between it is a plain macro whose text starts with (.
Conditional compilation
#ifdef __unix__
#define macro1 22
#endif
#ifndef __unix__
#define macro1 120
#endif
#ifdef DEBUG
print("debug build\n")
#else
print("release build\n")
#endif
#ifdef/#ifndef/#else/#endif test whether a macro is defined. There is no #if with expressions and no -D flag on the command line: besides the predefined macros below, define the symbols in a header you include.
Predefined macros
The compiler defines, with value 1, the same names a C compiler uses for the system it compiles for, which is the system it runs on:
| Target | Macros |
|---|---|
| Linux | __unix__, __linux__ |
| Windows | _WIN32, _WIN64 |
| x86-64 | __x86_64__ |
| ARM64 | __aarch64__ |
They are visible from the first line of the program, before any #include. The standard library uses them to pick its system layer: libstd/os, file, net, thread and the others include libstd/wrapper/linux/... (raw syscalls) or libstd/wrapper/windows/... (the system DLLs). Only the active branch is read, so the two wrappers can define functions with the same names.
#ifdef __unix__
#include <libstd/wrapper/linux/sysos>
#else
#include <libstd/wrapper/windows/sysos>
#endif
#for: compile-time loops
#for repeats its body at compile time, like NASM's %rep; the loop variable is a macro inside the body, so the value is substituted textually. Loops nest. The header works like TryC's for: several variables in the init and in the step (#for (i = 0, j = N * 2; i < j and i < 3; i++, j -= 2)), integer expressions with + - * / %, parentheses and macros, and a condition with comparisons and and, or, not, checked before every iteration. Each step piece can be i++, i--, i += E, i -= E, i *= E, i /= E, i %= E or i = E. A #for or #if opened in the body must be closed in the body.
#for (i = 0; i < 3; i++)
print("iter " + itoa(i) + "\n")
#endfor
#for (i = 0; i < 2; i++)
#for (j = 0; j < 6; j += 2)
print("i=" + itoa(i) + " j=" + itoa(j) + "\n")
#endfor
#endfor
iter 0
iter 1
iter 2
i=0 j=0
i=0 j=2
i=0 j=4
i=1 j=0
i=1 j=2
i=1 j=4
#pragma message, warning, error
A pragma prints its text during compilation (never in the program), prefixed with its level and the file name and line where it appears. message and warning do not interrupt the build; error stops it with a non-zero exit code, in the spirit of C's #error: a requirement that is not met is reported before a broken binary is produced. A pragma inside a disabled #ifdef branch is skipped like any other line, which gives the usual "missing configuration" check:
#pragma message "compiling the server"
#pragma warning "the debug build logs every request"
#ifndef API_KEY
#pragma error "API_KEY is not defined"
#endif
[message] server.tryc:12: compiling the server
[warning] server.tryc:13: the debug build logs every request
[error] server.tryc:16: API_KEY is not defined
inline def
inline def is also a precompiler feature: the body is copied at each call site with the arguments substituted and the locals renamed (name__inl1), so the compiler never sees a call. Rules and limits are in Functions. When an argument is not a simple name and the parameter is reassigned inside the body, the precompiler keeps a real call to preserve the semantics.
Seeing the result
python3 main.py program.tryc --dbi
cat program.i
The .i file is exactly what the compiler compiles: every included file inlined between start <path>/end <path> markers (used to report errors with the right file name), macros expanded, inline defs copied. It is the first place to look when something surprising happens with a macro.