ArkScript
A small, fast, functional and scripting language for video games
ImportSolver.hpp
Go to the documentation of this file.
1/**
2 * @file ImportSolver.hpp
3 * @author Alexandre Plateau ([email protected])
4 * @brief Handle imports, resolve them with modules and everything
5 * @version 2.1
6 * @date 2024-07-21
7 *
8 * @copyright Copyright (c) 2020-2024
9 *
10 */
11
12#ifndef ARK_COMPILER_IMPORTSOLVER_HPP
13#define ARK_COMPILER_IMPORTSOLVER_HPP
14
15#include <stack>
16#include <vector>
17#include <string>
18#include <filesystem>
19#include <unordered_map>
20
21#include <Ark/Compiler/Pass.hpp>
25
26namespace Ark::internal
27{
28 class ImportSolver final : public Pass
29 {
30 public:
31 /**
32 * @brief Create a new ImportSolver
33 * @param debug debug level
34 * @param libenv list of paths to the standard library
35 */
36 ImportSolver(unsigned debug, const std::vector<std::filesystem::path>& libenv);
37
38 /**
39 * @brief Configure the ImportSolver
40 * @param root path to the root file that imports all others
41 * @param origin_imports the first imports to go through
42 * @return ImportSolver& *this
43 */
44 ImportSolver& setup(const std::filesystem::path& root, const std::vector<Import>& origin_imports);
45
46 void process(const Node& origin_ast) override;
47
48 [[nodiscard]] const Node& ast() const noexcept override;
49
50 private:
51 unsigned m_debug_level;
52 std::vector<std::filesystem::path> m_libenv;
53 std::filesystem::path m_root; ///< Folder were the entry file is
55 std::stack<Import> m_imports;
56 std::unordered_map<std::string, Package> m_packages; ///< Package name to package AST & data mapping
57 std::vector<std::string> m_imported; ///< List of imports, in the order they were found and parsed
58
59 /**
60 * @brief Visits the AST, looking for import nodes to replace with their parsed module version
61 * @param ast
62 * @return
63 */
64 std::pair<Node, bool> findAndReplaceImports(const Node& ast);
65
66 /**
67 * @brief Parse a given file and returns a list of its imports.
68 * The AST is parsed and stored in m_modules[import.prefix]
69 *
70 * @param base_path path to the file containing the import
71 * @param import current import directive
72 * @return std::vector<Import> imports found in the processed file
73 */
74 std::vector<Import> parseImport(const std::filesystem::path& base_path, const Import& import);
75
76 /**
77 * @brief Search for an import file, using the root file path
78 *
79 * @param file path to the file containing the import
80 * @param import current import directive
81 * @return std::filesystem::path
82 */
83 [[nodiscard]] std::filesystem::path findFile(const std::filesystem::path& file, const Import& import) const;
84 };
85}
86
87#endif
AST node used by the parser, optimizer and compiler.
Interface for a compiler pass (take in an AST, output an AST)
std::filesystem::path m_root
Folder were the entry file is.
std::unordered_map< std::string, Package > m_packages
Package name to package AST & data mapping.
void process(const Node &origin_ast) override
Start processing the given AST.
std::filesystem::path findFile(const std::filesystem::path &file, const Import &import) const
Search for an import file, using the root file path.
std::vector< std::filesystem::path > m_libenv
std::stack< Import > m_imports
std::pair< Node, bool > findAndReplaceImports(const Node &ast)
Visits the AST, looking for import nodes to replace with their parsed module version.
ImportSolver & setup(const std::filesystem::path &root, const std::vector< Import > &origin_imports)
Configure the ImportSolver.
ImportSolver(unsigned debug, const std::vector< std::filesystem::path > &libenv)
Create a new ImportSolver.
std::vector< Import > parseImport(const std::filesystem::path &base_path, const Import &import)
Parse a given file and returns a list of its imports. The AST is parsed and stored in m_modules[impor...
std::vector< std::string > m_imported
List of imports, in the order they were found and parsed.
const Node & ast() const noexcept override
Output of the compiler pass.
A node of an Abstract Syntax Tree for ArkScript.
Definition Node.hpp:31
An interface to describe compiler passes.
Definition Pass.hpp:23