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

Add getAST, getFunctionAST and saveASTDot functions to 'utils'

The two first ones return a string containing the AST the later one
saves the AST to a file (its name is given as a string argument).
parent a37e6a09
Branches
Tags
1 merge request!66Feature/reduced verbosity
......@@ -32,6 +32,8 @@
#include <unordered_map>
#include <variant>
const std::unique_ptr<ASTNode>* p_root_node = nullptr;
void
parser(const std::string& filename)
{
......@@ -48,6 +50,7 @@ parser(const std::string& filename)
auto parse_and_execute = [](auto& input) {
std::unique_ptr<ASTNode> root_node = ASTBuilder::build(input);
p_root_node = &root_node;
ASTModulesImporter{*root_node};
ASTNodeTypeCleaner<language::import_instruction>{*root_node};
......@@ -68,35 +71,15 @@ parser(const std::string& filename)
ASTNodeTypeCleaner<language::var_declaration>{*root_node};
ASTNodeTypeCleaner<language::fct_declaration>{*root_node};
{
std::string dot_filename{"parse_tree.dot"};
std::ofstream fout(dot_filename);
ASTDotPrinter dot_printer{*root_node};
fout << dot_printer;
std::cout << " AST dot file: " << dot_filename << '\n';
}
ASTNodeEmptyBlockCleaner{*root_node};
ASTNodeExpressionBuilder{*root_node};
std::cout << ASTPrinter{*root_node} << '\n';
auto& function_table = root_node->m_symbol_table->functionTable();
for (size_t i_function = 0; i_function < function_table.size(); ++i_function) {
const auto& function_descriptor = function_table[i_function];
std::cout << "function " << rang::fgB::magenta << function_descriptor.name() << rang::style::reset << '\n';
std::cout << ASTPrinter(function_descriptor.domainMappingNode());
std::cout << ASTPrinter(function_descriptor.definitionNode());
std::cout << "--------\n";
}
ExecutionPolicy exec_all;
root_node->execute(exec_all);
std::cout << *(root_node->m_symbol_table) << '\n';
root_node->m_symbol_table->clearValues();
p_root_node = nullptr;
};
if (not SignalManager::pauseOnError()) {
......
#include <language/modules/UtilsModule.hpp>
#include <language/utils/ASTDotPrinter.hpp>
#include <language/utils/ASTPrinter.hpp>
#include <language/utils/BuiltinFunctionEmbedder.hpp>
#include <language/utils/SymbolTable.hpp>
#include <utils/PugsUtils.hpp>
extern const std::unique_ptr<ASTNode>* p_root_node;
UtilsModule::UtilsModule()
{
this->_addBuiltinFunction("getPugsVersion", std::make_shared<BuiltinFunctionEmbedder<std::string(void)>>(
......@@ -15,5 +20,65 @@ UtilsModule::UtilsModule()
[]() -> std::string { return pugsBuildInfo(); }
));
this->_addBuiltinFunction("getAST", std::make_shared<BuiltinFunctionEmbedder<std::string(void)>>(
[]() -> std::string {
Assert(p_root_node != nullptr, "unable to find AST's root node");
const auto& root_node = *p_root_node;
std::ostringstream os;
os << ASTPrinter{*root_node};
return os.str();
}
));
this->_addBuiltinFunction("saveASTDot", std::make_shared<BuiltinFunctionEmbedder<void(const std::string&)>>(
[](const std::string& dot_filename) -> void {
Assert(p_root_node != nullptr, "unable to find AST's root node");
const auto& root_node = *p_root_node;
std::ofstream fout(dot_filename);
if (not fout) {
std::ostringstream os;
os << "could not create file '" << dot_filename << "'\n";
throw NormalError(os.str());
}
ASTDotPrinter dot_printer{*root_node};
fout << dot_printer;
if (not fout) {
std::ostringstream os;
os << "could not write AST to '" << dot_filename << "'\n";
throw NormalError(os.str());
}
}
));
this->_addBuiltinFunction("getFunctionAST",
std::make_shared<BuiltinFunctionEmbedder<std::string(const FunctionSymbolId&)>>(
[](const FunctionSymbolId& function_symbol_id) -> std::string {
auto& function_table = function_symbol_id.symbolTable().functionTable();
const auto& function_descriptor = function_table[function_symbol_id.id()];
std::ostringstream os;
os << function_descriptor.name() << ": domain mapping\n";
os << ASTPrinter(function_descriptor.domainMappingNode());
os << function_descriptor.name() << ": definition\n";
os << ASTPrinter(function_descriptor.definitionNode());
return os.str();
}
));
}
......@@ -104,11 +104,11 @@ add_library(test_Pugs_MeshDataBase
target_link_libraries (unit_tests
test_Pugs_MeshDataBase
PugsLanguage
PugsLanguageAST
PugsLanguageModules
PugsLanguageAlgorithms
PugsLanguageUtils
PugsLanguage
PugsMesh
PugsAlgebra
PugsUtils
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment