How do I write a recursive for-loop “repeat” macro to generate C code with the CPP preprocessor?

后端 未结 4 971
慢半拍i
慢半拍i 2020-12-18 02:28

I want to force the preprocessor to do some automatic code generation for me. I don\'t need much: just a simple for-loop that contains another for-loop.[1]

I\'ve

4条回答
  •  遥遥无期
    2020-12-18 02:53

    Vesa Karvonen's "Order" library/language can definitely do this for you. It implements unrestricted recursion and looping in the C preprocessor, and as a really cool bonus dresses it up with the nice concise syntax of a "proper" programming language (to clarify: this is not an alternative preprocessor, it just does a lot of token-pasting to keep its keywords short. It is still pure CPP).

    It uses a rather different technique, converting your metaprograms to CPS and then passing them to a single loop construct that has potentially trillions of steps, and executes the metaprogram in a strictly linear fashion. Loops and recursive functions can therefore be nested as deeply as you like because they don't have separate drivers that need to interact and paint each other blue.

    Yes really, someone implemented a full virtual machine and interpreter using CPP macros. It's intimidating.

    (EDIT: try the archived version if Rosetta Code has stopped working for you too.)

提交回复
热议问题