Skip to content

Commit 39cfc80

Browse files
committed
fix(macros): renaming symcat -> $symcat, argcount -> $argcount
1 parent 9bf3a56 commit 39cfc80

File tree

15 files changed

+88
-77
lines changed

15 files changed

+88
-77
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
- checking for forbidden variable/constant name in the name & scope resolution pass, to give errors to the user before compiling some weird code
7474
- repl completion and colors are now generated automatically from the builtins, keywords & operators
7575
- fixed formating of comments inside function declarations
76+
- renamed the macros `symcat` and `argcount` to `$symcat` and `$argcount` for uniformity
7677

7778
### Removed
7879
- removed unused `NodeType::Closure`

examples/macros.ark

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
($ suffix-dup (sym x) {
22
($if (> x 1)
33
(suffix-dup sym (- x 1)))
4-
(symcat sym x)})
4+
($symcat sym x)})
55

66
($ partial (func ...defargs) {
7-
($ bloc (suffix-dup a (- (argcount func) (len defargs))))
7+
($ bloc (suffix-dup a (- ($argcount func) (len defargs))))
88
(fun (bloc) (func ...defargs bloc))
99
($undef bloc)})
1010

1111
(let test_func (fun (a b c) (* a b c)))
1212
(let test_func1 (partial test_func 1))
1313

1414
(print "Generated partial functions for test_func (a b c) => (* a b c)")
15-
(print "Expected arguments for test_func: " (argcount test_func) ", expected " 3)
16-
(print "Expected arguments for test_func1: " (argcount test_func1) ", expected " 2)
15+
(print "Expected arguments for test_func: " ($argcount test_func) ", expected " 3)
16+
(print "Expected arguments for test_func1: " ($argcount test_func1) ", expected " 2)
1717
(print "Calling them: " (test_func 1 2 3) " " (test_func1 2 3))
1818

1919
($ foo (a b) (+ a b))
@@ -90,4 +90,4 @@
9090
(+ data "-f4")}))
9191

9292
(print "We expected calls to go like this: f1, f2, f3, f4")
93-
(print (-> filename f1 f2 f3 f4)) # (f4 (f3 (f2 (f1 filename))))
93+
(print (-> filename f1 f2 f3 f4)) # (f4 (f3 (f2 (f1 filename))))

include/Ark/Compiler/Common.hpp

+31-13
Original file line numberDiff line numberDiff line change
@@ -91,19 +91,37 @@ namespace Ark::internal
9191
"pop!"
9292
};
9393

94-
// This list is related to include/Ark/Compiler/Instructions.hpp
95-
// from FIRST_OPERATOR, to LAST_OPERATOR
96-
// The order is very important
97-
constexpr std::array<std::string_view, 25> operators = {
98-
"+", "-", "*", "/",
99-
">", "<", "<=", ">=", "!=", "=",
100-
"len", "empty?", "tail", "head",
101-
"nil?", "assert",
102-
"toNumber", "toString",
103-
"@", "mod",
104-
"type", "hasField",
105-
"not"
106-
};
94+
namespace Language
95+
{
96+
constexpr std::string_view And = "and";
97+
constexpr std::string_view Or = "or";
98+
99+
constexpr std::string_view Symcat = "$symcat";
100+
constexpr std::string_view Argcount = "$argcount";
101+
constexpr std::string_view Repr = "$repr";
102+
constexpr std::string_view Paste = "$paste";
103+
104+
constexpr std::array macros = {
105+
Symcat,
106+
Argcount,
107+
Repr,
108+
Paste
109+
};
110+
111+
// This list is related to include/Ark/Compiler/Instructions.hpp
112+
// from FIRST_OPERATOR, to LAST_OPERATOR
113+
// The order is very important
114+
constexpr std::array<std::string_view, 25> operators = {
115+
"+", "-", "*", "/",
116+
">", "<", "<=", ">=", "!=", "=",
117+
"len", "empty?", "tail", "head",
118+
"nil?", "assert",
119+
"toNumber", "toString",
120+
"@", "mod",
121+
"type", "hasField",
122+
"not"
123+
};
124+
}
107125
}
108126

109127
#endif

include/Ark/Compiler/Macros/Processor.hpp

-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ namespace Ark::internal
5858
Node m_ast; ///< The modified AST
5959
std::vector<MacroScope> m_macros; ///< Handling macros in a scope fashion
6060
std::vector<std::shared_ptr<MacroExecutor>> m_executors;
61-
std::vector<std::string> m_predefined_macros; ///< Already existing macros, non-keywords, non-builtins
6261
std::unordered_map<std::string, Node> m_defined_functions;
6362

6463
/**

src/arkreactor/Compiler/Compiler.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -188,9 +188,9 @@ namespace Ark
188188

189189
std::optional<uint8_t> Compiler::getOperator(const std::string& name) noexcept
190190
{
191-
const auto it = std::ranges::find(internal::operators, name);
192-
if (it != internal::operators.end())
193-
return static_cast<uint8_t>(std::distance(internal::operators.begin(), it) + FIRST_OPERATOR);
191+
const auto it = std::ranges::find(internal::Language::operators, name);
192+
if (it != internal::Language::operators.end())
193+
return static_cast<uint8_t>(std::distance(internal::Language::operators.begin(), it) + FIRST_OPERATOR);
194194
return std::nullopt;
195195
}
196196

@@ -569,9 +569,9 @@ namespace Ark
569569
};
570570
const std::optional<ShortcircuitOp> maybe_shortcircuit =
571571
node.nodeType() == NodeType::Symbol
572-
? (node.string() == "and"
572+
? (node.string() == Language::And
573573
? std::make_optional(ShortcircuitOp::And)
574-
: (node.string() == "or"
574+
: (node.string() == Language::Or
575575
? std::make_optional(ShortcircuitOp::Or)
576576
: std::nullopt))
577577
: std::nullopt;
@@ -712,7 +712,7 @@ namespace Ark
712712
fmt::format(
713713
"can not create a chained expression (of length {}) for operator `{}'. You most likely forgot a `)'.",
714714
exp_count,
715-
operators[static_cast<std::size_t>(op.opcode - FIRST_OPERATOR)]),
715+
Language::operators[static_cast<std::size_t>(op.opcode - FIRST_OPERATOR)]),
716716
x);
717717
}
718718
}

src/arkreactor/Compiler/Macros/Processor.cpp

+18-25
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,6 @@ namespace Ark::internal
2323
m_executors = { { std::make_shared<SymbolExecutor>(this),
2424
std::make_shared<ConditionalExecutor>(this),
2525
std::make_shared<FunctionExecutor>(this) } };
26-
27-
m_predefined_macros = {
28-
"symcat",
29-
"argcount",
30-
"$repr", // TODO: unify predefined macro names (update documentation and examples and tests)
31-
"$paste"
32-
};
3326
}
3427

3528
void MacroProcessor::process(const Node& ast)
@@ -364,10 +357,10 @@ namespace Ark::internal
364357
checkMacroArgCount(node, 1, "not", "condition");
365358
return (!isTruthy(evaluate(node.list()[1], depth + 1, is_not_body))) ? getTrueNode() : getFalseNode();
366359
}
367-
else if (name == "and" && is_not_body)
360+
else if (name == Language::And && is_not_body)
368361
{
369362
if (node.list().size() < 3)
370-
throwMacroProcessingError(fmt::format("Interpreting a `and' chain with {} arguments, expected at least 2.", argcount), node);
363+
throwMacroProcessingError(fmt::format("Interpreting a `{}' chain with {} arguments, expected at least 2.", Language::And, argcount), node);
371364

372365
for (std::size_t i = 1, end = node.list().size(); i < end; ++i)
373366
{
@@ -376,10 +369,10 @@ namespace Ark::internal
376369
}
377370
return getTrueNode();
378371
}
379-
else if (name == "or" && is_not_body)
372+
else if (name == Language::Or && is_not_body)
380373
{
381374
if (node.list().size() < 3)
382-
throwMacroProcessingError(fmt::format("Interpreting an `or' chain with {} arguments, expected at least 2.", argcount), node);
375+
throwMacroProcessingError(fmt::format("Interpreting an `{}' chain with {} arguments, expected at least 2.", Language::Or, argcount), node);
383376

384377
for (std::size_t i = 1, end = node.list().size(); i < end; ++i)
385378
{
@@ -507,12 +500,12 @@ namespace Ark::internal
507500
}
508501
}
509502
}
510-
else if (name == "symcat")
503+
else if (name == Language::Symcat)
511504
{
512505
if (node.list().size() <= 2)
513-
throwMacroProcessingError(fmt::format("When expanding `symcat', expected at least 2 arguments, got {} arguments", argcount), node);
506+
throwMacroProcessingError(fmt::format("When expanding `{}', expected at least 2 arguments, got {} arguments", Language::Symcat, argcount), node);
514507
if (node.list()[1].nodeType() != NodeType::Symbol)
515-
throwMacroProcessingError(fmt::format("When expanding `symcat', expected the first argument to be a Symbol, got a {}", typeToString(node.list()[1])), node);
508+
throwMacroProcessingError(fmt::format("When expanding `{}', expected the first argument to be a Symbol, got a {}", Language::Symcat, typeToString(node.list()[1])), node);
516509

517510
std::string sym = node.list()[1].string();
518511

@@ -533,37 +526,37 @@ namespace Ark::internal
533526
break;
534527

535528
default:
536-
throwMacroProcessingError(fmt::format("When expanding `symcat', expected either a Number, String or Symbol, got a {}", typeToString(ev)), ev);
529+
throwMacroProcessingError(fmt::format("When expanding `{}', expected either a Number, String or Symbol, got a {}", Language::Symcat, typeToString(ev)), ev);
537530
}
538531
}
539532

540533
node.setNodeType(NodeType::Symbol);
541534
node.setString(sym);
542535
}
543-
else if (name == "argcount")
536+
else if (name == Language::Argcount)
544537
{
545538
Node sym = node.constList()[1];
546539
if (sym.nodeType() == NodeType::Symbol)
547540
{
548541
if (const auto it = m_defined_functions.find(sym.string()); it != m_defined_functions.end())
549542
setWithFileAttributes(node, node, Node(static_cast<long>(it->second.constList().size())));
550543
else
551-
throwMacroProcessingError(fmt::format("When expanding `argcount', expected a known function name, got unbound variable {}", sym.string()), sym);
544+
throwMacroProcessingError(fmt::format("When expanding `{}', expected a known function name, got unbound variable {}", Language::Argcount, sym.string()), sym);
552545
}
553546
else if (sym.nodeType() == NodeType::List && sym.list().size() == 3 && sym.list()[0].nodeType() == NodeType::Keyword && sym.list()[0].keyword() == Keyword::Fun)
554547
setWithFileAttributes(node, node, Node(static_cast<long>(sym.list()[1].list().size())));
555548
else
556-
throwMacroProcessingError(fmt::format("When trying to apply `argcount', got a {} instead of a Symbol or Function", typeToString(sym)), sym);
549+
throwMacroProcessingError(fmt::format("When trying to apply `{}', got a {} instead of a Symbol or Function", Language::Argcount, typeToString(sym)), sym);
557550
}
558-
else if (name == "$repr")
551+
else if (name == Language::Repr)
559552
{
560553
const Node ast = node.constList()[1];
561554
setWithFileAttributes(node, node, Node(NodeType::String, ast.repr()));
562555
}
563-
else if (name == "$paste")
556+
else if (name == Language::Paste)
564557
{
565558
if (node.list().size() != 2)
566-
throwMacroProcessingError(fmt::format("When expanding `$paste', expected one argument, got {} arguments", argcount), node);
559+
throwMacroProcessingError(fmt::format("When expanding `{}', expected one argument, got {} arguments", Language::Paste, argcount), node);
567560
return node.constList()[1];
568561
}
569562
}
@@ -626,8 +619,8 @@ namespace Ark::internal
626619

627620
bool MacroProcessor::isPredefined(const std::string& symbol)
628621
{
629-
const auto it = std::ranges::find(m_predefined_macros, symbol);
630-
return it != m_predefined_macros.end();
622+
const auto it = std::ranges::find(Language::macros, symbol);
623+
return it != Language::macros.end();
631624
}
632625

633626
void MacroProcessor::recurApply(Node& node)
@@ -674,13 +667,13 @@ namespace Ark::internal
674667
{
675668
case NodeType::Symbol:
676669
{
677-
const auto it = std::ranges::find(operators, node.string());
670+
const auto it = std::ranges::find(Language::operators, node.string());
678671
const auto it2 = std::ranges::find_if(Builtins::builtins,
679672
[&node](const std::pair<std::string, Value>& element) -> bool {
680673
return node.string() == element.first;
681674
});
682675

683-
return it != operators.end() ||
676+
return it != Language::operators.end() ||
684677
it2 != Builtins::builtins.end() ||
685678
findNearestMacro(node.string()) != nullptr ||
686679
node.string() == "list";

src/arkreactor/Compiler/NameResolutionPass.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,13 @@ namespace Ark::internal
7171
{
7272
for (const auto& builtin : Builtins::builtins)
7373
m_language_symbols.emplace(builtin.first);
74-
for (auto ope : operators)
74+
for (auto ope : Language::operators)
7575
m_language_symbols.emplace(ope);
7676
for (auto inst : listInstructions)
7777
m_language_symbols.emplace(inst);
7878

79-
m_language_symbols.emplace("and");
80-
m_language_symbols.emplace("or");
79+
m_language_symbols.emplace(Language::And);
80+
m_language_symbols.emplace(Language::Or);
8181
}
8282

8383
void NameResolutionPass::process(const Node& ast)

src/arkscript/REPL/Repl.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,22 @@ namespace Ark
1919
m_old_ip(0), m_lib_env(lib_env),
2020
m_state(m_lib_env), m_vm(m_state), m_has_init_vm(false)
2121
{
22-
m_keywords.reserve(keywords.size() + listInstructions.size() + operators.size() + Builtins::builtins.size());
22+
m_keywords.reserve(keywords.size() + listInstructions.size() + Language::operators.size() + Builtins::builtins.size());
2323
for (auto keyword : keywords)
2424
m_keywords.emplace_back(keyword);
2525
for (auto inst : listInstructions)
2626
m_keywords.emplace_back(inst);
27-
for (auto op : operators)
27+
for (auto op : Language::operators)
2828
m_keywords.emplace_back(op);
2929
for (const auto& builtin : std::ranges::views::keys(Builtins::builtins))
3030
m_keywords.push_back(builtin);
3131

32-
m_words_colors.reserve(keywords.size() + listInstructions.size() + operators.size() + Builtins::builtins.size() + 2);
32+
m_words_colors.reserve(keywords.size() + listInstructions.size() + Language::operators.size() + Builtins::builtins.size() + 2);
3333
for (auto keyword : keywords)
3434
m_words_colors.emplace_back(keyword, Replxx::Color::BRIGHTRED);
3535
for (auto inst : listInstructions)
3636
m_words_colors.emplace_back(inst, Replxx::Color::GREEN);
37-
for (auto op : operators)
37+
for (auto op : Language::operators)
3838
{
3939
auto safe_op = std::string(op);
4040
if (const auto it = safe_op.find_first_of(R"(-+=/*<>[]()?")"); it != std::string::npos)

tests/arkscript/macro-tests.ark

+9-9
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
($ suffix-dup (sym x) {
44
($if (> x 1)
55
(suffix-dup sym (- x 1)))
6-
(symcat sym x)})
6+
($symcat sym x)})
77
(let magic_func (fun ((suffix-dup a 3)) (- a1 a2 a3)))
88

99
($ partial (func ...defargs) {
10-
($ bloc (suffix-dup a (- (argcount func) (len defargs))))
10+
($ bloc (suffix-dup a (- ($argcount func) (len defargs))))
1111
(fun (bloc) (func ...defargs bloc))
1212
($undef bloc)})
1313

@@ -113,7 +113,7 @@
113113

114114
(test:case "define variable with a macro adding a suffix" {
115115
($ nice_value 12)
116-
($ define (prefix suffix value) (let (symcat prefix suffix) value))
116+
($ define (prefix suffix value) (let ($symcat prefix suffix) value))
117117

118118
(define a 1 2)
119119
(test:eq a1 2)
@@ -126,11 +126,11 @@
126126

127127
(test:case "partial functions" {
128128
(test:eq (magic_func 1 2 3) (- 1 2 3))
129-
(test:eq (argcount test_func) 3)
130-
(test:eq (argcount test_func1) 2)
131-
(test:eq (argcount test_func1_2) 1)
132-
(test:eq (argcount (fun () ())) 0)
133-
(test:eq (argcount (fun (a) ())) 1)
134-
(test:eq (argcount (fun (a b g h u t) ())) 6)
129+
(test:eq ($argcount test_func) 3)
130+
(test:eq ($argcount test_func1) 2)
131+
(test:eq ($argcount test_func1_2) 1)
132+
(test:eq ($argcount (fun () ())) 0)
133+
(test:eq ($argcount (fun (a) ())) 1)
134+
(test:eq ($argcount (fun (a b g h u t) ())) 6)
135135
(test:eq (test_func 1 2 3) (test_func1 2 3))
136136
(test:eq (test_func 1 2 3) (test_func1_2 3)) })})

tests/errors/compiler/invalid_let.ark

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
($ partial (func ...defargs) {
2-
($ bloc (suffix-dup a (- (argcount func) (len defargs))))
2+
($ bloc (suffix-dup a (- ($argcount func) (len defargs))))
33
})
44

55
(let test_func (fun (a b c) (* a b c)))

tests/errors/macros/argcount_unknown_arg.ark

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
($ suffix-dup (sym x) {
22
($if (> x 1)
33
(suffix-dup sym (- x 1)))
4-
(symcat sym x)})
4+
($symcat sym x)})
55

66
($ partial (func ...defargs) {
7-
($ bloc (suffix-dup a (- (argcount func) (len defargs))))
7+
($ bloc (suffix-dup a (- ($argcount func) (len defargs))))
88
(fun (bloc) (func ...defargs bloc))
99
($undef bloc)})
1010

tests/errors/macros/argcount_unknown_arg.expected

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ At test_func1!2 @ 13:41
66
13 | (let test_func1_2 (partial test_func1!2))
77
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
88
14 |
9-
When expanding `argcount', expected a known function name, got unbound variable test_func1!2
9+
When expanding `$argcount', expected a known function name, got unbound variable test_func1!2

tests/errors/macros/max_unification_depth.ark

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
($if (+ x 1)
33
est_func
44
($ p (a b c) (* a b c)))
5-
(symcat sym x)})
5+
($symcat sym x)})
66

77
($ partial (func ...defargs) {
88
($ bloc (suffix-dup a (len defargs)))

0 commit comments

Comments
 (0)