From 5b35383edfab40cb2e718466e7017534caa19964 Mon Sep 17 00:00:00 2001 From: Aryadev Chavali Date: Wed, 10 Jul 2024 02:09:12 +0100 Subject: [PATCH] Preprocesser: Better redefining of constants A constant can only be redefined by a file that is closer to the root. If a constant is defined at depth n, it can only be redefined at depths lower than n. --- src/preprocesser.cpp | 13 ++++++------- src/preprocesser.hpp | 1 + 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/preprocesser.cpp b/src/preprocesser.cpp index d1dd892..f641c74 100644 --- a/src/preprocesser.cpp +++ b/src/preprocesser.cpp @@ -62,12 +62,11 @@ namespace Preprocesser else if (end - i == 2) return new Err{ET::EMPTY_CONST, token}; - // If this content is actually being included (depth > 0) by another - // file, check if the constant is already defined. If so, stop what - // we're doing and continue because user has technically redefined the - // constant. Implements a #ifndef guard automatically on included - // constants. - if (depth > 0 && const_map.find(const_name) != const_map.end()) + // Check if we're redefining a constant. If the current depth is + // equivalent or higher than the depth when the constant was defined, + // then stop. + if (const_map.find(const_name) != const_map.end() && + const_map[const_name].depth <= depth) { i = end; #if VERBOSE >= 2 @@ -83,7 +82,7 @@ namespace Preprocesser std::copy(std::begin(tokens) + i + 2, std::begin(tokens) + end, std::begin(body)); - const_map[const_name] = {token, body}; + const_map[const_name] = {token, body, depth}; i = end; #if VERBOSE >= 2 diff --git a/src/preprocesser.hpp b/src/preprocesser.hpp index 3378428..0fe81cc 100644 --- a/src/preprocesser.hpp +++ b/src/preprocesser.hpp @@ -29,6 +29,7 @@ namespace Preprocesser { Lexer::Token *root; std::vector body; + int depth; }; typedef std::unordered_map Map;