https://github.com/geeeeeeeeek/simplecppprecompiler
Implemented a preprocessor for a few of directives.
https://github.com/geeeeeeeeek/simplecppprecompiler
Last synced: 5 months ago
JSON representation
Implemented a preprocessor for a few of directives.
- Host: GitHub
- URL: https://github.com/geeeeeeeeek/simplecppprecompiler
- Owner: geeeeeeeeek
- Created: 2015-04-17T09:06:54.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2015-04-17T12:57:35.000Z (over 10 years ago)
- Last Synced: 2025-03-16T08:51:54.538Z (10 months ago)
- Language: C++
- Homepage:
- Size: 5.17 MB
- Stars: 0
- Watchers: 2
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
###Simple C++ Pre Compiler
A lab belonging to the OOP course. Don't ever think of using it for complex preprocessor directives.
#PART 1. Preprocessor Directives
###MACROS - USAGE - SCENCRIO
**#INCLUDE** Tells the preprocessor to treat the contents of a specified file as if they appear in the source program at the point where the directive appears.
#include "path-spec"
#include < path-spec >
**#DEFINE** The #define creates a macro, which is the association of an identifier or parameterized identifier with a token string. After the macro is defined, the compiler can substitute the token string for each occurrence of the identifier in the source file.
#define identifier token-stringopt
**#UNDEF** Removes (undefines) a name previously created with #define.
#undef identifier
**#IFDEF** **#IFNDEF ** The #ifdef and #ifndef directives perform the same task as the #if directive when it is used with defined( identifier ).
#ifdef identifier
#ifndef identifier
**#IF** **#ELSE** **#ENDIF** **#ELIF** The #if directive, with the #elif, #else, and #endif directives, controls compilation of portions of a source file. If the expression you write (after the #if) has a nonzero value, the line group immediately following the #if directive is retained in the translation unit.
#if defined(CREDIT)
credit();
#elif defined(DEBIT)
debit();
#else
printerror();
#endif
#PART 2. Implementation
###My project is a Visual Studio solution.
1. The PreCompiler (PreCompiler.cpp) first takes in a streamed code file, preprocessed by Serializer (serializer.h) into a structurized model.

2. Code fragments are abstracted into three types – **MACRO**, **STATEMENT** and **BLOCK**. They are all inherited from BASEMODEL (model.h).
> **STMT** Statements ended with “;”.
> **MACRO** Preprocessor directives starting with “#”.
> **BLOCK** The file can be viewed as a BLOCK. if-else, functions, classes are also interpreted into a BLOCK. A BLOCK has two fields – content (optional, if-else conditions, function params) and context vector (STMTs, MACROs, BLOCKs embedded in the block).
3. The serialized code is then processed by a Processor (stmtp.h) one after another. There is a def_trans_map in macrop.h, which records macro definitions already met. macrop.add_macro takes in mcro string and converts it into structured key-value pair. macrop.process process each statement and replace embedded macro identifiers. Other complex directives are handled by stmtp.process.
4. After the code cleaned up, a Streamlizer (streamlizer.h) turns the structured document back to a streamed file with a nice format. And the PreCompiler writes it back into the disk.
#PART 3. Examples
The given demo.cpp and outputted out.cpp.
