From f08cdc8e22e06bbf55ce6fa1f36654589e620a81 Mon Sep 17 00:00:00 2001 From: pommicket Date: Sun, 9 Jan 2022 12:31:35 -0500 Subject: start fmacro replacement --- 05/main.b | 3 ++- 05/main.c | 15 ++++------- 05/preprocess.b | 80 +++++++++++++++++++++++++++++++++++++++++---------------- 3 files changed, 65 insertions(+), 33 deletions(-) (limited to '05') diff --git a/05/main.b b/05/main.b index 041979e..67634d1 100644 --- a/05/main.b +++ b/05/main.b @@ -45,7 +45,8 @@ function main local output_filename local pptokens - dat_banned_macros = 255 + dat_banned_objmacros = 255 + dat_banned_fmacros = 255 object_macros = malloc(4000000) function_macros = malloc(4000000) diff --git a/05/main.c b/05/main.c index 3186a09..92066af 100644 --- a/05/main.c +++ b/05/main.c @@ -1,10 +1,5 @@ -#define B A C A B A -#define A helo what -#define C how is it going - -#define D E -#define E D -B ! - -D -E +#define TEST(x) hello +#define TEST heloo 2 +TEST(55) +main(void) { +} diff --git a/05/preprocess.b b/05/preprocess.b index 2b5ca93..182a197 100644 --- a/05/preprocess.b +++ b/05/preprocess.b @@ -611,59 +611,95 @@ function look_up_function_macro function macro_replacement argument p_in argument p_out - global 2000 dat_banned_macros ; 255-terminated array of strings (initialized in main) - local old_banned_macros_end - local banned_macros + ; "banned" macros prevent #define x x from being a problem + ; C89 § 3.8.3.4 + ; "If the name of the macro being replaced is found during this scan + ; of the replacement list, it is not replaced. Further, if any nested + ; replacements encounter the name of the macro being replaced, it is not replaced." + global 2000 dat_banned_objmacros ; 255-terminated array of strings (initialized in main) + local old_banned_objmacros_end + global 2000 dat_banned_fmacros + local old_banned_fmacros_end + local banned_fmacros + local banned_objmacros local b local p + local q local replacement local in local out in = *8p_in out = *8p_out + banned_objmacros = &dat_banned_objmacros + banned_fmacros = &dat_banned_fmacros + old_banned_objmacros_end = memchr(banned_objmacros, 255) + old_banned_fmacros_end = memchr(banned_fmacros, 255) - ; "banned" macros prevent #define x x from being a problem - ; C89 § 3.8.3.4 - ; "If the name of the macro being replaced is found during this scan - ; of the replacement list, it is not replaced. Further, if any nested - ; replacements encounter the name of the macro being replaced, it is not replaced." + p = in + pptoken_skip(&p) + if *1p == '( goto fmacro_replacement - banned_macros = &dat_banned_macros - p = banned_macros + p = banned_objmacros - old_banned_macros_end = memchr(banned_macros, 255) - :check_banned_macros_loop - if *1p == 255 goto check_banned_macros_loop_end + :check_banned_objmacros_loop + if *1p == 255 goto check_banned_objmacros_loop_end b = str_equals(in, p) - if b != 0 goto no_replacement + if b != 0 goto done_replacement p = memchr(p, 0) p += 1 - goto check_banned_macros_loop - :check_banned_macros_loop_end + goto check_banned_objmacros_loop + :check_banned_objmacros_loop_end - p = strcpy(old_banned_macros_end, in) + ; add this to list of banned macros + p = strcpy(old_banned_objmacros_end, in) p += 1 *1p = 255 + replacement = look_up_object_macro(in) if replacement == 0 goto no_replacement p = replacement pptoken_skip(&in) :objreplace_loop - if *1p == 255 goto objreplace_loop_end + if *1p == 255 goto done_replacement macro_replacement(&p, &out) goto objreplace_loop + + :fmacro_replacement + p = banned_fmacros + :check_banned_fmacros_loop + if *1p == 255 goto check_banned_fmacros_loop_end + b = str_equals(in, p) + if b != 0 goto no_replacement + p = memchr(p, 0) + p += 1 + goto check_banned_fmacros_loop + :check_banned_fmacros_loop_end + + ; add this to list of banned macros + p = strcpy(old_banned_fmacros_end, in) + p += 1 + *1p = 255 + + replacement = look_up_function_macro(in) + if replacement == 0 goto no_replacement + p = replacement + :freplace_loop + if *1p == 255 goto done_replacement + byte 0xcc + :freplace_loop_end :no_replacement pptoken_copy_and_advance(&in, &out) ; (fallthrough) - :objreplace_loop_end + :done_replacement *8p_in = in *8p_out = out - ; unban this macro - *1old_banned_macros_end = 255 + ; unban any macros we just banned + *1old_banned_objmacros_end = 255 + *1old_banned_fmacros_end = 255 return - + function print_object_macros print_macros(object_macros) return -- cgit v1.2.3