15#include <fmt/ostream.h>
19 Welder::Welder(
const unsigned debug,
const std::vector<std::filesystem::path>& lib_env,
const uint16_t features) :
20 m_lib_env(lib_env), m_features(features),
23 m_import_solver(debug, lib_env),
24 m_macro_processor(debug),
25 m_ast_optimizer(debug),
26 m_name_resolver(debug),
27 m_logger(
"Welder", debug),
29 m_ir_optimizer(debug),
57 for (
const std::string& sym : symbols)
95 std::vector<internal::ValTableElem> values;
96 for (
const Value& constant : constants)
98 switch (constant.valueType())
101 values.emplace_back(constant.number());
105 values.emplace_back(constant.string());
109 values.emplace_back(
static_cast<std::size_t
>(constant.pageAddr()));
113 assert(
false &&
"This should not be possible to have a constant that isn't a Number, a String or a PageAddr");
130 std::ofstream output(filename, std::ofstream::binary);
133 static_cast<std::streamsize
>(
m_bytecode.size() *
sizeof(uint8_t)));
157 std::stringstream stream;
174 const auto filename = path.filename().replace_extension(
".ark.ir");
175 path.remove_filename();
179 std::ofstream output(path);
Constants used by ArkScript.
#define ARK_CACHE_DIRNAME
Tools to report code errors nicely to the user.
ArkScript homemade exceptions.
Lots of utilities about the filesystem.
Handle imports, resolve them with modules and everything.
Resolves names and fully qualify them in the AST (prefixing them with the package they are from)
Optimizes a given ArkScript AST.
Handles the macros and their expansion in ArkScript source code.
Default value type handled by the virtual machine.
In charge of welding everything needed to compile code.
bool computeAST(const std::string &filename, const std::string &code)
internal::ImportSolver m_import_solver
internal::Node m_computed_ast
Welder(unsigned debug, const std::vector< std::filesystem::path > &lib_env, uint16_t features=DefaultFeatures)
Create a new Welder.
internal::IROptimizer m_ir_optimizer
void registerSymbol(const std::string &name)
Register a symbol as a global in the compiler.
bool computeASTFromString(const std::string &code)
std::string textualIR() const noexcept
internal::Logger m_logger
bool generateBytecodeUsingTables(const std::vector< std::string > &symbols, const std::vector< Value > &constants, std::size_t start_page_at_offset)
Compile the AST processed by computeASTFromFile / computeASTFromString, with prefilled symbols and co...
std::vector< internal::IR::Block > m_ir
internal::NameResolutionPass m_name_resolver
std::filesystem::path m_root_file
internal::ASTLowerer m_lowerer
const bytecode_t & bytecode() const noexcept
internal::Parser m_parser
internal::IRCompiler m_ir_compiler
internal::MacroProcessor m_macro_processor
void redirectLogsTo(std::ostream &os)
Redirect the logs to a given stream.
bool saveBytecodeToFile(const std::string &filename)
Save the generated bytecode to a given file.
bool generateBytecode()
Compile the AST processed by computeASTFromFile / computeASTFromString.
bool computeASTFromFile(const std::string &filename)
internal::Optimizer m_ast_optimizer
bool computeASTFromStringWithKnownSymbols(const std::string &code, const std::vector< std::string > &symbols)
Compile code from a string, with a set of known symbols (useful for the debugger)
void dumpIRToFile() const
const internal::Node & ast() const noexcept
const std::vector< ValTableElem > & values() const noexcept
Return the value table pre-computed.
void process(Node &ast)
Start the compilation.
void offsetPagesBy(std::size_t offset)
Start bytecode pages at a given offset (by default, 0)
const std::vector< IR::Block > & intermediateRepresentation() const noexcept
Return the IR blocks (one per scope)
const std::vector< std::string > & symbols() const noexcept
Return the symbol table pre-computed.
void addToTables(const std::vector< std::string > &symbols, const std::vector< ValTableElem > &constants)
Pre-fill tables (used by the debugger)
void dumpToStream(std::ostream &stream) const
Dump the IR given to process to an output stream.
const bytecode_t & bytecode() const noexcept
Return the constructed bytecode object.
void process(const std::vector< IR::Block > &pages, const std::vector< std::string > &symbols, const std::vector< ValTableElem > &values)
Turn a given IR into bytecode.
const std::vector< IR::Block > & intermediateRepresentation() const noexcept
Return the IR blocks (one per scope)
void process(const std::vector< IR::Block > &pages, const std::vector< std::string > &symbols, const std::vector< ValTableElem > &values)
Turn a given IR into bytecode.
const Node & ast() const noexcept
ImportSolver & setup(const std::filesystem::path &root, const std::vector< Import > &origin_imports)
Configure the ImportSolver.
void process(const Node &origin_ast)
void info(const char *fmt, Args &&... args)
Write an info level log using fmtlib.
void configureOutputStream(std::ostream *os)
Set a custom output stream to use for warnings. This will disable colors.
const Node & ast() const noexcept
Return the modified AST.
void process(const Node &ast)
Send the complete AST and work on it.
const Node & ast() const noexcept
Unused overload that return the input AST (untouched as this pass only generates errors)
void process(const Node &ast)
Start visiting the given AST, checking for mutability violation and unbound variables.
std::string addDefinedSymbol(const std::string &sym, bool is_mutable)
Register a symbol as defined, so that later we can throw errors on undefined symbols.
A node of an Abstract Syntax Tree for ArkScript.
const Node & ast() const noexcept
Returns the modified AST.
void process(const Node &ast)
Send the AST to the optimizer, then run the different optimization strategies on it.
void process(const std::string &filename, const std::string &code)
Parse the given code.
const Node & ast() const noexcept
const std::vector< Import > & imports() const
void configureLogger(std::ostream &os)
Set a custom output stream for the logger.
ARK_API void generateWithCode(const CodeError &e, const std::string &code, std::ostream &os=std::cerr, bool colorize=true)
ARK_API void generate(const CodeError &e, std::ostream &os=std::cerr, bool colorize=true)
Generate a diagnostic from an error and print it to the standard error output.
std::string readFile(const std::string &name)
Helper to read a file.
NodeType
The different node types available.
constexpr uint16_t FeatureImportSolver
constexpr uint16_t FeatureIROptimizer
constexpr uint16_t FeatureMacroProcessor
constexpr uint16_t FeatureTestFailOnException
This feature should only be used in tests, to disable diagnostics generation and enable exceptions to...
std::vector< uint8_t > bytecode_t
constexpr uint16_t FeatureASTOptimizer
Disabled by default because embedding ArkScript should not prune nodes from the AST ; it is active in...
constexpr uint16_t FeatureDumpIR
constexpr uint16_t FeatureNameResolver
CodeError thrown by the compiler (parser, macro processor, optimizer, and compiler itself)