From 2da7b3f9a0fc067d7652ac4a8158d7ac0d24c1bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrius=20=C5=A0tikonas?= Date: Tue, 2 Nov 2021 21:51:38 +0000 Subject: [PATCH] Implement #undef. --- cc_macro.c | 40 ++++++++++++++++++++++++++++++++++++++ test/test.answers | 12 ++++++------ test/test1000/proof.answer | 2 +- 3 files changed, 47 insertions(+), 7 deletions(-) diff --git a/cc_macro.c b/cc_macro.c index 37626b2..82d010f 100644 --- a/cc_macro.c +++ b/cc_macro.c @@ -142,6 +142,36 @@ struct macro_list* lookup_macro(struct token_list* token) return NULL; } +void remove_macro(struct token_list* token) +{ + struct macro_list* hold = macro_env; + struct macro_list* temp; + + /* Deal with the first element */ + if (match(token->s, hold->symbol)) { + macro_env = hold->next; + free(hold); + return; + } + + /* Remove element form the middle of linked list */ + while (NULL != hold->next) + { + if (match(token->s, hold->next->symbol)) + { + temp = hold->next; + hold->next = hold->next->next; + free(temp); + return; + } + + hold = hold->next; + } + + /* nothing to undefine */ + return; +} + int macro_expression(); int macro_variable() { @@ -423,7 +453,13 @@ void handle_define() eat_current_token(); } +} +void handle_undef() +{ + eat_current_token(); + remove_macro(macro_token); + eat_current_token(); } void handle_error() @@ -576,6 +612,10 @@ void macro_directive() { handle_define(); } + else if(match("#undef", macro_token->s)) + { + handle_undef(); + } else if(match("#error", macro_token->s)) { handle_error(); diff --git a/test/test.answers b/test/test.answers index f2705f6..305691a 100644 --- a/test/test.answers +++ b/test/test.answers @@ -212,9 +212,9 @@ eb1f6aab09a1a4966f9f49a518d8aa03ca97aa7c46e443871d758a306671a85e test/results/t a2cbfd5f76d3be8049c6737e3fa02f191caf29b6885db006fa23f86e6eacc186 test/results/test0106-knight-posix-binary d75e450e2fcdf19df63f9d6a3fe5e032933e57b33f6f06b39a8ed2f3dc759f17 test/results/test0106-riscv64-binary 473cc504d6cba9eaf648abcf15c83a317ff3f4a7f08f5e8936e3b35b2cc4fbc6 test/results/test0106-x86-binary -bf1d23078b04a88ad8eeb615a3208f76cf1c2bd4c7070452f887c569ad94b684 test/results/test1000-aarch64-binary -aeb6d1cbb3616b8e6fc20b195dcc164cc77c835810c1a3c90504c3749f662385 test/results/test1000-amd64-binary -bb88a105f120d8dc1d34de87114b371363801fbcf4341e510bcb5bcc08e76037 test/results/test1000-armv7l-binary -85318559fd43f582cefbaf60e4300a59e9589745782b4a9083601152d231fcb6 test/results/test1000-knight-posix-binary -de7107777e9008b8467c52c5fab7ca91580b4216cf0f6c6737ee538cfb0f2ab3 test/results/test1000-riscv64-binary -bd66e02f6bfa34187d36116c147f32977c4e3bc34f46df66da4fe828984727e8 test/results/test1000-x86-binary +163b9e09a19f8cd592af1ad1ff289c985e769df17eb67eea33bf7f9fff140654 test/results/test1000-aarch64-binary +c8c56324ce5e53e0ce75a0c84db88c39397f79f761b3e2fbec5714b9f058b37c test/results/test1000-amd64-binary +19e79a0a80d43e22f1cbd1636985b362273fb2c8bafe27eede2f79aea16b07e5 test/results/test1000-armv7l-binary +b0c4cb6ba049f97704df3e3694b47364c1302b28e88d87065b27887642ebda76 test/results/test1000-knight-posix-binary +efe53ac2f7bd3bce3823160232da05af3b8e21e576a38f694272b9dc577a2baa test/results/test1000-riscv64-binary +430978cbac169ce8447eb485632698e5b35abcbb49d4d5156a6b66a607117b32 test/results/test1000-x86-binary diff --git a/test/test1000/proof.answer b/test/test1000/proof.answer index be43fc5..910e114 100644 --- a/test/test1000/proof.answer +++ b/test/test1000/proof.answer @@ -1 +1 @@ -e018a50ea8e2547020473cfdc60066a41cd3793b9d7c164bba5ce7e80a06dd8b test/test1000/proof +d9883c3c138fcf943bab6320855cbb30dfa8a7384c6431d4174cb4c137ed644f test/test1000/proof