[Oopic-compiler-devel] Preprocessor
Status: Planning
Brought to you by:
ndurant
From: Neil D. <nd...@us...> - 2004-12-29 01:21:42
|
Hi guys, Over Christmas I knocked together the beginnings of a C preprocessor in Python. It was mostly as a project to teach me Python, but I think I've managed to get enough functionality into it to be useful. Perhaps it could form the beginnings of this OOPic compiler we've been discussing. This is totally separate from Andy's OOPic-specific preprocessor, and it has nothing OOPic-specific about it. It just implements a subset of the standard C preprocessor directives. I suspect that if we're creating a compiler from scratch for the OOPic, many of the tasks handled by Andy's preprocessor such as constant handling etc, should be done by the compiler itself, as it will need to fully tokenise/parse the source code anyway. Here's what the preprocessor can currently handle: #include "path/to/file.h" (including ../../file.h etc) #include <file.h> using current directory and paths specified in INCLUDE environment variable (: is used as the delimiter) #define MACRO substitution text including recursive macro substitution, eg macros defined in terms of other macros. However parameterised macros and multi-line macros (using a trailing backslash) are not yet implemented #undef For undefining macros #ifdef NAME and #ifndef NAME For enabling/disabling sections of code depending on existence of a macro #if expression For enabling/disabling sections of code depending on result of an expression. Expressions can be as simple as 0, or as complex as something like !(MACRO + 5 > OTHERMACRO*(PI -7)) etc #elif expression For further #if type clauses #else Can be used with #if, #ifdef and #ifndef #endif For use with #if, #ifdef, #ifndef and #elif Furthermore, the preprocessor strips out comments (C and C++ style), and tracks initial file and line-numbers (so that errors/warnings given by the subsequent compilation stage can provide the original line numbers, regardless of #inclusion, #ifdefs changing the number of lines of code etc). I believe I have added sufficient error-checking to alert the developer to incorrect use of the preprocessor directives (currently the preprocessor will abort all preprocessing upon the first fatal error, although it will continue if warnings are generated). I've tested it on the Mac, but as it's Python it should work perfectly well on Windows and Linux without change. However there may be some issues I hadn't considered. To run it you'll need Python installed (www.python.org), then just unzip the attached file into a folder, and from a DOS box (in Windows) or console (Mac/Linux/other), type something like: python preproc.py myfile.c ...where myfile.c is the file you want to preprocess. Type this to get full usage information (eg command line options etc): python preproc.py --help If you have the time/inclination, check it out, and please hammer it with some complex preprocessor work (especially #if expressions) to see if you can break it and find bugs. Any suggestions, feedback, bug reports, bug fixes, or code for other preprocessor directives very welcome!!! When I get round to it I'll upload the code to the CVS code repository on sourceforget.net. In the meantime, I thought I'd just attach the thing to a posting. Happy New Year, Neil -- Neil Durant <nd...@us...> |