@let directive

To allow macros to be used like variables the @let directive can be used. This directive has two syntactic forms. When the type is specified, it introduces a name in that scope. Otherwise, it binds to an existing name by searching upwards through nested scopes for a previously defined variable with that name.

Before translationAfter translation
@let int m = 0
@let m = m+1
int f() { return m; }
@if (m > 0) @let m = m+1
int g() { return m; }



int f() { return 1; }
int g() { return 2; }

String variables

String literals can be given in single or double quotes (and this has no effect on the meaning). The binary ’+’ operator can be used to concatenate strings. However, string variables macroexpand without the containing quotes! This leads to some surprises. To get the quotes the @str directive must be used.

Before translationAfter translation
@let string m = "hello" + " "
@let m = @str(m) + 'world'
@let m := m !!!
const char* f() { return @str(m); }




const char* f() { return "hello world!!!"; }

There is a special syntax to assign a string variable to macroexpanded text. The directive @let x := y is equivalent to @let x = @str(y).