ArkScript
A small, lisp-inspired, functional scripting language
Welder.hpp
Go to the documentation of this file.
1/**
2 * @file Welder.hpp
3 * @author Lexy Plateau (lexplt.dev@gmail.com)
4 * @brief In charge of welding everything needed to compile code
5 * @date 2023-03-26
6 *
7 * @copyright Copyright (c) 2023-2026
8 *
9 */
10
11#ifndef ARK_COMPILER_WELDER_HPP
12#define ARK_COMPILER_WELDER_HPP
13
14#include <string>
15#include <vector>
16#include <filesystem>
17
25#include <Ark/Constants.hpp>
26#include <Ark/Utils/Logger.hpp>
31
32namespace Ark
33{
34 class Value;
35
36 /**
37 * @brief The welder joins all the compiler passes
38 */
39 class ARK_API Welder final
40 {
41 public:
42 /**
43 * @brief Create a new Welder
44 * @param debug debug level
45 * @param lib_env list of paths to the standard library
46 * @param features feature flags to toggle features on/off
47 */
48 Welder(unsigned debug, const std::vector<std::filesystem::path>& lib_env, uint16_t features = DefaultFeatures);
49
50 /**
51 * @brief Register a symbol as a global in the compiler
52 *
53 * @param name
54 */
55 void registerSymbol(const std::string& name);
56
57 /**
58 *
59 * @param filename
60 * @return true on success
61 */
62 bool computeASTFromFile(const std::string& filename);
63
64 /**
65 *
66 * @param code
67 * @return true on success
68 */
69 bool computeASTFromString(const std::string& code);
70
71 /**
72 * @brief Compile code from a string, with a set of known symbols (useful for the debugger)
73 *
74 * @param code
75 * @param symbols
76 * @return true on success
77 */
78 bool computeASTFromStringWithKnownSymbols(const std::string& code, const std::vector<std::string>& symbols);
79
80 /**
81 * @brief Compile the AST processed by computeASTFromFile / computeASTFromString
82 *
83 * @return true on success
84 */
85 bool generateBytecode();
86
87 /**
88 * @brief Compile the AST processed by computeASTFromFile / computeASTFromString, with prefilled symbols and constants tables (useful for the debugger)
89 *
90 * @param symbols
91 * @param constants
92 * @param start_page_at_offset
93 * @return true on success
94 */
95 bool generateBytecodeUsingTables(const std::vector<std::string>& symbols, const std::vector<Value>& constants, std::size_t start_page_at_offset);
96
97 /**
98 * @brief Save the generated bytecode to a given file
99 * @param filename
100 * @return true on success
101 */
102 bool saveBytecodeToFile(const std::string& filename);
103
104 /**
105 * @brief Redirect the logs to a given stream
106 * @param os output stream
107 */
108 void redirectLogsTo(std::ostream& os);
109
110 [[nodiscard]] const internal::Node& ast() const noexcept;
111 [[nodiscard]] std::string textualIR() const noexcept;
112 [[nodiscard]] const bytecode_t& bytecode() const noexcept;
113
114 private:
115 std::vector<std::filesystem::path> m_lib_env;
116 uint16_t m_features;
117
118 std::optional<std::filesystem::path> m_root_file;
119 std::vector<std::string> m_imports;
120 std::vector<internal::IR::Block> m_ir;
123
129
135
136 void dumpIRToFile() const;
137
138 bool computeAST(const std::string& filename, const std::string& code);
139 };
140} // namespace Ark
141
142#endif
Common code for the compiler.
Constants used by ArkScript.
Compile the intermediate representation to bytecode.
Try to inline IR blocks.
Optimize IR based on IR entity grouped by 2 (or more)
Handle imports, resolve them with modules and everything.
Internal logger.
#define ARK_API
Definition Module.hpp:22
Resolves names and fully qualify them in the AST (prefixing them with the package they are from)
AST node used by the parser, optimizer and compiler.
Optimizes a given ArkScript AST.
Parse ArkScript code, but do not handle any import declarations.
Handles the macros and their expansion in ArkScript source code.
The welder joins all the compiler passes.
Definition Welder.hpp:40
internal::ImportSolver m_import_solver
Definition Welder.hpp:125
internal::Node m_computed_ast
Definition Welder.hpp:122
internal::IROptimizer m_ir_optimizer
Definition Welder.hpp:133
std::vector< std::string > m_imports
Definition Welder.hpp:119
internal::Logger m_logger
Definition Welder.hpp:130
std::vector< internal::IR::Block > m_ir
Definition Welder.hpp:120
internal::NameResolutionPass m_name_resolver
Definition Welder.hpp:128
internal::IRInliner m_ir_inliner
Definition Welder.hpp:132
internal::ASTLowerer m_lowerer
Definition Welder.hpp:131
std::optional< std::filesystem::path > m_root_file
Definition Welder.hpp:118
internal::Parser m_parser
Definition Welder.hpp:124
internal::IRCompiler m_ir_compiler
Definition Welder.hpp:134
internal::MacroProcessor m_macro_processor
Definition Welder.hpp:126
uint16_t m_features
Definition Welder.hpp:116
internal::Optimizer m_ast_optimizer
Definition Welder.hpp:127
bytecode_t m_bytecode
Definition Welder.hpp:121
std::vector< std::filesystem::path > m_lib_env
Definition Welder.hpp:115
The ArkScript AST to IR compiler.
The class handling the macros definitions and calls, given an AST.
Definition Processor.hpp:32
A node of an Abstract Syntax Tree for ArkScript.
Definition Node.hpp:32
The ArkScript AST optimizer.
Definition Optimizer.hpp:29
constexpr uint16_t DefaultFeatures
Definition Constants.hpp:74
std::vector< uint8_t > bytecode_t
Definition Common.hpp:22