Skip to content
Snippets Groups Projects
Commit 5e89fc1a authored by Stéphane Del Pino's avatar Stéphane Del Pino
Browse files

Add getModuleInfo function in core module (string -> string)

This function lists  is functions and types provided by a module
parent 5af5af83
No related branches found
No related tags found
1 merge request!67Feature/inline help
......@@ -84,6 +84,24 @@ dataTypeName(const ASTNodeDataType& data_type)
return name;
}
std::string
dataTypeName(const std::vector<ASTNodeDataType>& data_type_vector)
{
if (data_type_vector.size() == 0) {
return dataTypeName(ASTNodeDataType::build<ASTNodeDataType::void_t>());
} else if (data_type_vector.size() == 1) {
return dataTypeName(data_type_vector[0]);
} else {
std::ostringstream os;
os << '(' << dataTypeName(data_type_vector[0]);
for (size_t i = 1; i < data_type_vector.size(); ++i) {
os << ',' << dataTypeName(data_type_vector[i]);
}
os << ')';
return os.str();
}
}
ASTNodeDataType
dataTypePromotion(const ASTNodeDataType& data_type_1, const ASTNodeDataType& data_type_2)
{
......
......@@ -14,6 +14,8 @@ class ASTNodeDataType;
ASTNodeDataType getVectorDataType(const ASTNode& type_node);
std::string dataTypeName(const std::vector<ASTNodeDataType>& data_type_vector);
std::string dataTypeName(const ASTNodeDataType& data_type);
ASTNodeDataType dataTypePromotion(const ASTNodeDataType& data_type_1, const ASTNodeDataType& data_type_2);
......
......@@ -29,5 +29,16 @@ CoreModule::CoreModule() : BuiltinModule(true)
return repository.getAvailableModules();
}
));
this->_addBuiltinFunction("getModuleInfo", std::make_shared<BuiltinFunctionEmbedder<std::string(const std::string&)>>(
[](const std::string& module_name) -> std::string {
const ModuleRepository& repository =
ASTExecutionInfo::current().moduleRepository();
return repository.getModuleInfo(module_name);
}
));
}
......@@ -11,6 +11,7 @@
#include <language/utils/BuiltinFunctionEmbedder.hpp>
#include <language/utils/ParseError.hpp>
#include <language/utils/SymbolTable.hpp>
#include <language/utils/TypeDescriptor.hpp>
#include <utils/PugsAssert.hpp>
#include <algorithm>
......@@ -115,3 +116,37 @@ ModuleRepository::getAvailableModules() const
return os.str();
}
std::string
ModuleRepository::getModuleInfo(const std::string& module_name) const
{
std::stringstream os;
auto i_module = m_module_set.find(module_name);
if (i_module != m_module_set.end()) {
os << rang::fgB::yellow << "Module '" << rang::fgB::blue << module_name << rang::fgB::yellow << "' provides"
<< rang::style::reset << '\n';
const auto& builtin_function_map = i_module->second->getNameBuiltinFunctionMap();
if (builtin_function_map.size() > 0) {
os << " functions\n";
for (auto& [name, function] : builtin_function_map) {
os << " " << rang::fgB::green << name << rang::style::reset << ": ";
os << dataTypeName(function->getParameterDataTypes());
os << rang::fgB::yellow << " -> " << rang::style::reset;
os << dataTypeName(function->getReturnDataType()) << '\n';
}
}
const auto& builtin_type_map = i_module->second->getNameTypeMap();
if (builtin_type_map.size() > 0) {
os << " types\n";
for (auto& [name, descriptor] : builtin_type_map) {
os << " " << rang::fgB::green << name << rang::style::reset << '\n';
}
}
} else {
throw NormalError(std::string{"could not find module "} + module_name);
}
return os.str();
}
......@@ -31,6 +31,7 @@ class ModuleRepository
void populateMandatorySymbolTable(const ASTNode& root_node, SymbolTable& symbol_table);
std::string getAvailableModules() const;
std::string getModuleInfo(const std::string& module_name) const;
const ModuleRepository& operator=(const ModuleRepository&) = delete;
const ModuleRepository& operator=(ModuleRepository&&) = delete;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment