• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ArkScript-lang / Ark / 12320388836

13 Dec 2024 05:39PM UTC coverage: 79.237% (+1.9%) from 77.378%
12320388836

push

github

SuperFola
feat(ast optimizer): adding AST optimization tests and fixing the json compiler to accept (if cond then) and not just (if cond then else)

59 of 68 new or added lines in 3 files covered. (86.76%)

14 existing lines in 5 files now uncovered.

5667 of 7152 relevant lines covered (79.24%)

14746.86 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.99
/src/arkreactor/Compiler/NameResolution/NameResolutionPass.cpp
1
#include <Ark/Compiler/NameResolution/NameResolutionPass.hpp>
2

3
#include <Ark/Exceptions.hpp>
4
#include <Ark/Utils.hpp>
5
#include <Ark/Builtins/Builtins.hpp>
6

7
namespace Ark::internal
8
{
9
    NameResolutionPass::NameResolutionPass(const unsigned debug) :
300✔
10
        Pass("NameResolution", debug),
150✔
11
        m_ast()
150✔
12
    {
300✔
13
        for (const auto& builtin : Builtins::builtins)
8,550✔
14
            m_language_symbols.emplace(builtin.first);
8,400✔
15
        for (auto ope : Language::operators)
3,600✔
16
            m_language_symbols.emplace(ope);
3,450✔
17
        for (auto inst : Language::listInstructions)
1,500✔
18
            m_language_symbols.emplace(inst);
1,350✔
19

20
        m_language_symbols.emplace(Language::And);
150✔
21
        m_language_symbols.emplace(Language::Or);
150✔
22
        m_language_symbols.emplace(Language::SysArgs);
150✔
23
    }
150✔
24

25
    void NameResolutionPass::process(const Node& ast)
82✔
26
    {
82✔
27
        m_logger.traceStart("process");
82✔
28

29
        m_ast = ast;
82✔
30
        visit(m_ast, /* register_declarations= */ true);
82✔
31

32
        m_logger.traceEnd();
82✔
33

34
        m_logger.trace("AST after name resolution");
82✔
35
        if (m_logger.shouldTrace())
82✔
36
            m_ast.debugPrint(std::cout) << '\n';
×
37

38
        m_logger.traceStart("checkForUndefinedSymbol");
82✔
39
        checkForUndefinedSymbol();
82✔
40
        m_logger.traceEnd();
82✔
41
    }
82✔
42

43
    const Node& NameResolutionPass::ast() const noexcept
80✔
44
    {
80✔
45
        return m_ast;
80✔
46
    }
47

48
    std::string NameResolutionPass::addDefinedSymbol(const std::string& sym, const bool is_mutable)
1,770✔
49
    {
1,770✔
50
        const std::string fully_qualified_name = m_scope_resolver.registerInCurrent(sym, is_mutable);
1,770✔
51
        m_defined_symbols.emplace(fully_qualified_name);
1,770✔
52
        return fully_qualified_name;
1,770✔
53
    }
1,770✔
54

55
    void NameResolutionPass::visit(Node& node, const bool register_declarations)
89,033✔
56
    {
89,033✔
57
        switch (node.nodeType())
89,033✔
58
        {
7,855✔
59
            case NodeType::Symbol:
60
            {
61
                const std::string old_name = node.string();
7,855✔
62
                updateSymbolWithFullyQualifiedName(node);
7,855✔
63
                addSymbolNode(node, old_name);
7,852✔
64
                break;
65
            }
8,575✔
66

67
            case NodeType::Field:
68
                for (auto& child : node.list())
2,152✔
69
                {
70
                    const std::string old_name = child.string();
1,439✔
71
                    // in case of field, no need to check if we can fully qualify names
72
                    child.setString(m_scope_resolver.getFullyQualifiedNameInNearestScope(old_name));
1,439✔
73
                    addSymbolNode(child, old_name);
1,439✔
74
                }
1,439✔
75
                break;
9,413✔
76

77
            case NodeType::List:
78
                if (!node.constList().empty())
8,700✔
79
                {
80
                    if (node.constList()[0].nodeType() == NodeType::Keyword)
8,676✔
81
                        visitKeyword(node, node.constList()[0].keyword(), register_declarations);
3,122✔
82
                    else
83
                    {
84
                        // function calls
85
                        // the UpdateRef function calls kind get a special treatment, like let/mut/set,
86
                        // because we need to check for mutability errors
87
                        if (node.constList().size() > 1 && node.constList()[0].nodeType() == NodeType::Symbol &&
7,066✔
88
                            node.constList()[1].nodeType() == NodeType::Symbol && register_declarations)
3,951✔
89
                        {
90
                            const auto funcname = node.constList()[0].string();
790✔
91
                            const auto arg = node.constList()[1].string();
790✔
92

93
                            if (std::ranges::find(Language::UpdateRef, funcname) != Language::UpdateRef.end() && m_scope_resolver.isImmutable(arg).value_or(false))
790✔
94
                                throw CodeError(
10✔
95
                                    fmt::format("MutabilityError: Can not modify the constant list `{}' using `{}'", arg, funcname),
5✔
96
                                    node.filename(),
5✔
97
                                    node.constList()[1].line(),
5✔
98
                                    node.constList()[1].col(),
5✔
99
                                    arg);
5✔
100

101
                            // check that we aren't doing a (append! a a) nor a (concat! a a)
102
                            if (funcname == Language::AppendInPlace || funcname == Language::ConcatInPlace)
785✔
103
                            {
104
                                for (std::size_t i = 2, end = node.constList().size(); i < end; ++i)
68✔
105
                                {
106
                                    if (node.constList()[i].nodeType() == NodeType::Symbol && node.constList()[i].string() == arg)
34✔
107
                                        throw CodeError(
4✔
108
                                            fmt::format("MutabilityError: Can not {} the list `{}' to itself", funcname, arg),
2✔
109
                                            node.filename(),
2✔
110
                                            node.constList()[1].line(),
2✔
111
                                            node.constList()[1].col(),
2✔
112
                                            arg);
2✔
113
                                }
32✔
114
                            }
32✔
115
                        }
790✔
116

117
                        for (auto& child : node.list())
86,274✔
118
                            visit(child, register_declarations);
80,727✔
119
                    }
120
                }
8,669✔
121
                break;
8,724✔
122

123
            case NodeType::Namespace:
124
            {
125
                auto& namespace_ = node.arkNamespace();
31✔
126
                // no need to guard createNewNamespace with an if (register_declarations), we want to keep the namespace node
127
                // (which will get ignored by the compiler, that only uses its AST), so that we can (re)construct the
128
                // scopes correctly
129
                m_scope_resolver.createNewNamespace(namespace_.name, namespace_.with_prefix, namespace_.is_glob, namespace_.symbols);
31✔
130
                StaticScope* scope = m_scope_resolver.currentScope();
31✔
131

132
                visit(*namespace_.ast, /* register_declarations= */ true);
31✔
133
                // dual visit so that we can handle forward references
134
                visit(*namespace_.ast, /* register_declarations= */ false);
31✔
135

136
                // if we had specific symbols to import, check that those exist
137
                if (!namespace_.symbols.empty())
31✔
138
                {
139
                    for (const auto& sym : namespace_.symbols)
26✔
140
                    {
141
                        if (!scope->get(sym, true).has_value())
16✔
142
                            throw CodeError(
×
143
                                fmt::format("ImportError: Can not import symbol {} from {}, as it isn't in the package", sym, namespace_.name),
×
144
                                node.filename(),
×
145
                                namespace_.ast->line(),
×
146
                                namespace_.ast->col(),
×
147
                                sym);
×
148
                    }
16✔
149
                }
10✔
150

151
                m_scope_resolver.saveNamespaceAndRemove();
31✔
152
                break;
153
            }
71,765✔
154

155
            default:
156
                break;
71,734✔
157
        }
88,975✔
158
    }
88,985✔
159

160
    void NameResolutionPass::visitKeyword(Node& node, const Keyword keyword, const bool register_declarations)
3,122✔
161
    {
3,122✔
162
        switch (keyword)
3,122✔
163
        {
1,236✔
164
            case Keyword::Set:
165
                [[fallthrough]];
166
            case Keyword::Let:
167
                [[fallthrough]];
168
            case Keyword::Mut:
169
                // first, visit the value, then register the symbol
170
                // this allows us to detect things like (let foo (fun (&foo) ()))
171
                if (node.constList().size() > 2)
1,236✔
172
                    visit(node.list()[2], register_declarations);
1,235✔
173
                if (node.constList().size() > 1 && node.constList()[1].nodeType() == NodeType::Symbol)
1,236✔
174
                {
175
                    const std::string& name = node.constList()[1].string();
1,234✔
176
                    if (m_language_symbols.contains(name) && register_declarations)
1,234✔
177
                        throw CodeError(
4✔
178
                            fmt::format("Can not use a reserved identifier ('{}') as a {} name.", name, keyword == Keyword::Let ? "constant" : "variable"),
2✔
179
                            node.filename(),
2✔
180
                            node.constList()[1].line(),
2✔
181
                            node.constList()[1].col(),
2✔
182
                            name);
2✔
183

184
                    if (m_scope_resolver.isInScope(name) && keyword == Keyword::Let && register_declarations)
1,232✔
185
                        throw CodeError(
2✔
186
                            fmt::format("MutabilityError: Can not use 'let' to redefine variable `{}'", name),
1✔
187
                            node.filename(),
1✔
188
                            node.constList()[1].line(),
1✔
189
                            node.constList()[1].col(),
1✔
190
                            name);
1✔
191
                    if (keyword == Keyword::Set && m_scope_resolver.isRegistered(name))
1,231✔
192
                    {
193
                        if (m_scope_resolver.isImmutable(name).value_or(false) && register_declarations)
206✔
194
                            throw CodeError(
2✔
195
                                fmt::format("MutabilityError: Can not set the constant `{}' to {}", name, node.constList()[2].repr()),
1✔
196
                                node.filename(),
1✔
197
                                node.constList()[1].line(),
1✔
198
                                node.constList()[1].col(),
1✔
199
                                name);
1✔
200

201
                        updateSymbolWithFullyQualifiedName(node.list()[1]);
205✔
202
                    }
205✔
203
                    else if (keyword != Keyword::Set)
1,025✔
204
                    {
205
                        // update the declared variable name to use the fully qualified name
206
                        // this will prevent name conflicts, and handle scope resolution
207
                        const std::string fully_qualified_name = addDefinedSymbol(name, keyword != Keyword::Let);
1,022✔
208
                        if (register_declarations)
1,022✔
209
                            node.list()[1].setString(fully_qualified_name);
561✔
210
                    }
1,022✔
211
                }
1,236✔
212
                break;
1,232✔
213

214
            case Keyword::Import:
215
                if (!node.constList().empty())
×
216
                    m_plugin_names.push_back(node.constList()[1].constList().back().string());
×
217
                break;
455✔
218

219
            case Keyword::Fun:
220
                // create a new scope to track variables
221
                m_scope_resolver.createNew();
455✔
222

223
                if (node.constList()[1].nodeType() == NodeType::List)
455✔
224
                {
225
                    for (auto& child : node.list()[1].list())
1,200✔
226
                    {
227
                        if (child.nodeType() == NodeType::Capture)
746✔
228
                        {
229
                            if (!m_scope_resolver.isRegistered(child.string()) && register_declarations)
145✔
230
                                throw CodeError(
4✔
231
                                    fmt::format("Can not capture {} because it is referencing a variable defined in an unreachable scope.", child.string()),
2✔
232
                                    child.filename(),
2✔
233
                                    child.line(),
2✔
234
                                    child.col(),
2✔
235
                                    child.repr());
2✔
236

237
                            // update the declared variable name to use the fully qualified name
238
                            // this will prevent name conflicts, and handle scope resolution
239
                            std::string fqn = updateSymbolWithFullyQualifiedName(child);
143✔
240
                            addDefinedSymbol(fqn, true);
143✔
241
                        }
143✔
242
                        else if (child.nodeType() == NodeType::Symbol)
601✔
243
                            addDefinedSymbol(child.string(), /* is_mutable= */ true);
601✔
244
                    }
746✔
245
                }
452✔
246
                if (node.constList().size() > 2)
453✔
247
                    visit(node.list()[2], register_declarations);
452✔
248

249
                // remove the scope once the function has been compiled, only we were registering declarations
250
                m_scope_resolver.removeLastScope();
453✔
251
                break;
1,884✔
252

253
            default:
254
                for (auto& child : node.list())
7,871✔
255
                    visit(child, register_declarations);
6,440✔
256
                break;
1,431✔
257
        }
3,116✔
258
    }
3,122✔
259

260
    void NameResolutionPass::addSymbolNode(const Node& symbol, const std::string& old_name)
9,291✔
261
    {
9,291✔
262
        const std::string& name = symbol.string();
9,291✔
263

264
        // we don't accept builtins/operators as a user symbol
265
        if (m_language_symbols.contains(name))
9,291✔
266
            return;
4,400✔
267

268
        // remove the old name node, to avoid false positive when looking for unbound symbols
269
        if (!old_name.empty())
4,891✔
270
        {
271
            auto it = std::ranges::find_if(m_symbol_nodes, [&old_name, &symbol](const Node& sym_node) -> bool {
409,328✔
272
                return sym_node.string() == old_name &&
408,927✔
273
                    sym_node.col() == symbol.col() &&
4,490✔
274
                    sym_node.line() == symbol.line() &&
5,545✔
275
                    sym_node.filename() == symbol.filename();
2,545✔
276
            });
277
            if (it != m_symbol_nodes.end())
4,891✔
278
            {
279
                it->setString(name);
2,545✔
280
                return;
2,545✔
281
            }
282
        }
4,891✔
283

284
        const auto it = std::ranges::find_if(m_symbol_nodes, [&name](const Node& sym_node) -> bool {
187,239✔
285
            return sym_node.string() == name;
184,893✔
286
        });
287
        if (it == m_symbol_nodes.end())
2,346✔
288
            m_symbol_nodes.push_back(symbol);
297✔
289
    }
9,291✔
290

291
    bool NameResolutionPass::mayBeFromPlugin(const std::string& name) const noexcept
293✔
292
    {
293✔
293
        std::string splitted = Utils::splitString(name, ':')[0];
293✔
294
        const auto it = std::ranges::find_if(
293✔
295
            m_plugin_names,
293✔
296
            [&splitted](const std::string& plugin) -> bool {
293✔
297
                return plugin == splitted;
×
298
            });
299
        return it != m_plugin_names.end();
293✔
300
    }
293✔
301

302
    std::string NameResolutionPass::updateSymbolWithFullyQualifiedName(Node& symbol)
8,203✔
303
    {
8,203✔
304
        auto [allowed, fqn] = m_scope_resolver.canFullyQualifyName(symbol.string());
37,214✔
305

306
        if (m_language_symbols.contains(fqn) && symbol.string() != fqn)
8,203✔
307
        {
308
            throw CodeError(
2✔
309
                fmt::format(
1✔
310
                    "Symbol `{}' was resolved to `{}', which is also a builtin name. Either the symbol or the package it's in needs to be renamed to avoid conflicting with the builtin.",
1✔
311
                    symbol.string(), fqn),
1✔
312
                symbol.filename(),
1✔
313
                symbol.line(),
1✔
314
                symbol.col(),
1✔
315
                symbol.repr());
1✔
316
        }
317
        if (!allowed)
8,202✔
318
        {
319
            std::string message;
2✔
320
            if (fqn.ends_with("#hidden"))
2✔
321
                message = fmt::format(
2✔
322
                    R"(Unbound variable "{}". However, it exists in a namespace as "{}", did you forget to add it to the symbol list while importing?)",
1✔
323
                    symbol.string(),
1✔
324
                    fqn.substr(0, fqn.find_first_of('#')));
2✔
325
            else
326
                message = fmt::format(R"(Unbound variable "{}". However, it exists in a namespace as "{}", did you forget to prefix it with its namespace?)", symbol.string(), fqn);
1✔
327
            throw CodeError(
4✔
328
                message,
329
                symbol.filename(),
2✔
330
                symbol.line(),
2✔
331
                symbol.col(),
2✔
332
                symbol.repr());
2✔
333
        }
2✔
334

335
        symbol.setString(fqn);
8,200✔
336
        return fqn;
8,200✔
337
    }
8,206✔
338

339
    void NameResolutionPass::checkForUndefinedSymbol() const
82✔
340
    {
82✔
341
        for (const auto& sym : m_symbol_nodes)
375✔
342
        {
343
            const auto& str = sym.string();
293✔
344
            const bool is_plugin = mayBeFromPlugin(str);
293✔
345

346
            if (!m_defined_symbols.contains(str) && !is_plugin)
293✔
347
            {
348
                std::string message;
2✔
349

350
                const std::string suggestion = offerSuggestion(str);
2✔
351
                if (suggestion.empty())
2✔
352
                    message = fmt::format(R"(Unbound variable error "{}" (variable is used but not defined))", str);
1✔
353
                else
354
                {
355
                    const std::string prefix = suggestion.substr(0, suggestion.find_first_of(':'));
1✔
356
                    const std::string note_about_prefix = fmt::format(
2✔
357
                        " You either forgot to import it in the symbol list (eg `(import {} :{})') or need to fully qualify it by adding the namespace",
1✔
358
                        prefix,
359
                        str);
1✔
360
                    const bool add_note = suggestion.ends_with(":" + str);
1✔
361
                    message = fmt::format(R"(Unbound variable error "{}" (did you mean "{}"?{}))", str, suggestion, add_note ? note_about_prefix : "");
1✔
362
                }
1✔
363

364
                throw CodeError(message, sym.filename(), sym.line(), sym.col(), sym.repr());
2✔
365
            }
2✔
366
        }
293✔
367
    }
82✔
368

369
    std::string NameResolutionPass::offerSuggestion(const std::string& str) const
2✔
370
    {
2✔
371
        auto iterate = [](const std::string& word, const std::unordered_set<std::string>& dict) -> std::string {
5✔
372
            std::string suggestion;
3✔
373
            // our suggestion shouldn't require more than half the string to change
374
            std::size_t suggestion_distance = word.size() / 2;
3✔
375
            for (const std::string& symbol : dict)
97✔
376
            {
377
                const std::size_t current_distance = Utils::levenshteinDistance(word, symbol);
94✔
378
                if (current_distance <= suggestion_distance)
94✔
379
                {
380
                    suggestion_distance = current_distance;
1✔
381
                    suggestion = symbol;
1✔
382
                }
1✔
383
            }
94✔
384
            return suggestion;
3✔
385
        };
3✔
386

387
        std::string suggestion = iterate(str, m_defined_symbols);
2✔
388
        // look for a suggestion related to language builtins
389
        if (suggestion.empty())
2✔
390
            suggestion = iterate(str, m_language_symbols);
1✔
391
        // look for a suggestion related to a namespace change
392
        if (suggestion.empty())
2✔
393
        {
394
            if (const auto it = std::ranges::find_if(m_defined_symbols, [&str](const std::string& symbol) {
3✔
395
                    return symbol.ends_with(":" + str);
1✔
396
                });
397
                it != m_defined_symbols.end())
1✔
UNCOV
398
                suggestion = *it;
×
399
        }
1✔
400

401
        return suggestion;
2✔
402
    }
2✔
403
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc