From 430af0fa7dc6c021cdb8866d266cc7b38d980e4d Mon Sep 17 00:00:00 2001 From: Alexandre Plateau Date: Sun, 20 Oct 2024 23:32:54 +0200 Subject: [PATCH] refactor(macro processor): removing unnecessary checks and using asserts just in case --- src/arkreactor/Compiler/Macros/Processor.cpp | 25 +++++++++----------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/src/arkreactor/Compiler/Macros/Processor.cpp b/src/arkreactor/Compiler/Macros/Processor.cpp index 1574859b5..495396760 100644 --- a/src/arkreactor/Compiler/Macros/Processor.cpp +++ b/src/arkreactor/Compiler/Macros/Processor.cpp @@ -2,6 +2,7 @@ #include #include +#include #include #include #include @@ -46,8 +47,8 @@ namespace Ark::internal void MacroProcessor::registerMacro(Node& node) { // a macro needs at least 2 nodes, name + value is the minimal form - if (node.constList().size() < 2) - throwMacroProcessingError("Invalid macro, missing value", node); + // this is guaranted by the parser + assert(node.constList().size() >= 2 && "Invalid macro, missing value"); const Node& first_node = node.list()[0]; const Node& second_node = node.list()[1]; @@ -55,29 +56,25 @@ namespace Ark::internal // ($ name value) if (node.constList().size() == 2) { - if (first_node.nodeType() == NodeType::Symbol) - m_macros.back().add(first_node.string(), node); - else - throwMacroProcessingError("Can not define a macro without a symbol", first_node); + assert(first_node.nodeType() == NodeType::Symbol && "Can not define a macro without a symbol"); + m_macros.back().add(first_node.string(), node); } // ($ name (args) body) else if (node.constList().size() == 3 && first_node.nodeType() == NodeType::Symbol) { - if (second_node.nodeType() != NodeType::List) - throwMacroProcessingError("Invalid macro argument's list", second_node); + assert(second_node.nodeType() == NodeType::List && "Invalid macro argument's list"); + bool had_spread = false; for (const Node& n : second_node.constList()) { - if (n.nodeType() != NodeType::Symbol && n.nodeType() != NodeType::Spread) - throwMacroProcessingError("Invalid macro argument's list, expected symbols", n); + assert((n.nodeType() == NodeType::Symbol || n.nodeType() == NodeType::Spread) && "Invalid macro argument's list, expected symbols"); if (n.nodeType() == NodeType::Spread) { - if (had_spread) - throwMacroProcessingError("Invalid macro, multiple spread detected in argument list but only one is allowed", n); + assert(!had_spread && "Invalid macro, multiple spread detected in argument list but only one is allowed"); had_spread = true; } - else if (had_spread && n.nodeType() == NodeType::Symbol) - throwMacroProcessingError(fmt::format("Invalid macro, a spread should mark the end of an argument list, but found another argument: {}", n.string()), n); + + assert(!(had_spread && n.nodeType() == NodeType::Symbol) && "Invalid macro, a spread should mark the end of an argument list, but found another argument"); } m_macros.back().add(first_node.string(), node); }