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

ArkScript-lang / Ark / 20065764942

09 Dec 2025 01:49PM UTC coverage: 90.564% (+0.008%) from 90.556%
20065764942

push

github

SuperFola
chore(tests): adding IR generation tests for arg attributes

8043 of 8881 relevant lines covered (90.56%)

180911.91 hits per line

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

88.06
/src/arkreactor/Builtins/IO.cpp
1
#include <Ark/Builtins/Builtins.hpp>
2

3
#include <iostream>
4
#include <filesystem>
5
#include <fmt/core.h>
6

7
#include <Ark/Utils/Files.hpp>
8
#include <Ark/Utils/Utils.hpp>
9
#include <Ark/VM/VM.hpp>
10
#include <Ark/Error/Exceptions.hpp>
11
#include <Ark/TypeChecker.hpp>
12

13
namespace Ark::internal::Builtins::IO
14
{
15
    /**
16
     * @name print
17
     * @brief Print value(s) in the terminal
18
     * @details No separator is put between the values. Adds a \n at the end
19
     * @param values the values to print
20
     * =begin
21
     * (print "hello")
22
     * =end
23
     * @author https://github.com/SuperFola
24
     */
25
    // cppcheck-suppress constParameterReference
26
    Value print(std::vector<Value>& n, VM* vm)
77✔
27
    {
77✔
28
        for (const auto& value : n)
206✔
29
            fmt::print("{}", value.toString(*vm));
129✔
30
        fmt::println("");
77✔
31

32
        return nil;
77✔
33
    }
×
34

35
    /**
36
     * @name puts
37
     * @brief Print value(s) in the terminal
38
     * @details No separator is put between the values, no \n at the end
39
     * @param values the values to print
40
     * =begin
41
     * (puts "hello")
42
     * =end
43
     * @author https://github.com/SuperFola
44
     */
45
    // cppcheck-suppress constParameterReference
46
    Value puts_(std::vector<Value>& n, VM* vm)
302✔
47
    {
302✔
48
        for (const auto& value : n)
663✔
49
            fmt::print("{}", value.toString(*vm));
361✔
50

51
        return nil;
302✔
52
    }
×
53

54
    /**
55
     * @name input
56
     * @brief Request a value from the user
57
     * @details Return the value as a string
58
     * @param prompt (optional) printed before asking for the user input
59
     * =begin
60
     * (input "put a number> ")
61
     * =end
62
     * @author https://github.com/SuperFola
63
     */
64
    // cppcheck-suppress constParameterReference
65
    Value input(std::vector<Value>& n, VM* vm [[maybe_unused]])
×
66
    {
×
67
        if (types::check(n, ValueType::String))
×
68
            fmt::print("{}", n[0].string());
×
69
        else if (!n.empty())
×
70
            throw types::TypeCheckingError("input", { { types::Contract {}, types::Contract { { types::Typedef("prompt", ValueType::String) } } } }, n);
×
71

72
        std::string line;
×
73
#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
74
        std::getline(std::cin, line);
×
75
#else
76
        line = "fuzzer input";
77
#endif
78

79
        return Value(line);
×
80
    }
×
81

82
    // cppcheck-suppress constParameterReference
83
    Value writeFile(std::vector<Value>& n, VM* vm [[maybe_unused]])
3✔
84
    {
3✔
85
        if (types::check(n, ValueType::String, ValueType::Any))
3✔
86
        {
87
#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
88
            std::ofstream f(n[0].string());
2✔
89
            if (f.is_open())
2✔
90
            {
91
                f << n[1].toString(*vm);
2✔
92
                f.close();
2✔
93
            }
2✔
94
            else
95
                throw std::runtime_error(fmt::format("io:writeFile: couldn't write to file \"{}\"", n[0].stringRef()));
×
96
#endif
97
        }
3✔
98
        else
99
            throw types::TypeCheckingError(
2✔
100
                "io:writeFile",
1✔
101
                { { types::Contract { { types::Typedef("filename", ValueType::String), types::Typedef("content", ValueType::Any) } } } },
1✔
102
                n);
1✔
103

104
        return nil;
2✔
105
    }
1✔
106

107
    // cppcheck-suppress constParameterReference
108
    Value appendToFile(std::vector<Value>& n, VM* vm [[maybe_unused]])
3✔
109
    {
3✔
110
        if (types::check(n, ValueType::String, ValueType::Any))
3✔
111
        {
112
#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
113
            std::ofstream f(n[0].string(), std::ios::out | std::ios::app);
2✔
114
            if (f.is_open())
2✔
115
            {
116
                f << n[1].toString(*vm);
2✔
117
                f.close();
2✔
118
            }
2✔
119
            else
120
                throw std::runtime_error(fmt::format("io:appendToFile: couldn't write to file \"{}\"", n[0].stringRef()));
×
121
#endif
122
        }
3✔
123
        else
124
            throw types::TypeCheckingError(
2✔
125
                "io:appendToFile",
1✔
126
                { { types::Contract { { types::Typedef("filename", ValueType::String), types::Typedef("content", ValueType::Any) } } } },
1✔
127
                n);
1✔
128

129
        return nil;
2✔
130
    }
1✔
131

132
    // cppcheck-suppress constParameterReference
133
    Value readFile(std::vector<Value>& n, VM* vm [[maybe_unused]])
7✔
134
    {
7✔
135
        if (!types::check(n, ValueType::String))
7✔
136
            throw types::TypeCheckingError(
3✔
137
                "io:readFile",
1✔
138
                { { types::Contract { { types::Typedef("filename", ValueType::String) } } } },
1✔
139
                n);
1✔
140

141
        std::string filename = n[0].string();
6✔
142
        if (!Utils::fileExists(filename))
6✔
143
            throw std::runtime_error(
1✔
144
                fmt::format("io:readFile: couldn't read file \"{}\" because it doesn't exist", filename));
1✔
145

146
        return Value(Utils::readFile(filename));
5✔
147
    }
8✔
148

149
    // cppcheck-suppress constParameterReference
150
    Value readLinesFile(std::vector<Value>& n, VM* vm [[maybe_unused]])
2✔
151
    {
2✔
152
        if (!types::check(n, ValueType::String))
2✔
153
            throw types::TypeCheckingError(
2✔
154
                "io:readLinesFile",
1✔
155
                { { types::Contract { { types::Typedef("filename", ValueType::String) } } } },
1✔
156
                n);
1✔
157

158
        std::string filename = n[0].string();
1✔
159
        if (!Utils::fileExists(filename))
1✔
160
            throw std::runtime_error(
×
161
                fmt::format("io:readLinesFile: couldn't read file \"{}\" because it doesn't exist", filename));
×
162

163
        Value out = Value(ValueType::List);
1✔
164
        for (auto&& s : Utils::splitString(Utils::readFile(filename), '\n'))
2✔
165
            out.push_back(Value(s));
1✔
166
        return out;
1✔
167
    }
2✔
168

169
    // cppcheck-suppress constParameterReference
170
    Value fileExists(std::vector<Value>& n, VM* vm [[maybe_unused]])
9✔
171
    {
9✔
172
        if (!types::check(n, ValueType::String))
9✔
173
            throw types::TypeCheckingError(
2✔
174
                "io:fileExists?",
1✔
175
                { { types::Contract { { types::Typedef("filename", ValueType::String) } } } },
1✔
176
                n);
1✔
177

178
        return Utils::fileExists(n[0].string()) ? trueSym : falseSym;
8✔
179
    }
1✔
180

181
    // cppcheck-suppress constParameterReference
182
    Value listFiles(std::vector<Value>& n, VM* vm [[maybe_unused]])
2✔
183
    {
2✔
184
        if (!types::check(n, ValueType::String))
2✔
185
            throw types::TypeCheckingError(
2✔
186
                "io:listFiles",
1✔
187
                { { types::Contract { { types::Typedef("path", ValueType::String) } } } },
1✔
188
                n);
1✔
189

190
        std::vector<Value> r;
1✔
191
        for (const auto& entry : std::filesystem::directory_iterator(n[0].string()))
35✔
192
            // cppcheck-suppress useStlAlgorithm
193
            // We can't use std::transform with a directory_iterator
194
            r.emplace_back(entry.path().string());
34✔
195

196
        return Value(std::move(r));
1✔
197
    }
2✔
198

199
    // cppcheck-suppress constParameterReference
200
    Value isDirectory(std::vector<Value>& n, VM* vm [[maybe_unused]])
5✔
201
    {
5✔
202
        if (!types::check(n, ValueType::String))
5✔
203
            throw types::TypeCheckingError(
2✔
204
                "io:dir?",
1✔
205
                { { types::Contract { { types::Typedef("path", ValueType::String) } } } },
1✔
206
                n);
1✔
207

208
        return (std::filesystem::is_directory(std::filesystem::path(n[0].string()))) ? trueSym : falseSym;
4✔
209
    }
1✔
210

211
    // cppcheck-suppress constParameterReference
212
    Value makeDir(std::vector<Value>& n, VM* vm [[maybe_unused]])
3✔
213
    {
3✔
214
        if (!types::check(n, ValueType::String))
3✔
215
            throw types::TypeCheckingError(
2✔
216
                "io:makeDir",
1✔
217
                { { types::Contract { { types::Typedef("path", ValueType::String) } } } },
1✔
218
                n);
1✔
219

220
        std::filesystem::create_directories(std::filesystem::path(n[0].string()));
2✔
221
        return nil;
2✔
222
    }
1✔
223

224
    // cppcheck-suppress constParameterReference
225
    Value removeFile(std::vector<Value>& n, VM* vm [[maybe_unused]])
5✔
226
    {
5✔
227
        if (!types::check(n, ValueType::String))
5✔
228
            throw types::TypeCheckingError(
2✔
229
                "io:removeFile",
1✔
230
                { { types::Contract { { types::Typedef("filename", ValueType::String) } } } },
1✔
231
                n);
1✔
232

233
#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
234
        std::filesystem::remove_all(std::filesystem::path(n[0].string()));
4✔
235
#endif
236
        return nil;
4✔
237
    }
1✔
238
}
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

© 2026 Coveralls, Inc