ArkScript
A small, lisp-inspired, functional scripting language
Import.hpp
Go to the documentation of this file.
1#ifndef ARK_COMPILER_AST_IMPORT_HPP
2#define ARK_COMPILER_AST_IMPORT_HPP
3
4#include <vector>
5#include <string>
6#include <numeric>
7
9
10namespace Ark::internal
11{
13 {
14 std::size_t line, col; ///< Position in the source file
15
16 /**
17 * @brief The filename without the extension
18 * @details Example: `(import foo.bar)` => `bar`
19 * `(import foo.bar.egg:*)` => `egg`
20 * `(import foo :a :b :c)` => `foo`
21 *
22 */
23 std::string prefix;
24
25 /**
26 * @brief Package with all the segments
27 * @details Example: `(import foo.bar)` => `{foo, bar}`
28 * `(import foo.bar.egg:*)` => `{foo, bar, egg}`
29 * `(import foo :a :b :c)` => `{foo}`
30 */
31 std::vector<std::string> package;
32
33 /**
34 * @brief Import with prefix (import package)
35 *
36 */
37 bool with_prefix = true;
38
39 /**
40 * @brief Import as glob (import package:*)
41 */
42 bool is_glob = false;
43
44 /**
45 * @brief List of symbols to import, can be empty if none provided. (import package :a :b)
46 *
47 */
48 std::vector<std::string> symbols;
49
50 /**
51 *
52 * @return a package string, eg a.b.c
53 */
54 [[nodiscard]] std::string toPackageString() const
55 {
56 return std::accumulate(
57 package.begin() + 1,
58 package.end(),
59 package.front(),
60 [](const std::string& left, const std::string& right) {
61 return left + "." + right;
62 });
63 }
64
65 /**
66 *
67 * @return a package as a path, eg a/b/c
68 */
69 [[nodiscard]] std::string packageToPath() const
70 {
71 return std::accumulate(
72 std::next(package.begin()),
73 package.end(),
74 package[0],
75 [](const std::string& a, const std::string& b) {
76 return a + "/" + b;
77 });
78 }
79
80 /**
81 * @brief Check if we should import everything with a prefix, given a `(import foo.bar.egg)`
82 *
83 * @return true
84 * @return false
85 */
86 [[nodiscard]] bool isBasic() const
87 {
88 return with_prefix && symbols.empty();
89 }
90 };
91}
92
93#endif
#define ARK_API
Definition Module.hpp:22
ArkScript configuration macros.
bool isBasic() const
Check if we should import everything with a prefix, given a (import foo.bar.egg)
Definition Import.hpp:86
std::vector< std::string > symbols
List of symbols to import, can be empty if none provided. (import package :a :b)
Definition Import.hpp:48
std::size_t col
Position in the source file.
Definition Import.hpp:14
std::string prefix
The filename without the extension.
Definition Import.hpp:23
std::string packageToPath() const
Definition Import.hpp:69
std::string toPackageString() const
Definition Import.hpp:54
std::vector< std::string > package
Package with all the segments.
Definition Import.hpp:31