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
24#include <Ark/Constants.hpp>
25#include <Ark/Utils/Logger.hpp>
30
31namespace Ark
32{
33 class Value;
34
35 /**
36 * @brief The welder joins all the compiler passes
37 */
38 class ARK_API Welder final
39 {
40 public:
41 /**
42 * @brief Create a new Welder
43 * @param debug debug level
44 * @param lib_env list of paths to the standard library
45 * @param features feature flags to toggle features on/off
46 */
47 Welder(unsigned debug, const std::vector<std::filesystem::path>& lib_env, uint16_t features = DefaultFeatures);
48
49 /**
50 * @brief Register a symbol as a global in the compiler
51 *
52 * @param name
53 */
54 void registerSymbol(const std::string& name);
55
56 /**
57 *
58 * @param filename
59 * @return true on success
60 */
61 bool computeASTFromFile(const std::string& filename);
62
63 /**
64 *
65 * @param code
66 * @return true on success
67 */
68 bool computeASTFromString(const std::string& code);
69
70 /**
71 * @brief Compile code from a string, with a set of known symbols (useful for the debugger)
72 *
73 * @param code
74 * @param symbols
75 * @return true on success
76 */
77 bool computeASTFromStringWithKnownSymbols(const std::string& code, const std::vector<std::string>& symbols);
78
79 /**
80 * @brief Compile the AST processed by computeASTFromFile / computeASTFromString
81 *
82 * @return true on success
83 */
84 bool generateBytecode();
85
86 /**
87 * @brief Compile the AST processed by computeASTFromFile / computeASTFromString, with prefilled symbols and constants tables (useful for the debugger)
88 *
89 * @param symbols
90 * @param constants
91 * @param start_page_at_offset
92 * @return true on success
93 */
94 bool generateBytecodeUsingTables(const std::vector<std::string>& symbols, const std::vector<Value>& constants, std::size_t start_page_at_offset);
95
96 /**
97 * @brief Save the generated bytecode to a given file
98 * @param filename
99 * @return true on success
100 */
101 bool saveBytecodeToFile(const std::string& filename);
102
103 /**
104 * @brief Redirect the logs to a given stream
105 * @param os output stream
106 */
107 void redirectLogsTo(std::ostream& os);
108
109 [[nodiscard]] const internal::Node& ast() const noexcept;
110 [[nodiscard]] std::string textualIR() const noexcept;
111 [[nodiscard]] const bytecode_t& bytecode() const noexcept;
112
113 private:
114 std::vector<std::filesystem::path> m_lib_env;
115 uint16_t m_features;
116
117 std::filesystem::path m_root_file;
118 std::vector<std::string> m_imports;
119 std::vector<internal::IR::Block> m_ir;
122
128
133
134 void dumpIRToFile() const;
135
136 bool computeAST(const std::string& filename, const std::string& code);
137 };
138} // namespace Ark
139
140#endif
Common code for the compiler.
Constants used by ArkScript.
Compile the intermediate representation to bytecode.
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:39
internal::ImportSolver m_import_solver
Definition Welder.hpp:124
internal::Node m_computed_ast
Definition Welder.hpp:121
internal::IROptimizer m_ir_optimizer
Definition Welder.hpp:131
std::vector< std::string > m_imports
Definition Welder.hpp:118
internal::Logger m_logger
Definition Welder.hpp:129
std::vector< internal::IR::Block > m_ir
Definition Welder.hpp:119
internal::NameResolutionPass m_name_resolver
Definition Welder.hpp:127
std::filesystem::path m_root_file
Definition Welder.hpp:117
internal::ASTLowerer m_lowerer
Definition Welder.hpp:130
internal::Parser m_parser
Definition Welder.hpp:123
internal::IRCompiler m_ir_compiler
Definition Welder.hpp:132
internal::MacroProcessor m_macro_processor
Definition Welder.hpp:125
uint16_t m_features
Definition Welder.hpp:115
internal::Optimizer m_ast_optimizer
Definition Welder.hpp:126
bytecode_t m_bytecode
Definition Welder.hpp:120
std::vector< std::filesystem::path > m_lib_env
Definition Welder.hpp:114
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:67
std::vector< uint8_t > bytecode_t
Definition Common.hpp:22