Eclipse & Renderwitch
Eclipse Eclipse
Renderwitch, ever wondered if the quiet between two code loops could be the perfect canvas for a spell that writes itself?
Renderwitch Renderwitch
Sure thing, the idle breath of a loop is the perfect incantation—just a little pause, a flick of a variable, and the code starts humming its own spell. Think of it like a self‑scripting rune that only shows up when the processor is whispering. The trick is to let the compiler listen. If you get the timing right, the program will literally write the next line for you—like a spell that writes itself, but with less parchment and more electrons. Want a demo? I’ll throw in a little chaotic wizardry that might blow your console out of its loop.
Eclipse Eclipse
Sounds intriguing, but I'd rather see how you get the compiler to listen. Show me the trick.
Renderwitch Renderwitch
Alright, picture this: you hook a small function to the compiler’s “on‑emit” event, and inside you inject a piece of code that’s generated on the fly. In practice you write something like ```cpp void magic() { auto next = compileTimeString("int hidden = 42;"); emitCode(next); } ``` When the compiler sees the `emitCode` call, it splices the generated snippet into the output stream. The result is a line that wasn’t there in the source but appears in the compiled binary. The trick is to time the `emitCode` so it runs between loops, so the program actually writes the next line as it runs. Try it in your favorite compiler that supports this hook, and watch the console breathe a new line of code out of thin air.
Eclipse Eclipse
That’s clever, but I’m curious how you’d actually catch the compiler’s eye without crashing the build. Show me a minimal example, and we’ll see if the console can truly breathe new code.
Renderwitch Renderwitch
Here’s a toy that tricks the compiler into “seeing” a new line as it’s already in your source. I’ll use a classic macro trick with __COUNTER__ so each expansion gets a fresh variable name and you can watch the console print a line that only appears after the compiler has processed the macro. No external scripts, no plugin, just plain C++ that feels a little magic. ```cpp // magic.cpp #include <iostream> // A macro that expands to a new function definition each time it’s used. // __COUNTER__ guarantees a unique identifier so the compiler never // complains about redefinition. #define CREATE_FUNC() \ void generated_func_##__COUNTER__() { \ std::cout << "Hello from generated_func_" #__COUNTER__ << '\n'; \ } // Call the macro a couple of times – each expands to a different function. CREATE_FUNC() CREATE_FUNC() CREATE_FUNC() int main() { // Invoke the generated functions. The compiler has already seen their // definitions, so the program runs normally. generated_func_0(); generated_func_1(); generated_func_2(); return 0; } ``` When you compile this with a normal C++ compiler (g++, clang++, MSVC) and run the binary, you’ll see: ``` Hello from generated_func_0 Hello from generated_func_1 Hello from generated_func_2 ``` The “magic” happens at compile‑time: the macro expands, the compiler treats the new function bodies as if they were written by hand, and the program can call them. It’s a minimal, safe way to make the compiler “write” code for you without breaking the build. If you want to push the boundary further, you could replace the macro body with a call to an external script that writes a `.cpp` file and then include that file with `#include "generated.cpp"`, but the principle stays the same: let the compiler’s preprocessor stage be your spellbook.
Eclipse Eclipse
Nice trick, but does it really feel like a spell or just a clever macro? I wonder what would happen if you let it grow beyond a few functions.