Skip to content

Commit

Permalink
fix(macro processor): empty\? should work with nil when evaluated as …
Browse files Browse the repository at this point in the history
…a macro

tail also works in macros with non-list nodes
  • Loading branch information
SuperFola committed Oct 20, 2024
1 parent acd909a commit 0b08238
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 11 deletions.
20 changes: 11 additions & 9 deletions src/arkreactor/Compiler/Macros/Processor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -396,16 +396,16 @@ namespace Ark::internal
{
if (node.list().size() > 2)
throwMacroProcessingError(fmt::format("When expanding `empty?' inside a macro, got {} arguments, expected 1", argcount), node);
if (Node& lst = node.list()[1]; lst.nodeType() == NodeType::List) // only apply len at compile time if we can
if (Node& lst = node.list()[1]; lst.nodeType() == NodeType::List && isConstEval(lst))
{
if (isConstEval(lst))
{
if (!lst.list().empty() && lst.list()[0] == getListNode())
setWithFileAttributes(node, node, lst.list().size() - 1 == 0 ? getTrueNode() : getFalseNode());
else
setWithFileAttributes(node, node, lst.list().size() == 0 ? getTrueNode() : getFalseNode());
}
// only apply len at compile time if we can
if (!lst.list().empty() && lst.list()[0] == getListNode())
setWithFileAttributes(node, node, lst.list().size() - 1 == 0 ? getTrueNode() : getFalseNode());
else
setWithFileAttributes(node, node, lst.list().empty() ? getTrueNode() : getFalseNode());
}
else if (lst == getNilNode())
setWithFileAttributes(node, node, getTrueNode());
}
else if (name == "@")
{
Expand Down Expand Up @@ -487,6 +487,7 @@ namespace Ark::internal
else if (!sublist.list().empty())
{
sublist.list().erase(sublist.constList().begin());
sublist.list().insert(sublist.list().begin(), getListNode());
setWithFileAttributes(node, node, sublist);
}
else
Expand Down Expand Up @@ -692,7 +693,8 @@ namespace Ark::internal
return it != Language::operators.end() ||
it2 != Builtins::builtins.end() ||
findNearestMacro(node.string()) != nullptr ||
node.string() == "list";
node.string() == "list" ||
node.string() == "nil";
}

case NodeType::List:
Expand Down
19 changes: 17 additions & 2 deletions tests/unittests/resources/LangSuite/macro-tests.ark
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,23 @@
(test:eq (add_two 1 2) 3)
(test:eq (add_two nice_value 2) 14)

($ c (* 4 (/ 10 2)))
(test:eq c 20) })
($ c (a b) (* a b))
(test:eq (c 4 10) 40)

($ d (a b) (/ a b))
(test:eq (d 10 2) 5) })

(test:case "node manipulation" {
($ node_tail () (tail (begin 1 2 3)))
($ length () (len (fun () 5)))
($ not_empty_node () (empty? (fun () ())))
($ empty_node () (empty? ()))

(test:eq (length) 3)
(test:eq (not_empty_node) false)
(test:eq (empty_node) true)
# because it removes the "begin"
(test:eq (node_tail) [1 2 3]) })

(test:case "conditional macros" {
(test:expect ($if (and true true) true false))
Expand Down

0 comments on commit 0b08238

Please sign in to comment.