Created
May 7, 2024 14:02
-
-
Save rfl890/0d9a6b0d2e27a067d3c7b1bad91670d8 to your computer and use it in GitHub Desktop.
Macro Abuse 2: Electric Boogaloo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "wtf.h" | |
MAINF | |
PRINT("Hello, world!") | |
ENDMAINF |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#ifdef _WIN32 | |
void Sleep(unsigned int ms); | |
// Windows-specific function | |
#define SLEEP(time) Sleep(time); | |
#endif | |
// Prints a string with a trailing newline | |
#define PRINT(text) printf("%s\n", text); | |
// Prints a string without a trailing newline | |
#define PRINTNOL(text) printf("%s", text); | |
// Prints a number as hex | |
#define PRINTHEX(n) printf("%02x\n", n); | |
#define PRINTHEXNOL(n) printf("%02x", n); | |
// Prints any integer | |
#define PRINTINT(num) printf("%ll\n", (long long)num); | |
// Prints any unsigned integer | |
#define PRINTUNSIGNED(num) printf("%llu\n", (unsigned long long)num); | |
// Low-level printf access | |
#define PRINTF(fmt, ...) printf(fmt, __VA_ARGS__) | |
// Calls a function. Usage: FUNCTIONCALL(puts, "hello world") | |
#define FUNCTIONCALL(fname, ...) (fname)(__VA_ARGS__); | |
// For when you use a function call as an rvalue. Usage: FUNCTIONCALL(somefunc, FUNCTIONCALLCHAIN(somefuncret, 0xffff)) | |
#define FUNCTIONCALLCHAIN(fname, ...) (fname)(__VA_ARGS__) | |
// For loops. Usage: FOR(cond1, cond2, cond3) | |
#define FOR(exp1, exp2, exp3) for (exp1; exp2; exp3) { | |
#define ENDFOR } | |
// While loops | |
// Usage: | |
// WHILE(1) | |
// ... code here | |
// ENDWHILE | |
// | |
// DO_WHILE | |
// ... code | |
// END_DO_WHILE(0) | |
#define WHILE(x) while (x) { | |
#define ENDWHILE } | |
#define DO_WHILE do { | |
#define END_DO_WHILE(x) } while (x); | |
// Generic loops | |
// Usage: | |
// LOOP(100, { | |
// ... code | |
// }) | |
// | |
// FOREVER({ | |
// ...code | |
// }) | |
#define LOOP(amount, code) FOR(ASSIGN_FOR(NEWVARTDEF_ASSIGN(int, i), 0), i < amount, INC_PRE(i)) code ENDFOR | |
#define FOREVER(code) FOR(,,) code ENDFOR | |
#define COMMENT(...) | |
// Typedefs | |
// | |
// Usage: | |
// | |
// TYPEDEF(UInt32, U32) | |
// TYPEDEF_P(PUInt32, PU32) | |
// TYPEDEF_FP(MyEpicFunction, int, PU32, int) | |
#define TYPEDEF(newtypename, ...) typedef __VA_ARGS__ newtypename; | |
#define TYPEDEF_P(newtypename, ...) typedef __VA_ARGS__ *newtypename; | |
#define TYPEDEF_FP(typename, returntype, ...) typedef returntype (*typename)(__VA_ARGS__); | |
// Gets an element from an array. | |
// Usage: ARRELEM(MyCoolArray, 32) | |
#define ARRELEM(array, position) (array[position]) | |
// Dumps an array as hex to the console. | |
#define DUMPARRHEX(arr, len) LOOP(len, PRINTHEXNOL(CAST(PU8, arr)[i])) PRINT("") | |
// Dumps an array as hex to the console without a trailing newline | |
#define DUMPARRHEXNOL(arr, len) LOOP(len, PRINTHEXNOL(CAST(PU8, arr)[i])) | |
#define NEWFUNCTION(fname, returntype, calling_conv, ...) returntype calling_conv fname(__VA_ARGS__) { | |
#define NEWFUNCTIONINLINE(fname, returntype, ...) inline NEWFUNCTION(fname, returntype,,__VA_ARGS__) | |
#define NEWFUNCTIONPROTOTYPE(fname, returntype, ...) returntype fname(__VA_ARGS__); | |
#define NEWFUNCTIONEND } | |
#define ARG(type, name) type name | |
#define MAINF int main(void) { | |
#define ENDMAINF } | |
TYPEDEF(I8, char) | |
TYPEDEF(I16, short) | |
TYPEDEF(I32, int) | |
TYPEDEF(I64, long long) | |
TYPEDEF(U8, unsigned char) | |
TYPEDEF(U16, unsigned short) | |
TYPEDEF(U32, unsigned int) | |
TYPEDEF(U64, unsigned long long) | |
TYPEDEF_P(PI8, char) | |
TYPEDEF_P(PI16, short) | |
TYPEDEF_P(PI32, int) | |
TYPEDEF_P(PI64, long long) | |
TYPEDEF_P(PU8, unsigned char) | |
TYPEDEF_P(PU16, unsigned short) | |
TYPEDEF_P(PU32, unsigned int) | |
TYPEDEF_P(PU64, unsigned long long) | |
TYPEDEF(VOID, void) | |
TYPEDEF_P(PVOID, void); | |
#define NEWI8ARR(name, ...) I8 name[] = { __VA_ARGS__ }; | |
#define NEWI16ARR(name, ...) I16 name[] = { __VA_ARGS__ }; | |
#define NEWI32ARR(name, ...) I32 name[] = { __VA_ARGS__ }; | |
#define NEWI64ARR(name, ...) I64 name[] = { __VA_ARGS__ }; | |
#define NEWU8ARR(name, ...) U8 name[] = { __VA_ARGS__ }; | |
#define NEWU6ARR(name, ...) U16 name[] = { __VA_ARGS__ }; | |
#define NEWU32ARR(name, ...) U32 name[] = { __VA_ARGS__ }; | |
#define NEWU64ARR(name, ...) U64 name[] = { __VA_ARGS__ }; | |
#define NEWI8ARRDEF(name, length) I8 name[length]; | |
#define NEWI16ARRDEF(name, length) I16 name[length]; | |
#define NEWI32ARRDEF(name, length) I32 name[length]; | |
#define NEWI64ARRDEF(name, length) I64 name[length]; | |
#define NEWU8ARRDEF(name, length) U8 name[length]; | |
#define NEWU6ARRDEF(name, length) U16 name[length]; | |
#define NEWU32ARRDEF(name, length) U32 name[length]; | |
#define NEWU64ARRDEF(name, length) U64 name[length]; | |
#define NEWTARR(typename, name, ...) typename name[] = { __VA_ARGS__ }; | |
#define NEWTARRDEF(typename, name, length) typename name[length]; | |
#define NEWTARRHEAP(typename, name, length) typename *name = calloc(length, sizeof(typename)); | |
#define NEWSTRING(name, initializer) I8 name[] = initializer; | |
#define NEWSTRINGW(name, initializer) U16 name[] = L ## initializer; | |
#define NEWVARTDEF(typename, name) typename name; | |
#define NEWVARTDEF_ASSIGN(typename, name) typename name | |
#define NEWVART(typename, name, value) ASSIGN(NEWVARTDEF_ASSIGN(typename, name), value) | |
#define NEWI8(name, initializer) ASSIGN(NEWVARTDEF_ASSIGN(I8, name), initializer) | |
#define NEWI16(name, initializer) ASSIGN(NEWVARTDEF_ASSIGN(I16, name), initializer) | |
#define NEWI32(name, initializer) ASSIGN(NEWVARTDEF_ASSIGN(I32, name), initializer) | |
#define NEWI64(name, initializer) ASSIGN(NEWVARTDEF_ASSIGN(I64, name), initializer) | |
#define NEWU8(name, initializer) ASSIGN(NEWVARTDEF_ASSIGN(U8, name), initializer) | |
#define NEWU16(name, initializer) ASSIGN(NEWVARTDEF_ASSIGN(U16, name), initializer) | |
#define NEWU32(name, initializer) ASSIGN(NEWVARTDEF_ASSIGN(U32, name), initializer) | |
#define NEWU64(name, initializer) ASSIGN(NEWVARTDEF_ASSIGN(U64, name), initializer) | |
#define STRUCT(name) struct name | |
#define NEWSTRUCT(name) STRUCT(name) { | |
#define NEWSTRUCTELEMI8(name) I8 name; | |
#define NEWSTRUCTELEMI16(name) I16 name; | |
#define NEWSTRUCTELEMI32(name) I32 name; | |
#define NEWSTRUCTELEMI64(name) I64 name; | |
#define NEWSTRUCTELEMU8(name) U8 name; | |
#define NEWSTRUCTELEMU16(name) U16 name; | |
#define NEWSTRUCTELEMU32(name) U32 name; | |
#define NEWSTRUCTELEMU64(name) U64 name; | |
#define NEWSTRUCTELEMT(type, name) type name; | |
#define STRUCTELEM(name, elem) (name.elem) | |
#define STRUCTELEMP(name, elem) (name->elem) | |
#define ENDNEWSTRUCT }; | |
#define ADDROF(name) (&name) | |
#define CAST(type, var) ((type)(var)) | |
#define DEREF(name) (*name) | |
#define ASSIGN(x, y) x = y; | |
#define ASSIGN_FOR(x, y) x = y | |
#define ASSIGN_CAPTURE(x, y) (x = y) | |
#define ADD(x, y) (x + y) | |
#define SUB(x, y) (x - y) | |
#define DIV(x, y) (x / y) | |
#define MUL(x, y) (x * y) | |
#define MOD(x, y) (x % y) | |
#define UNM(x) (-x) | |
#define UNP(x) (+x) | |
#define INC_PRE(x) (++x) | |
#define INC_POST(x) (x++) | |
#define EQ(x, y) (x == y) | |
#define NEQ(x, y) (x != y) | |
#define GT(x, y) (x > y) | |
#define LT(x, y) (x < y) | |
#define GTE(x, y) (x >= y) | |
#define LTE(x, y) (x <= y) | |
#define AND(x, y) (x && y) | |
#define OR(x, y) (x || y) | |
#define NOT(x) (!x) | |
#define BNOT(x) (~x) | |
#define BAND(x, y) (x & y) | |
#define BOR(x, y) (x | y) | |
#define BXOR(x, y) (x ^ y) | |
#define BLSHIFT(x, y) (x << y) | |
#define BRSHIFT(x, y) (x >> y) | |
#define ADD_ASIGN(x, y) (x += y) | |
#define SUB_ASSIGN(x, y) (x -= y) | |
#define MUL_ASSIGN(x, y) (x *= y) | |
#define DIV_ASSIGN(x, y) (x /= y) | |
#define MOD_ASSIGN(x, y) (x %= y) | |
#define BAND_ASSIGN(x, y) (x &= y) | |
#define BOR_ASSIGN(x, y) (x |= y) | |
#define BXOR_ASSIGN(x, y) (x ^= y) | |
#define BLSHIFT_ASSIGN(x, y) (x <<= y) | |
#define BRSHIFT_ASSIGN(x, y) (x >>= y) | |
#define TERNARY(x, y, z) (x ? y : z) | |
#define SIZEOF(x) (sizeof(x)) | |
#define MEMALLOC(amount) (malloc(amount)) | |
#define FREE(ptr) free(ptr); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment