Commit Graph

35 Commits

Author SHA1 Message Date
Andrius Štikonas b908dac7c5 Fix macro processing when they have brackets. 2023-01-08 00:56:57 +00:00
Jeremiah Orians 286964d5f8
Properly remove blocks that aren't executed 2022-12-10 20:55:41 -05:00
Jeremiah Orians 62c6f6c80e
Catching another segfault 2022-09-21 23:53:07 -04:00
Jeremiah Orians c50dcade62
Even more segfaults 2022-05-19 19:40:19 -04:00
Jeremiah Orians fba6dffb66
Fix checksums and add support for #warning 2022-05-09 06:55:22 -04:00
Jeremiah Orians 406d0856df
Backport macro functionality to better handle nested #if statements 2022-01-21 22:35:45 -05:00
Andrius Štikonas 575f91cac1 Fix typos / add copyright. 2021-11-11 15:21:28 +00:00
Jeremiah Orians 6d4d6c56ea
Catch the special case of attempting to remove a NULL macro token 2021-11-07 19:49:57 -05:00
Jeremiah Orians cdb09b1608
Catch trying to expand nulls 2021-11-07 00:19:20 -04:00
Jeremiah Orians ff65efa99e
Catch lookup_macro being passed NULL 2021-11-06 22:49:34 -04:00
Jeremiah Orians 876ebe7c68
catch half broken #define (statements 2021-11-06 22:36:02 -04:00
Jeremiah Orians 9997d7a275
catch garbage at the end of a #define 2021-11-06 21:55:22 -04:00
Andrius Štikonas 2da7b3f9a0 Implement #undef. 2021-11-02 22:04:11 +00:00
Andrius Štikonas de8f47fa9c Add support for evaluating macro variables. 2021-11-01 20:49:11 +00:00
Andrius Štikonas 7de9e684a6 Implement #error statement in preprocessor. 2021-10-31 23:33:00 +00:00
Andrius Štikonas 7a7bfd4529 Implement #ifdef and #ifndef. 2021-10-30 22:51:17 +01:00
Andrius Štikonas 945743e2c6 Add support for empty defines. 2021-10-30 22:16:24 +01:00
Jeremiah Orians d8fb878d7a
Correct #define behavior inside of #if/#elif/#else/#endif blocks 2021-10-27 22:22:49 -04:00
Jeremiah Orians 55929d4ddf
Catch segfault for half defined #defines and provide a warning for #unkowns 2021-10-21 21:27:16 -04:00
Jeremiah Orians d80d8a652b
Move more functionality into M2libc 2021-04-03 22:41:52 -04:00
Jeremiah Orians 46cf81af83
Replace file_print with fputs 2021-04-03 18:56:55 -04:00
Jeremiah Orians 8007ae9ce6
Add support for #if defined(__M2__) to enable M2-Planet specific functionality 2021-02-05 20:39:54 -05:00
Sanne Wouda 09acd6253d Implement simple macro expansion
- no longer generate CONSTANT lines for #defined constants

- "#define FOO 1" turns "return FOO;" into "return 1;"

- Macros expanding into a macro is not supported:
  #define FOO 1
  #define BAR FOO
  will expand BAR to FOO, not to 1

- Expansion inside #if conditions is not supported.

- Function-like macros are not supported.
2021-01-09 17:39:48 +00:00
Sanne Wouda a8551f2fcd Implement --bootstrap-mode for preprocessor
- // and # lines always appear in the result of read_all_tokens

- in bootstrap mode, // tokens (but not the subsequent lines) and #
  lines are stripped. no preprocessing happens.

- in non-bootstrap mode, // lines are stripped and # lines stay for the
  preprocessing phase

- updates tests to pass --bootstrap-mode when necessary
2021-01-09 17:38:50 +00:00
Sanne Wouda 4550eec19e Extend macro #define support
- now supports #if defined(FOO)
- translation of #define FOO 1 to CONSTANT FOO 1 is still supported
2021-01-08 21:31:43 +00:00
Jeremiah Orians 3c4c09b95e
Added hacky version of #define support 2021-01-06 19:24:16 -05:00
Jeremiah Orians 25fd5e66c9
Another set of segfaults cleared out 2021-01-05 23:22:26 -05:00
Jeremiah Orians bdd4e65580
Catch some basic segfaults 2021-01-05 23:00:58 -05:00
Jeremiah Orians 0ab7cb78eb
Adjusted cc_macro.c formatting with astyle -A1tSxejz2fpUxV 2021-01-05 22:22:33 -05:00
Sanne Wouda 63bb59404d Improve error reporting for unexpected non-newline 2021-01-05 22:56:30 +00:00
Sanne Wouda 49fbe8942f Add error checking for unexpected #endif 2021-01-05 22:56:30 +00:00
Sanne Wouda 89c6513cd8 Add support for conditional inclusion
- support for #if, #elif, #else, #endif, but no macro expansion just
  yet. Any macro is treated as undefined and expands to 0.

for example:
./bin/M2-Planet -E test.c

 #if 1 == FOO
foo
 #else
bar
 #endif

results in

bar
2021-01-05 22:54:30 +00:00
Sanne Wouda cd96f65a23 Add macro directives to token_list
- change the lexer to not remove preprocessor directives
- preprocess() removes any lines starting with a macro directive that it
  doesn't understand (which is currently all of them)
2021-01-05 22:53:39 +00:00
Sanne Wouda 7b8a99db3c Add a preprocessor-only mode
- Very useful for testing and debugging
- Add preprocess() stub and skip compilation in preprocessor mode
2021-01-05 22:53:04 +00:00
Sanne Wouda a18e0c1782 Add newline tokens to parse stream
- needed to determine start and end of macro directives
- the main parser doesn't need them, so strip out the newline tokens
  before parsing to avoid changing it
2021-01-05 22:23:15 +00:00