From 397a7dfcd4ba6eaf22e8726e86bff0a164d9574a Mon Sep 17 00:00:00 2001 From: Alexandre Plateau Date: Sun, 23 Jun 2024 18:59:58 +0200 Subject: [PATCH] feat(macro processor): adding $paste to stop node evalution inside macros --- CHANGELOG.md | 1 + lib/std | 2 +- src/arkreactor/Compiler/Macros/Processor.cpp | 9 ++++++++- tests/arkscript/builtins-tests.ark | 21 ++++++++++---------- 4 files changed, 20 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 97ef86fe6..9e3ab61aa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,7 @@ - check on number of arguments passed to `type` - warning when the formatter deletes comment(s) by mistake - check on arguments passed to `list`, `concat`, `append` and friends to only push valid nodes (that produces a value) +- `$paste` to paste a node inside a maro without evaluating it further ; useful to stop recursive evaluation of nodes inside function macros ### Changed - instructions are on 4 bytes: 1 byte for the instruction, 1 byte of padding, 2 bytes for an immediate argument diff --git a/lib/std b/lib/std index aae15fe5c..c579744df 160000 --- a/lib/std +++ b/lib/std @@ -1 +1 @@ -Subproject commit aae15fe5ca56e6b268068715aab79293a259a407 +Subproject commit c579744dfac36b2149d3c2ebeea917e3da6df738 diff --git a/src/arkreactor/Compiler/Macros/Processor.cpp b/src/arkreactor/Compiler/Macros/Processor.cpp index 51dcb66a1..b021d88ac 100644 --- a/src/arkreactor/Compiler/Macros/Processor.cpp +++ b/src/arkreactor/Compiler/Macros/Processor.cpp @@ -25,7 +25,8 @@ namespace Ark::internal m_predefined_macros = { "symcat", "argcount", - "$repr" // TODO: unify predefined macro names (update documentation and examples and tests) + "$repr", // TODO: unify predefined macro names (update documentation and examples and tests) + "$paste" }; } @@ -446,6 +447,12 @@ namespace Ark::internal const Node ast = node.constList()[1]; node = Node(NodeType::String, ast.repr()); } + else if (name == "$paste") + { + if (node.list().size() != 2) + throwMacroProcessingError(fmt::format("When expanding `$paste', expected one argument, got {} arguments", argcount), node); + return node.constList()[1]; + } } if (node.nodeType() == NodeType::List && !node.constList().empty()) diff --git a/tests/arkscript/builtins-tests.ark b/tests/arkscript/builtins-tests.ark index f4737e7c6..f3632dc2d 100644 --- a/tests/arkscript/builtins-tests.ark +++ b/tests/arkscript/builtins-tests.ark @@ -20,17 +20,16 @@ (test:eq (list:sort [5]) [5]) (test:eq (list:sort []) []) - # fixme - #(let short_list (list:fill 12 nil)) - #(test:eq (len short_list) 12) - #(mut i 0) - #(while (< i 12) { - # (test:eq (@ short_list i) nil) - # (set i (+ 1 i))}) - #(del i) -# - #(test:eq (@ (list:setAt short_list 5 "a") 5) "a") - #(del short_list) + (let short_list (list:fill 12 nil)) + (test:eq (len short_list) 12) + (mut i 0) + (while (< i 12) { + (test:eq (@ short_list i) nil) + (set i (+ 1 i))}) + (del i) + + (test:eq (@ (list:setAt short_list 5 "a") 5) "a") + (del short_list) (test:expect (not (io:fileExists? "test.txt"))) (io:writeFile "test.txt" "hello, world!")