21 Pass(
"MacroProcessor", debug)
24 m_conditional_executor = std::make_shared<ConditionalExecutor>(
this);
25 m_executors.emplace_back(std::make_shared<SymbolExecutor>(
this));
26 m_executors.emplace_back(m_conditional_executor);
27 m_executors.emplace_back(std::make_shared<FunctionExecutor>(
this));
54 assert(node.
constList().size() >= 2 &&
"Invalid macro, missing value");
56 const Node& first_node = node.
list()[0];
107 "Max recursion depth reached ({}). You most likely have a badly defined recursive macro calling itself without a proper exit condition",
113 bool has_created =
false;
116 while (i < node.
list().size())
118 const std::size_t pos = i;
121 bool added_begin =
false;
126 if ((!
m_macros.empty() && !
m_macros.back().empty() &&
m_macros.back().depth() < depth && !is_processing_namespace) ||
127 (!has_created && !is_processing_namespace) ||
128 (
m_macros.empty() && is_processing_namespace))
143 node.
list().erase(node.
constList().begin() +
static_cast<std::vector<Node>::difference_type
>(pos));
144 else if (!added_begin)
168 node.
list().erase(node.
constList().begin() +
static_cast<std::vector<Node>::difference_type
>(pos));
173 if (!
m_macros.empty() &&
m_macros.back().depth() == depth && !is_processing_namespace)
192 "Max macro processing depth reached ({}). You may have a macro trying to evaluate itself, try splitting your code in multiple nodes.",
198 if (executor->canHandle(node))
201 const bool applied = executor->applyMacro(node, depth);
213 const std::size_t argcount = node.
constList().size();
214 if (argcount != expected + 1)
219 "When expanding `{}' inside a macro, got {} argument{}, expected {}",
222 argcount > 2 ?
"s" :
"",
228 "Interpreting a `{}'{} with {} argument{}, expected {}.",
230 kind.empty() ? kind :
" " + kind,
232 argcount > 2 ?
"s" :
"",
240 const std::size_t argcount = node.
constList().size();
241 if (argcount < expected + 1)
244 "Interpreting a `{}'{} with {} argument{}, expected at least {}.",
246 kind.empty() ? kind :
" " + kind,
248 argcount > 2 ?
"s" :
"",
258 if (macro !=
nullptr && macro->
constList().size() == 2)
264 const std::string& name = node.
list()[0].string();
265 const std::size_t argcount = node.
list().size() - 1;
273 else if (name ==
"=" && is_not_body)
280 else if (name ==
"!=" && is_not_body)
287 else if (name ==
"<" && is_not_body)
294 else if (name ==
">" && is_not_body)
301 else if (name ==
"<=" && is_not_body)
308 else if (name ==
">=" && is_not_body)
315 else if (name ==
"+" && is_not_body)
319 for (
auto& child : node.
list() | std::ranges::views::drop(1))
328 else if (name ==
"-" && is_not_body)
336 for (
auto& child : node.
list() | std::ranges::views::drop(2))
345 else if (name ==
"*" && is_not_body)
349 for (
auto& child : node.
list() | std::ranges::views::drop(1))
358 else if (name ==
"/" && is_not_body)
366 for (
auto& child : node.
list() | std::ranges::views::drop(2))
375 else if (name ==
"not" && is_not_body)
382 if (node.
list().size() < 3)
385 for (std::size_t i = 1, end = node.
list().size(); i < end; ++i)
394 if (node.
list().size() < 3)
397 for (std::size_t i = 1, end = node.
list().size(); i < end; ++i)
404 else if (name ==
"len")
412 if (!lst.list().empty() && lst.list()[0] ==
getListNode())
419 else if (name ==
"empty?")
426 if (!lst.list().empty() && lst.list()[0] ==
getListNode())
434 else if (name ==
"@")
443 const std::size_t size = sublist.
list().size();
444 std::size_t real_size = size;
445 long num_idx =
static_cast<long>(idx.
number());
456 if (num_idx >= 0 && std::cmp_less(num_idx, size))
457 output = sublist.
list()[
static_cast<std::size_t
>(num_idx)];
458 else if (
const auto c =
static_cast<long>(size) + num_idx; num_idx < 0 && std::cmp_less(c, size) && c >= 0)
459 output = sublist.
list()[
static_cast<std::size_t
>(c)];
467 else if (name ==
"head")
484 else if (!sublist.
list().empty())
490 else if (name ==
"tail")
499 if (sublist.
list().size() > 1)
510 else if (!sublist.
list().empty())
525 if (node.
list().size() <= 2)
530 "When expanding `{}', expected the first argument to be a Symbol, got a {}: {}",
533 node.
list()[1].repr()),
536 std::string sym = node.
list()[1].string();
538 for (std::size_t i = 2, end = node.
list().size(); i < end; ++i)
557 "When expanding `{}', expected either a Number, String or Symbol, got a {}: {}",
594 if (node.
list().size() != 2)
600 if (node.
list().size() != 2)
613 "When expanding `{}', got a {}. Can not un-define a macro without a valid name",
619 if (node.
list().size() != 2)
629 for (
auto& child : node.
list())
630 child.updateValueAndType(
evaluate(child, depth + 1, is_not_body));
643 if (node.
string() ==
"true")
667 for (
const auto& m_macro : std::ranges::reverse_view(
m_macros))
669 if (
const auto res = m_macro.has(name); res !=
nullptr)
680 for (
auto& m_macro : std::ranges::reverse_view(
m_macros))
682 if (m_macro.remove(name))
707 const std::size_t previous = i;
709 for (std::size_t block_idx = 1, end = lst.
constList().size(); block_idx < end; ++block_idx)
711 node.
constList().begin() +
static_cast<std::vector<Node>::difference_type
>(i + block_idx),
712 lst.
list()[block_idx]);
714 node.
list().erase(node.
constList().begin() +
static_cast<std::vector<Node>::difference_type
>(previous));
727 [&node](
const std::pair<std::string, Value>& element) ->
bool {
728 return node.
string() == element.first;
734 node.
string() ==
"list" ||
739 return std::ranges::all_of(node.
constList(), [
this](
const Node& child) {
740 return isConstEval(child);
762 const std::optional<CodeErrorContext> maybe_context = [
this]() -> std::optional<CodeErrorContext> {
Host the declaration of all the ArkScript builtins.
Executor for Conditional Macros.
Constants used by ArkScript.
ArkScript homemade exceptions.
The base class for all MacroExecutors.
Executor for List Macros.
Handles the macros and their expansion in ArkScript source code.
Executor for Symbol Macros.
void debug(const Logger::MessageAndLocation &data, Args &&... args)
Write a debug level log using fmtlib.
void trace(const char *fmt, Args &&... args)
Write a trace level log using fmtlib.
void traceStart(std::string &&trace_name)
void checkMacroArgCountEq(const Node &node, std::size_t expected, const std::string &name, bool is_expansion=false, const std::string &kind="")
Check if the given node has exactly the provided argument count, otherwise throws an error.
Node evaluate(Node &node, unsigned depth, bool is_not_body=false)
Evaluate only the macros.
void registerFuncDef(const Node &node)
Registers a function definition node.
const Node & ast() const noexcept override
Return the modified AST.
std::shared_ptr< MacroExecutor > m_conditional_executor
bool applyMacro(Node &node, unsigned depth)
Apply a macro on a given node.
static bool isBeginNode(const Node &node)
Check if a given node is a list node, and starts with a Begin.
static void removeBegin(Node &node, std::size_t i)
Remove a begin block added by a macro.
std::optional< Node > lookupDefinedFunction(const std::string &name) const
Return std::nullopt if the function isn't registered, otherwise return its node.
std::vector< Node > m_macros_being_applied
Stack of macros being applied. The last one is the current one we are working on.
const Node * findNearestMacro(const std::string &name) const
Find the nearest macro matching a given name.
Node m_ast
The modified AST.
void throwMacroProcessingError(const std::string &message, const Node &node) const
Throw a macro processing error.
bool isTruthy(const Node &node)
Check if a node can be evaluated to true.
std::unordered_map< std::string, Node > m_defined_functions
bool isConstEval(const Node &node) const
Check if a node can be evaluated at compile time.
std::vector< std::shared_ptr< MacroExecutor > > m_executors
void process(const Node &ast) override
Send the complete AST and work on it.
MacroProcessor(unsigned debug) noexcept
Construct a new Macro Processor object.
void processNode(Node &node, unsigned depth, bool is_processing_namespace=false)
Register macros in scopes and apply them as needed.
std::vector< MacroScope > m_macros
Handling macros in a scope fashion.
void checkMacroArgCountGe(const Node &node, std::size_t expected, const std::string &name, const std::string &kind="")
Check if the given node has at least the provided argument count, otherwise throws an error.
void handleMacroNode(Node &node)
Registers macros based on their type, expand conditional macros.
void deleteNearestMacro(const std::string &name)
Find the nearest macro matching a given name and delete it.
A node of an Abstract Syntax Tree for ArkScript.
NodeType nodeType() const noexcept
Return the node type.
void setNodeType(NodeType type) noexcept
Set the Node Type object.
bool isListLike() const noexcept
Check if the node is a list like node.
const std::string & filename() const noexcept
Return the filename in which this node was created.
const std::string & string() const noexcept
Return the string held by the value (if the node type allows it)
const std::vector< Node > & constList() const noexcept
Return the list of sub-nodes held by the node.
Keyword keyword() const noexcept
Return the keyword held by the value (if the node type allows it)
Namespace & arkNamespace() noexcept
Return the namespace held by the value (if the node type allows it)
std::string repr() const noexcept
Compute a representation of the node without any comments or additional sugar, colors,...
void setPositionFrom(const Node &source) noexcept
Position the current node at a given span in a file.
std::ostream & debugPrint(std::ostream &os) const noexcept
Print a node to an output stream with added type annotations.
FileSpan position() const noexcept
Get the span of the node (start and end)
void push_back(const Node &node) noexcept
Every node has a list as well as a value so we can push_back on all node no matter their type.
void setString(const std::string &value) noexcept
Set the String object.
double number() const noexcept
Return the number held by the value (if the node type allows it)
void updateValueAndType(const Node &source) noexcept
Copy a node to the current one, while keeping the filename and position in the file.
std::vector< Node > & list() noexcept
Return the list of sub-nodes held by the node.
An interface to describe compiler passes.
ARK_API const std::vector< std::pair< std::string, Value > > builtins
constexpr std::string_view AsIs
constexpr std::string_view Type
constexpr std::string_view Argcount
constexpr std::array< std::string_view, 24 > operators
constexpr std::string_view And
constexpr std::string_view Repr
constexpr std::string_view Or
constexpr std::string_view Symcat
constexpr std::string_view Undef
std::string typeToString(const Node &node) noexcept
const Node & getNilNode()
Keyword
The different keywords available.
const Node & getFalseNode()
const Node & getListNode()
const Node & getTrueNode()
constexpr std::size_t MaxMacroProcessingDepth
Controls the number of recursive calls to MacroProcessor::processNode.
std::string to_string(const Ark::ValueType type) noexcept
CodeError thrown by the compiler (parser, macro processor, optimizer, and compiler itself)
std::shared_ptr< Node > ast