omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    A pure Python C processor

    Pythonista
    1
    1
    1931
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • dgelessus
      dgelessus last edited by

      From the guy with almost zero practical experience with C, here's a more-or-less functional C preprocessor, for all your C preprocessing needs: https://github.com/dgelessus/pythonista-c-utils/blob/master/preprocess.py

      Features:

      • Macro constants and functions
        • #define and #undef
        • Macro constant and function substitution
        • Stringification (#) and concatenation (##) in macro functions
        • Macro functions internally use Python functions/callables, which means (relatively) easy wrapping of Python functions in macros
        • Nested macro function calls work correctly
        • Macro function arguments containing commas (such as array/struct literals) work correctly
      • Conditional blocks
        • #ifdef, #ifndef, #else and #endif are fully functional
        • #if and #elseif work in simple cases, more complex expressions require user input
      • File inclusion with #include
        • Both angle-bracket and double-quote style are supported
        • Argument may be a macro
      • #error and #warning cause Python exceptions and warnings respectively
      • #pragma and _Pragma always cause warnings
      • #line, __FILE__, __LINE__

      Current issues and caveats:

      • Many constructs and features of the C preprocessor are supported, but that doesn't mean that you'll be able to process most standard header files. Those often rely on special environment-dependent macros provided by the preprocessor, such as __LP64__ indicating a 64-bit environment. By default only very few macros are provided.
      • The tokenizer is relatively dumb and knows little about C syntax, except for comments and string/char literals. This means that for example apostrophes (single quotes) in #error and the like cause problems if they aren't "balanced".
      • #if and #elif conditions have to be evaluated manually by the user, unless they are already a simple number literal.
      • The whole code isn't very pretty - although this is my third attempt or so, it is the first one that actually works well, which means the entire code isn't very polished yet.
      • Did I mention that I have almost no practical experience with C? It is entirely possible that I'm misunderstanding something about how C or the preprocessor works. If one of you folks who are more experienced in C find that I am wrong about something, please do tell me!
      1 Reply Last reply Reply Quote 1
      • First post
        Last post
      Powered by NodeBB Forums | Contributors