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 * @date 2024-07-21
6 *
7 * @copyright Copyright (c) 2020-2025
8 *
9 */
10
11#ifndef ARK_COMPILER_IMPORTSOLVER_HPP
12#define ARK_COMPILER_IMPORTSOLVER_HPP
13
14#include <stack>
15#include <vector>
16#include <string>
17#include <filesystem>
18#include <unordered_map>
19
20#include <Ark/Platform.hpp>
21#include <Ark/Compiler/Pass.hpp>
25
26namespace Ark::internal
27{
28 class ARK_API 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:
52 {
53 std::filesystem::path file;
54 Import import;
55 };
56
57 unsigned m_debug_level;
58 std::vector<std::filesystem::path> m_libenv;
59 std::filesystem::path m_root; ///< Folder were the entry file is
61 std::stack<ImportWithSource> m_imports;
62 std::unordered_map<std::string, Package> m_packages; ///< Package name to package AST & data mapping
63 std::vector<std::string> m_imported; ///< List of imports, in the order they were found and parsed
64
65 /**
66 * @brief Visits the AST, looking for import nodes to replace with their parsed module version
67 * @param ast
68 * @return
69 */
70 std::pair<Node, bool> findAndReplaceImports(const Node& ast);
71
72 /**
73 * @brief Parse a given file and returns a list of its imports.
74 * The AST is parsed and stored in m_modules[import.prefix]
75 *
76 * @param source path to the file containing the import
77 * @param import current import directive
78 * @return std::vector<ImportWithSource> imports found in the processed file
79 */
80 std::vector<ImportWithSource> parseImport(const std::filesystem::path& source, const Import& import);
81
82 /**
83 * @brief Search for an import file, using the root file path
84 *
85 * @param file path to the file containing the import
86 * @param import current import directive
87 * @return std::filesystem::path
88 */
89 [[nodiscard]] std::filesystem::path findFile(const std::filesystem::path& file, const Import& import) const;
90 };
91}
92
93#endif
#define ARK_API
Definition Module.hpp:28
AST node used by the parser, optimizer and compiler.
Interface for a compiler pass (take in an AST, output an AST)
ArkScript configuration macros.
std::stack< ImportWithSource > m_imports
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.
std::vector< std::filesystem::path > m_libenv
std::vector< std::string > m_imported
List of imports, in the order they were found and parsed.
A node of an Abstract Syntax Tree for ArkScript.
Definition Node.hpp:30
An interface to describe compiler passes.
Definition Pass.hpp:23