Thiago Adams
2017-02-14 23:37:40 UTC
I was wondering how to eliminate pre-processor
keeping compatibility with existing C code.
Let's say we move the pre-processor for parser in
a new C2 language. (file extensions .h2 and .c2)
I want to keep the original includes of system headers
as it is:
Let's say there is a flag inside the compiler
classic pre-processor ON or OFF.
So in a file.c2 that is "C2" the pre-processor mode starts as OFF
//When we include a extension .h
//the c pre-processor flat is ON
//normal C pre-processor using DEBUG, WIN32 etc..
//include itself is not normal in this file.
#include <stdio.h>
//pre-processor flag is OFF after include
//The include here is not pre-processor command
// pre-processor still OF because the file is .h2
#include "myfile.h2"
Now, let's see the new rules
#define is inside the C grammar.
If must be used in some specific places.
The same for #if #else...
The defined symbol is no more a macro. It's a symbol inside
the AST.
The #if is branch inside the AST like normal if / else.
The #define in pre-processor ON it's not the same (don't mix) with
#define when pre-processor is OFF.
sizeof and other new features can be added to #if expressions.
For instance, we can ask if the function F was defined or some variable.
Macro expansion?
Besides simple macros I don't have idea the new rules.
So I don't known what would be allowed. For the worst case, only
simple #define would work.
Why to do this? For many reasons.
For safer code, for refactoring tools, better tools etc.
What do you think?
keeping compatibility with existing C code.
Let's say we move the pre-processor for parser in
a new C2 language. (file extensions .h2 and .c2)
I want to keep the original includes of system headers
as it is:
Let's say there is a flag inside the compiler
classic pre-processor ON or OFF.
So in a file.c2 that is "C2" the pre-processor mode starts as OFF
//When we include a extension .h
//the c pre-processor flat is ON
//normal C pre-processor using DEBUG, WIN32 etc..
//include itself is not normal in this file.
#include <stdio.h>
//pre-processor flag is OFF after include
//The include here is not pre-processor command
// pre-processor still OF because the file is .h2
#include "myfile.h2"
Now, let's see the new rules
#define is inside the C grammar.
If must be used in some specific places.
The same for #if #else...
The defined symbol is no more a macro. It's a symbol inside
the AST.
The #if is branch inside the AST like normal if / else.
The #define in pre-processor ON it's not the same (don't mix) with
#define when pre-processor is OFF.
sizeof and other new features can be added to #if expressions.
For instance, we can ask if the function F was defined or some variable.
Macro expansion?
Besides simple macros I don't have idea the new rules.
So I don't known what would be allowed. For the worst case, only
simple #define would work.
Why to do this? For many reasons.
For safer code, for refactoring tools, better tools etc.
What do you think?