ArkScript
A small, fast, functional and scripting language for video games
ImportSolver.cpp
Go to the documentation of this file.
2
3#include <ranges>
4#include <algorithm>
5#include <fmt/core.h>
6
7#include <Ark/Files.hpp>
8#include <Ark/Exceptions.hpp>
10
11namespace Ark::internal
12{
13 ImportSolver::ImportSolver(const unsigned debug, const std::vector<std::filesystem::path>& libenv) :
14 Pass("ImportSolver", debug), m_debug_level(debug), m_libenv(libenv), m_ast()
15 {}
16
17 ImportSolver& ImportSolver::setup(const std::filesystem::path& root, const std::vector<Import>& origin_imports)
18 {
19 if (is_directory(root))
20 m_root = root;
21 else
22 m_root = root.parent_path();
23
24 for (const auto& origin_import : std::ranges::reverse_view(origin_imports))
25 m_imports.push(origin_import);
26
27 return *this;
28 }
29
30 void ImportSolver::process(const Node& origin_ast)
31 {
32 m_logger.traceStart("process");
33
34 while (!m_imports.empty())
35 {
36 Import import = m_imports.top();
37 m_logger.debug("Importing {}", import.toPackageString());
38
39 // Remove the top element to process the other imports
40 // It needs to be removed first because we might be adding
41 // other imports later and don't want to pop THEM
42 m_imports.pop();
43 const auto package = import.toPackageString();
44
45 if (m_packages.contains(package))
46 {
47 // merge the definition, so that we can generate valid Full Qualified Names in the name & scope resolver
48 m_packages[package].import.with_prefix |= import.with_prefix;
49 m_packages[package].import.is_glob |= import.is_glob;
50 for (auto&& symbol : import.symbols)
51 m_packages[package].import.symbols.push_back(symbol);
52 }
53 else
54 {
55 // NOTE: since the "file" (=root) argument doesn't change between all calls, we could get rid of it
56 std::vector<Import> temp = parseImport(m_root, import);
57 for (auto& additional_import : std::ranges::reverse_view(temp))
58 m_imports.push(additional_import);
59 }
60 }
61
62 m_logger.traceStart("findAndReplaceImports");
63 m_ast = findAndReplaceImports(origin_ast).first;
65
67 }
68
69 std::pair<Node, bool> ImportSolver::findAndReplaceImports(const Node& ast)
70 {
71 Node x = ast;
72 if (x.nodeType() == NodeType::List)
73 {
74 if (x.constList().size() >= 2 && x.constList()[0].nodeType() == NodeType::Keyword &&
75 x.constList()[0].keyword() == Keyword::Import)
76 {
77 // compute the package string: foo.bar.egg
78 const auto import_node = x.constList()[1].constList();
79 const std::string package = std::accumulate(
80 std::next(import_node.begin()),
81 import_node.end(),
82 import_node[0].string(),
83 [](const std::string& acc, const Node& elem) -> std::string {
84 return acc + "." + elem.string();
85 });
86
87 // if it wasn't imported already, register it
88 if (std::ranges::find(m_imported, package) == m_imported.end())
89 {
90 m_imported.push_back(package);
91 // modules are already handled, we can safely replace the node
92 x = m_packages[package].ast;
93 if (!m_packages[package].has_been_processed)
94 {
95 const auto import = m_packages[package].import;
96
97 // prefix to lowercase ; usually considered unsafe (https://devblogs.microsoft.com/oldnewthing/20241007-00/?p=110345)
98 // but we are dealing with prefix from filenames, thus we can somewhat assume we are in safe zone
99 std::string prefix = import.prefix;
100 std::ranges::transform(
101 prefix, prefix.begin(),
102 [](auto c) {
103 return std::tolower(c);
104 });
105
106 x = Node(Namespace {
107 .name = prefix,
108 .is_glob = import.is_glob,
109 .with_prefix = import.with_prefix,
110 .symbols = import.symbols,
111 .ast = std::make_shared<Node>(findAndReplaceImports(x).first) });
112 x.arkNamespace().ast->setPos(ast.line(), ast.col());
113 x.arkNamespace().ast->setFilename(ast.filename());
114 }
115 // we parsed an import node, return true in the pair to notify the caller
116 return std::make_pair(x, /* is_import= */ true);
117 }
118
119 // Replace by empty node to avoid breaking the code gen
120 x = Node(NodeType::List);
122 }
123 else
124 {
125 for (std::size_t i = 0; i < x.constList().size(); ++i)
126 {
127 auto [node, is_import] = findAndReplaceImports(x.constList()[i]);
128 x.list()[i] = node;
129 }
130 }
131 }
132
133 return std::make_pair(x, /* is_import= */ false);
134 }
135
136 const Node& ImportSolver::ast() const noexcept
137 {
138 return m_ast;
139 }
140
141 std::vector<Import> ImportSolver::parseImport(const std::filesystem::path& base_path, const Import& import)
142 {
143 m_logger.traceStart(fmt::format("parseImport {}", base_path.string()));
144
145 const auto path = findFile(base_path, import);
146 if (path.extension() == ".arkm") // Nothing to import in case of modules
147 {
148 // Creating an import node that will stay there when visiting the AST and
149 // replacing the imports with their parsed module
150 auto module_node = Node(NodeType::List);
151 module_node.push_back(Node(Keyword::Import));
152
153 auto package_node = Node(NodeType::List);
154 std::ranges::transform(import.package, std::back_inserter(package_node.list()), [](const std::string& stem) {
155 return Node(NodeType::String, stem);
156 });
157 module_node.push_back(package_node);
158 // empty symbols list
159 module_node.push_back(Node(NodeType::List));
160
161 m_packages[import.toPackageString()] = Package {
162 module_node,
163 import,
164 true
165 };
166
167 return {};
168 }
169
170 Parser parser(m_debug_level);
171 const std::string code = Utils::readFile(path.generic_string());
172 parser.process(path.string(), code);
173 m_packages[import.toPackageString()] = Package {
174 parser.ast(),
175 import,
176 false
177 };
178
180 return parser.imports();
181 }
182
183 std::optional<std::filesystem::path> testExtensions(const std::filesystem::path& folder, const std::string& package_path)
184 {
185 if (auto code_path = folder / (package_path + ".ark"); std::filesystem::exists(code_path))
186 return code_path;
187 if (auto module_path = folder / (package_path + ".arkm"); std::filesystem::exists(module_path))
188 return module_path;
189 return {};
190 }
191
192 std::filesystem::path ImportSolver::findFile(const std::filesystem::path& file, const Import& import) const
193 {
194 const std::string package_path = import.packageToPath();
195 if (auto maybe_path = testExtensions(m_root, package_path); maybe_path.has_value())
196 return maybe_path.value();
197
198 // search in all folders in environment path
199 for (const auto& path : m_libenv)
200 {
201 if (auto maybe_path = testExtensions(path, package_path); maybe_path.has_value())
202 return maybe_path.value();
203 }
204
205 // fallback, we couldn't find the file
206 throw CodeError(
207 fmt::format("While processing file {}, couldn't import {}: file not found",
208 file.generic_string(), import.toPackageString()),
209 file.generic_string(),
210 import.line,
211 import.col,
212 fmt::format("(import {})", import.toPackageString()));
213 }
214}
ArkScript homemade exceptions.
Lots of utilities about the filesystem.
Handle imports, resolve them with modules and everything.
Parse ArkScript code, but do not handle any import declarations.
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.
void debug(const char *fmt, Args &&... args)
Write a debug level log using fmtlib.
Definition Logger.hpp:65
void traceStart(std::string &&trace_name)
Definition Logger.hpp:75
A node of an Abstract Syntax Tree for ArkScript.
Definition Node.hpp:31
NodeType nodeType() const noexcept
Return the node type.
Definition Node.cpp:77
const std::string & filename() const noexcept
Return the filename in which this node was created.
Definition Node.cpp:145
const std::vector< Node > & constList() const noexcept
Return the list of sub-nodes held by the node.
Definition Node.cpp:72
Namespace & arkNamespace() noexcept
Return the namespace held by the value (if the node type allows it)
Definition Node.cpp:52
std::size_t col() const noexcept
Get the column at which this node was created.
Definition Node.cpp:140
void push_back(const Node &node) noexcept
Every node has a list as well as a value so we can push_back on all node no matter their type.
Definition Node.cpp:62
std::size_t line() const noexcept
Get the line at which this node was created.
Definition Node.cpp:135
std::vector< Node > & list() noexcept
Return the list of sub-nodes held by the node.
Definition Node.cpp:67
void process(const std::string &filename, const std::string &code)
Parse the given code.
Definition Parser.cpp:14
const Node & ast() const noexcept
Definition Parser.cpp:48
const std::vector< Import > & imports() const
Definition Parser.cpp:53
An interface to describe compiler passes.
Definition Pass.hpp:23
std::string readFile(const std::string &name)
Helper to read a file.
Definition Files.hpp:48
std::optional< std::filesystem::path > testExtensions(const std::filesystem::path &folder, const std::string &package_path)
CodeError thrown by the compiler (parser, macro processor, optimizer, and compiler itself)
std::shared_ptr< Node > ast
Definition Namespace.hpp:18