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

ArkScript-lang / Ark / 14012476381

22 Mar 2025 09:21PM UTC coverage: 79.378% (+0.5%) from 78.852%
14012476381

Pull #519

github

web-flow
Merge 8dcea74d7 into 4aa4303da
Pull Request #519: Feat/better locals

273 of 307 new or added lines in 14 files covered. (88.93%)

27 existing lines in 2 files now uncovered.

6047 of 7618 relevant lines covered (79.38%)

77956.04 hits per line

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

81.08
/src/arkreactor/Compiler/Welder.cpp
1
#include <Ark/Constants.hpp>
2
#include <Ark/Compiler/Welder.hpp>
3

4
#include <Ark/Compiler/Package/ImportSolver.hpp>
5
#include <Ark/Compiler/AST/Optimizer.hpp>
6
#include <Ark/Compiler/Macros/Processor.hpp>
7
#include <Ark/Compiler/NameResolution/NameResolutionPass.hpp>
8
#include <Ark/Files.hpp>
9
#include <Ark/Exceptions.hpp>
10

11
#include <sstream>
12
#include <fmt/ostream.h>
13

14
namespace Ark
15
{
16
    Welder::Welder(const unsigned debug, const std::vector<std::filesystem::path>& lib_env, const uint16_t features) :
396✔
17
        m_lib_env(lib_env), m_features(features),
198✔
18
        m_computed_ast(internal::NodeType::Unused),
198✔
19
        m_parser(debug),
198✔
20
        m_import_solver(debug, lib_env),
198✔
21
        m_macro_processor(debug),
198✔
22
        m_ast_optimizer(debug),
198✔
23
        m_name_resolver(debug),
198✔
24
        m_logger("Welder", debug),
198✔
25
        m_lowerer(debug),
198✔
26
        m_ir_optimizer(debug),
198✔
27
        m_ir_compiler(debug)
198✔
28
    {}
396✔
29

30
    void Welder::registerSymbol(const std::string& name)
22✔
31
    {
22✔
32
        m_name_resolver.addDefinedSymbol(name, /* is_mutable= */ false);
22✔
33
    }
22✔
34

35
    bool Welder::computeASTFromFile(const std::string& filename)
194✔
36
    {
194✔
37
        m_root_file = std::filesystem::path(filename);
194✔
38
        const std::string code = Utils::readFile(filename);
194✔
39

40
        return computeAST(filename, code);
194✔
41
    }
194✔
42

43
    bool Welder::computeASTFromString(const std::string& code)
4✔
44
    {
4✔
45
        m_root_file = std::filesystem::current_path();  // No filename given, take the current working directory
4✔
46

47
        return computeAST(ARK_NO_NAME_FILE, code);
4✔
48
    }
×
49

50
    bool Welder::generateBytecode()
154✔
51
    {
154✔
52
        try
53
        {
54
            m_lowerer.process(m_computed_ast);
154✔
NEW
55
            m_ir = m_lowerer.intermediateRepresentation();
×
56

57
            if ((m_features & FeatureIROptimizer) != 0)
89✔
58
            {
59
                m_ir_optimizer.process(m_ir, m_lowerer.symbols(), m_lowerer.values());
89✔
60
                m_ir = m_ir_optimizer.intermediateRepresentation();
89✔
61
            }
89✔
62

63
            m_ir_compiler.process(m_ir, m_lowerer.symbols(), m_lowerer.values());
89✔
64
            m_bytecode = m_ir_compiler.bytecode();
94✔
65

66
            if ((m_features & FeatureDumpIR) != 0)
×
67
                dumpIRToFile();
×
68

69
            return true;
×
70
        }
68✔
71
        catch (const CodeError& e)
72
        {
73
            if ((m_features & FeatureTestFailOnException) > 0)
34✔
74
                throw;
34✔
75

76
            Diagnostics::generate(e);
×
77
            return false;
×
78
        }
34✔
79
    }
376✔
80

81
    bool Welder::saveBytecodeToFile(const std::string& filename)
78✔
82
    {
78✔
83
        m_logger.info("Final bytecode size: {}B", m_bytecode.size() * sizeof(uint8_t));
78✔
84

85
        if (m_bytecode.empty())
78✔
86
            return false;
×
87

88
        std::ofstream output(filename, std::ofstream::binary);
78✔
89
        output.write(
156✔
90
            reinterpret_cast<char*>(&m_bytecode[0]),
78✔
91
            static_cast<std::streamsize>(m_bytecode.size() * sizeof(uint8_t)));
78✔
92
        output.close();
78✔
93
        return true;
78✔
94
    }
78✔
95

96
    const internal::Node& Welder::ast() const noexcept
11✔
97
    {
11✔
98
        return m_computed_ast;
11✔
99
    }
100

101
    std::string Welder::textualIR() const noexcept
11✔
102
    {
11✔
103
        std::stringstream stream;
11✔
104
        m_ir_compiler.dumpToStream(stream);
11✔
105
        return stream.str();
11✔
106
    }
11✔
107

108
    const bytecode_t& Welder::bytecode() const noexcept
5✔
109
    {
5✔
110
        return m_bytecode;
5✔
111
    }
112

113
    void Welder::dumpIRToFile() const
×
114
    {
×
115
        std::filesystem::path path = m_root_file;
×
116
        if (is_directory(m_root_file))
×
117
            path /= "output.ark.ir";
×
118
        else
119
            path.replace_extension(".ark.ir");
×
120

121
        std::ofstream output(path);
×
122
        m_ir_compiler.dumpToStream(output);
×
123
        output.close();
×
124
    }
×
125

126
    bool Welder::computeAST(const std::string& filename, const std::string& code)
270✔
127
    {
270✔
128
        try
129
        {
130
            m_parser.process(filename, code);
270✔
131
            m_computed_ast = m_parser.ast();
×
132

133
            if ((m_features & FeatureImportSolver) != 0)
289✔
134
            {
135
                m_import_solver.setup(m_root_file, m_parser.imports());
168✔
136
                m_import_solver.process(m_computed_ast);
168✔
137
                m_computed_ast = m_import_solver.ast();
168✔
138
            }
168✔
139

140
            if ((m_features & FeatureMacroProcessor) != 0)
289✔
141
            {
142
                m_macro_processor.process(m_computed_ast);
168✔
143
                m_computed_ast = m_macro_processor.ast();
147✔
144
            }
147✔
145

146
            if ((m_features & FeatureNameResolver) != 0)
268✔
147
            {
148
                m_name_resolver.process(m_computed_ast);
147✔
149
                m_computed_ast = m_name_resolver.ast();
128✔
150
            }
128✔
151

152
            if ((m_features & FeatureASTOptimizer) != 0)
249✔
153
            {
154
                m_ast_optimizer.process(m_computed_ast);
7✔
155
                m_computed_ast = m_ast_optimizer.ast();
7✔
156
            }
7✔
157

158
            return true;
7✔
159
        }
118✔
160
        catch (const CodeError& e)
161
        {
162
            if ((m_features & FeatureTestFailOnException) > 0)
59✔
163
                throw;
59✔
164

165
            Diagnostics::generate(e);
×
166
            return false;
×
167
        }
59✔
168
    }
665✔
169
}
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