MCPcopy Create free account
hub / github.com/covscript/covscript / import

Method import

sources/instance/instance.cpp:74–129  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

72 }
73
74 namespace_t instance_type::import(const std::string &path, const std::string &name)
75 {
76 std::vector<std::string> collection;
77 {
78 std::string tmp;
79 for (auto &ch : path) {
80 if (ch == cs::path_delimiter) {
81 collection.push_back(tmp);
82 tmp.clear();
83 }
84 else
85 tmp.push_back(ch);
86 }
87 collection.push_back(tmp);
88 }
89 std::exception_ptr eptr = nullptr;
90 for (auto &it : collection) {
91 std::string package_path = it + path_separator + name;
92 if (context->compiler->modules.count(package_path) > 0)
93 return context->compiler->modules[package_path];
94 if (std::ifstream(package_path + ".csp")) {
95 context_t rt = create_subcontext(context);
96 rt->compiler->import_csym(package_path + ".csp", package_path + ".csym");
97 rt->compiler->swap_context(rt);
98 try {
99 rt->instance->compile(package_path + ".csp");
100 }
101 catch (...) {
102 context->compiler->swap_context(context);
103 throw;
104 }
105 context->compiler->swap_context(context);
106 rt->instance->interpret();
107 if (rt->package_name.empty())
108 throw runtime_error("Target file is not a package.");
109 if (rt->package_name != name)
110 throw runtime_error("Package name is different from file name.");
111 namespace_t module = std::make_shared<name_space>(*rt->instance->storage.get_namespace());
112 context->compiler->modules.emplace(package_path, module);
113 return module;
114 }
115 else if (std::ifstream(package_path + ".cse")) {
116 try {
117 namespace_t module = std::make_shared<extension>(package_path + ".cse");
118 context->compiler->modules.emplace(package_path, module);
119 return module;
120 }
121 catch (...) {
122 eptr = std::current_exception();
123 }
124 }
125 }
126 if (eptr != nullptr)
127 std::rethrow_exception(eptr);
128 throw fatal_error("No such file or directory.");
129 }
130
131 void instance_type::compile(const std::string &path)

Callers 3

preprocessMethod · 0.80
get_namespaceMethod · 0.80
importFunction · 0.80

Calls 10

create_subcontextFunction · 0.85
runtime_errorClass · 0.85
fatal_errorClass · 0.85
import_csymMethod · 0.80
compileMethod · 0.80
interpretMethod · 0.80
clearMethod · 0.45
swap_contextMethod · 0.45
emptyMethod · 0.45
get_namespaceMethod · 0.45

Tested by

no test coverage detected