-
Notifications
You must be signed in to change notification settings - Fork 27
Support preprocessing #5
Comments
This probably deserves its own pass. |
Ok I'm actually thinking about this seriously now. The first step is to figure out how to preserve locations for error messages. I think I can start with |
There's lots of things that are shared between the lexer and preprocessor, it's a weird mix. I'd like to reuse the code but I'd also like to be able to preprocess things on their own. I guess I don't actually need to make them separate to reuse the functions as long as I can go from |
Working on this in https://github.com/jyn514/rcc/tree/cpp |
Ok how about this - the preprocessor runs after the lexer and the lexer just collects the tokens and puts them an a mini AST. That leaves a clean break between syntax and semantics. The only issues I see are |
I could special case line in the lexer, that shouldn't be too hacky. Something like this: if let CppToken::Line(n) = token {
self.location.line = n;
} and then everything else still gets done in the preprocessor. I like that idea, I think I'll do it. |
Actually this can't go after the lexer because it's affected by whitespace :( These have different meanings: #define f(a) a
f(a) // emits a
#define f (a) a
f(a) // emits (a) a |
Crazy idea: substitute |
That works well for |
Update on It has to support arbitrary C expressions in an |
This gets even worse: not all valid C expressions are valid preprocessor expressions. For examples: $ run_clang
#if (int)1
#endif
<stdin>:1:10: error: token is not a valid binary operator in a preprocessor subexpression $ run_clang
#if 1 = 1
#endif
<stdin>:2:7: error: token is not a valid binary operator in a preprocessor subexpression $ run_clang
run_clang
#if 1.31 + 1
#endif
<stdin>:1:5: error: floating point literal in preprocessor expression |
Test cases: https://github.com/pfultz2/Cloak |
Section 6.10:
#if
/#ifdef
/#ifndef
/#elif
/#else
/defined
conditional compilation (added in First pass at preprocessor #184)#include
headers#define
macros and substitutions__VA_ARGS__
#
and##
operatorsundef
#line
control (can be ignored for now, waiting on Emit debug info #152, Allow setting custom line numbers brendanzab/codespan#157)#error
directives#pragma
_Pragma ()
#
on its own (ignored)__STDC_NO_ATOMICS__
__STDC_NO_COMPLEX__
__STDC_NO_THREADS__
__STDC_NO_VLA__
The text was updated successfully, but these errors were encountered: