Skip to content

Commit

Permalink
refactor(macro processor): removing unnecessary checks and using asse…
Browse files Browse the repository at this point in the history
…rts just in case
  • Loading branch information
SuperFola committed Oct 20, 2024
1 parent af049e5 commit 430af0f
Showing 1 changed file with 11 additions and 14 deletions.
25 changes: 11 additions & 14 deletions src/arkreactor/Compiler/Macros/Processor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <utility>
#include <algorithm>
#include <cassert>
#include <ranges>
#include <sstream>
#include <fmt/core.h>
Expand Down Expand Up @@ -46,38 +47,34 @@ 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];

// ($ 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);
}
Expand Down

0 comments on commit 430af0f

Please sign in to comment.