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

ArkScript-lang / Ark / 11445793250

21 Oct 2024 05:51PM UTC coverage: 76.877% (+0.4%) from 76.499%
11445793250

push

github

SuperFola
refactor(macro): removing useless constructor

5130 of 6673 relevant lines covered (76.88%)

9379.81 hits per line

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

91.18
/src/arkscript/JsonCompiler.cpp
1
#include <CLI/JsonCompiler.hpp>
2

3
#include <string>
4
#include <vector>
5
#include <ranges>
6
#include <Ark/Exceptions.hpp>
7

8
#include <fmt/core.h>
9

10
using namespace Ark::internal;
11

12
JsonCompiler::JsonCompiler(const unsigned debug, const std::vector<std::filesystem::path>& lib_env) :
9✔
13
    m_welder(debug, lib_env, 0)
9✔
14
{}
9✔
15

16
void JsonCompiler::feed(const std::string& filename)
9✔
17
{
9✔
18
    m_welder.computeASTFromFile(filename);
9✔
19
}
9✔
20

21
std::string JsonCompiler::compile()
9✔
22
{
9✔
23
    return _compile(m_welder.ast());
9✔
24
}
25

26
std::string JsonCompiler::_compile(const Node& node)
653✔
27
{
653✔
28
    std::string json;
653✔
29

30
    switch (node.nodeType())
653✔
31
    {
287✔
32
        case NodeType::Symbol:
×
33
        {
34
            json += fmt::format(
287✔
35
                R"({{"type": "Symbol", "name": "{}"}})",
287✔
36
                node.string());
287✔
37
            break;
287✔
38
        }
6✔
39

40
        case NodeType::Spread:
×
41
        {
42
            json += fmt::format(
6✔
43
                R"({{"type": "Spread", "name": "{}"}})",
6✔
44
                node.string());
6✔
45
            break;
6✔
46
        }
5✔
47

48
        case NodeType::Capture:
49
        {
50
            json += fmt::format(
5✔
51
                R"({{"type": "Capture", "name": "{}"}})",
5✔
52
                node.string());
5✔
53
            break;
5✔
54
        }
4✔
55

56
        case NodeType::Field:
57
        {
58
            json += R"({"type": "Field", "children": )";
4✔
59
            json += toJsonList(node, 0) + "}";
4✔
60
            break;
4✔
61
        }
50✔
62

×
63
        case NodeType::String:
64
        {
65
            json += fmt::format(
50✔
66
                R"({{"type": "String", "value": "{}"}})",
50✔
67
                node.string());
50✔
68
            break;
50✔
69
        }
75✔
70

×
71
        case NodeType::Number:
72
        {
×
73
            json += fmt::format(
75✔
74
                R"({{"type": "Number", "value": {}}})",
75✔
75
                node.number());
75✔
76
            break;
75✔
77
        }
208✔
78

79
        case NodeType::List:
80
        {
×
81
            if (!node.constList().empty() && node.constList()[0].nodeType() == NodeType::Keyword)
208✔
82
            {
83
                Node keyword = node.constList()[0];
86✔
84
                switch (keyword.keyword())
86✔
85
                {
18✔
86
                    case Keyword::Fun:
87
                    {
88
                        // (fun (args) (body))
89
                        std::string args;
18✔
90
                        Node args_node = node.constList()[1];
18✔
91
                        if (args_node.nodeType() == NodeType::List)
18✔
92
                        {
93
                            args = "[";
17✔
94
                            for (std::size_t i = 0, end = args_node.constList().size(); i < end; ++i)
41✔
95
                            {
96
                                args += _compile(args_node.constList()[i]);
24✔
97
                                if (end > 1 && i != end - 1)
24✔
98
                                    args += ", ";
8✔
99
                            }
24✔
100
                            args += "]";
17✔
101
                        }
17✔
102
                        else
103
                            args = _compile(args_node);
1✔
104

105
                        json += fmt::format(
36✔
106
                            R"({{"type": "Fun", "args": {}, "body": {}}})",
18✔
107
                            args, _compile(node.constList()[2]));
18✔
108
                        break;
109
                    }
38✔
110

111
                    case Keyword::Let:
112
                    {
113
                        // (let name value)
114
                        json += fmt::format(
40✔
115
                            R"({{"type": "Let", "name": {}, "value": {}}})",
20✔
116
                            _compile(node.constList()[1]), _compile(node.constList()[2]));
20✔
117
                        break;
20✔
118
                    }
3✔
119

120
                    case Keyword::Mut:
121
                    {
122
                        // (mut name value)
123
                        json += fmt::format(
6✔
124
                            R"({{"type": "Mut", "name": {}, "value": {}}})",
3✔
125
                            _compile(node.constList()[1]), _compile(node.constList()[2]));
3✔
126
                        break;
3✔
127
                    }
5✔
128

×
129
                    case Keyword::Set:
130
                    {
131
                        // (set name value)
132
                        json += fmt::format(
10✔
133
                            R"({{"type": "Set", "name": {}, "value": {}}})",
5✔
134
                            _compile(node.constList()[1]), _compile(node.constList()[2]));
5✔
135
                        break;
5✔
136
                    }
5✔
137

138
                    case Keyword::If:
139
                    {
140
                        // (if condition then else)
141
                        json += fmt::format(
10✔
142
                            R"({{"type": "If", "condition": {}, "then": {}, "else": {}}})",
5✔
143
                            _compile(node.constList()[1]), _compile(node.constList()[2]), _compile(node.constList()[3]));
5✔
144
                        break;
5✔
145
                    }
2✔
146

147
                    case Keyword::While:
148
                    {
149
                        // (while condition body)
150
                        json += fmt::format(
4✔
151
                            R"({{"type": "While", "condition": {}, "body": {}}})",
2✔
152
                            _compile(node.constList()[1]), _compile(node.constList()[2]));
2✔
153
                        break;
2✔
154
                    }
28✔
155

156
                    case Keyword::Begin:
157
                    {
×
158
                        // (begin body)
159
                        json += R"({"type": "Begin", "children": )";
29✔
160
                        json += toJsonList(node, 1) + "}";
28✔
161
                        break;
28✔
162
                    }
4✔
163

164
                    case Keyword::Import:
165
                    {
166
                        // (import pkg.value)
167
                        // (import pkg.value :sym)
168
                        // (import pkg.value:*)
169
                        std::string package = node.constList()[1].constList().front().string();
4✔
170
                        for (const auto& sym : node.constList()[1].constList() | std::views::drop(1))
8✔
171
                            package += "." + sym.string();
4✔
172

173
                        bool is_glob = node.constList()[2].nodeType() == NodeType::Symbol && node.constList()[2].string() == "*";
4✔
174
                        std::vector<std::string> syms;
4✔
175
                        if (node.constList()[2].nodeType() == NodeType::List)
4✔
176
                        {
177
                            for (const auto& sym : node.constList()[2].constList())
5✔
178
                                syms.push_back('"' + sym.string() + '"');
2✔
179
                        }
3✔
180
                        json += fmt::format(
8✔
181
                            R"({{"type": "Import", "package": "{}", "glob": {}, "symbols": [{}]}})",
4✔
182
                            package,
183
                            is_glob,
184
                            fmt::join(syms, ", "));
4✔
185
                        break;
186
                    }
5✔
187

188
                    case Keyword::Del:
189
                    {
190
                        // (del value)
191
                        json += fmt::format(
2✔
192
                            R"({{"type": "Del", "value": {}}})",
1✔
193
                            _compile(node.constList()[1]));
1✔
194
                        break;
1✔
195
                    }
196
                }
86✔
197
            }
86✔
198
            else if (node.constList().size() > 1 && node.constList()[0].nodeType() == NodeType::Symbol)
122✔
199
            {
200
                // (foo bar 1)
201
                json += fmt::format(
234✔
202
                    R"({{"type": "FunctionCall", "name": {}, "args": {}}})",
117✔
203
                    _compile(node.constList()[0]),
117✔
204
                    toJsonList(node, 1));
117✔
205
            }
117✔
206
            else
207
                json += toJsonList(node, 0);
5✔
208

209
            break;
208✔
210
        }
18✔
211

212
        case NodeType::Macro:
213
        {
214
            if (const auto& first = node.constList()[0]; first.nodeType() == NodeType::Symbol)
36✔
215
            {
216
                json += fmt::format(
28✔
217
                    R"({{"type": "Macro", "name": {}, )",
14✔
218
                    _compile(node.constList()[0]));
14✔
219
                if (node.constList().size() == 2)
14✔
220
                    json += fmt::format(R"("value": {}}})", _compile(node.constList()[1]));
7✔
221
                else
222
                    json += fmt::format(
14✔
223
                        R"("args": {}, "body": {}}})",
7✔
224
                        toJsonList(node.constList()[1], 0),
7✔
225
                        _compile(node.constList()[2]));
7✔
226
            }
14✔
227
            else if (first.nodeType() == NodeType::Keyword)
4✔
228
            {
229
                if (first.keyword() == Keyword::If)
4✔
230
                    json += fmt::format(
8✔
231
                        R"({{"type": "MacroCondition", "condition": {}, "then": {}, "else": {}}})",
4✔
232
                        _compile(node.constList()[1]),
4✔
233
                        _compile(node.constList()[2]),
4✔
234
                        node.constList().size() == 4 ? _compile(node.constList()[3]) : R"({"type": "Symbol", "name": "nil"})");
4✔
235
            }
4✔
236
            break;
18✔
237
        }
×
238

239
        default:
240
            throw Ark::Error(fmt::format(
×
241
                "Not handled NodeType::{} ({} at {}:{}), please report this error on GitHub",
×
242
                nodeTypes[static_cast<std::size_t>(node.nodeType())].data(),
×
243
                node.filename(),
×
244
                node.line(),
×
245
                node.col()));
×
246
    }
653✔
247
    return json;
653✔
248
}
653✔
249

250
std::string JsonCompiler::toJsonList(const Node& node, std::size_t start)
161✔
251
{
161✔
252
    std::string json = "[";
161✔
253
    for (std::size_t i = start, end = node.constList().size(); i < end; ++i)
530✔
254
    {
255
        json += _compile(node.constList()[i]);
369✔
256
        if (i != end - 1)
369✔
257
            json += ", ";
209✔
258
    }
369✔
259
    json += "]";
161✔
260
    return json;
161✔
261
}
161✔
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