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

ArkScript-lang / Ark / 13997547789

21 Mar 2025 05:33PM UTC coverage: 78.902% (+0.05%) from 78.852%
13997547789

Pull #519

github

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

266 of 293 new or added lines in 10 files covered. (90.78%)

25 existing lines in 2 files now uncovered.

6021 of 7631 relevant lines covered (78.9%)

77817.42 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) :
394✔
17
        m_lib_env(lib_env), m_features(features),
197✔
18
        m_computed_ast(internal::NodeType::Unused),
197✔
19
        m_parser(debug),
197✔
20
        m_import_solver(debug, lib_env),
197✔
21
        m_macro_processor(debug),
197✔
22
        m_ast_optimizer(debug),
197✔
23
        m_name_resolver(debug),
197✔
24
        m_logger("Welder", debug),
197✔
25
        m_lowerer(debug),
197✔
26
        m_ir_optimizer(debug),
197✔
27
        m_ir_compiler(debug)
197✔
28
    {}
394✔
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)
193✔
36
    {
193✔
37
        m_root_file = std::filesystem::path(filename);
193✔
38
        const std::string code = Utils::readFile(filename);
193✔
39

40
        return computeAST(filename, code);
193✔
41
    }
193✔
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()
152✔
51
    {
152✔
52
        try
53
        {
54
            m_lowerer.process(m_computed_ast);
152✔
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();
93✔
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
    }
372✔
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
10✔
102
    {
10✔
103
        std::stringstream stream;
10✔
104
        m_ir_compiler.dumpToStream(stream);
10✔
105
        return stream.str();
10✔
106
    }
10✔
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)
269✔
127
    {
269✔
128
        try
129
        {
130
            m_parser.process(filename, code);
269✔
131
            m_computed_ast = m_parser.ast();
×
132

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

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

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

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

158
            return true;
6✔
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
    }
662✔
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