Skip to content

Instantly share code, notes, and snippets.

@lerno
Created November 6, 2018 21:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lerno/6e8c595a060a61def988cf1e1e569ce3 to your computer and use it in GitHub Desktop.
Save lerno/6e8c595a060a61def988cf1e1e569ce3 to your computer and use it in GitHub Desktop.
Macro proposal
// 1. Macros with arguments
macro @foo(int &v) {
v++;
if (v > 10) return 10;
return v;
}
int bar() {
int a = 10;
@foo(a);
}
// Expands to:
/*
int bar() {
int a = 10;
a++;
if (a > 10) return 10;
return a;
}
*/
// 2. macros may return values
macro int @foo2(int &v, int &w) {
v++;
if (v > 10) return 10;
w += 3;
return 0;
}
int bar2() {
d = 0;
a = 10;
int b = @foo2(a, d);
}
// Expands to
/*
void bar2() {
d = 0;
a = 10;
int b;
if (a > 10) {
b = 10;
} else {
b = 0;
d += 3;
}
}
*/
// 3. They can include a single body that is expanded inline
macro int @foo3(int &a, @block body) {
while (a > 10) {
a--;
@body();
}
}
void bar3() {
b = 20;
@foo3(b) {
print(b);
}
}
// Expands to:
/*
void bar3() {
b = 20;
while (b > 10) {
b--;
{
print(b);
}
}
}
*/
// 4. The body itself may take an arbitrary number of injected parameters
macro int @foo4(@block(int x, int y) body) {
for (int i = 0; i < 100; i++) {
@body(i, i % 3);
}
}
void bar4() {
@foo4() {
if (@y == 0) print(@x);
}
}
// Expands to:
/*
void bar4() {
for (int i = 0; i < 100; i++) {
if (i % 3 == 0) print (i);
}
}
*/
// 5. Untyped is just fine:
macro @foo5(&v) {
v *= 2;
}
int bar5() {
int a = 10;
@foo5(a); // Valid, becomes a *= 2;
float b = 1.2;
@foo5(b); // Valid, becomes b *= 2;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment