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

Create ASTNodeDataTypeFlattener

This class purpose is to convert compound type lists into list of "simple"
types. It is an helper class that should be used in many contexts: list
affectations, function parameter evaluations,...
parent 60a0277a
No related branches found
No related tags found
1 merge request!37Feature/language
#include <ASTNodeDataTypeFlattener.hpp>
#include <FunctionTable.hpp>
#include <SymbolTable.hpp>
#include <PEGGrammar.hpp>
ASTNodeDataTypeFlattener::ASTNodeDataTypeFlattener(ASTNode& node, FlattenedDataType& flattened_datatype)
{
if (node.is_type<language::expression_list>()) {
for (auto& child_node : node.children) {
ASTNodeDataTypeFlattener{*child_node, flattened_datatype};
}
} else if (node.is_type<language::function_evaluation>()) {
if (node.m_data_type != ASTNodeDataType::typename_t) {
flattened_datatype.push_back(node.m_data_type);
} else {
ASTNode& function_name_node = *node.children[0];
auto [i_function_symbol, found] = node.m_symbol_table->find(function_name_node.string(), node.begin());
Assert(found);
switch (i_function_symbol->attributes().dataType()) {
case ASTNodeDataType::function_t: {
uint64_t function_id = std::get<uint64_t>(i_function_symbol->attributes().value());
FunctionDescriptor& function_descriptor = node.m_symbol_table->functionTable()[function_id];
ASTNode& function_image_domain = *function_descriptor.domainMappingNode().children[1];
for (auto& image_sub_domain : function_image_domain.children) {
ASTNodeDataType data_type = ASTNodeDataType::undefined_t;
if (image_sub_domain->is_type<language::B_set>()) {
data_type = ASTNodeDataType::bool_t;
} else if (image_sub_domain->is_type<language::Z_set>()) {
data_type = ASTNodeDataType::int_t;
} else if (image_sub_domain->is_type<language::N_set>()) {
data_type = ASTNodeDataType::unsigned_int_t;
} else if (image_sub_domain->is_type<language::R_set>()) {
data_type = ASTNodeDataType::double_t;
} else if (image_sub_domain->is_type<language::string_type>()) {
data_type = ASTNodeDataType::string_t;
}
Assert(data_type != ASTNodeDataType::undefined_t);
flattened_datatype.push_back(data_type);
}
break;
}
// LCOV_EXCL_START
default: {
throw parse_error("unexpected function type", node.begin());
}
// LCOV_EXCL_STOP
}
}
} else {
flattened_datatype.push_back(node.m_data_type);
}
}
#ifndef AST_NODE_DATA_TYPE_FLATTENER_HPP
#define AST_NODE_DATA_TYPE_FLATTENER_HPP
#include <ASTNode.hpp>
#include <vector>
struct ASTNodeDataTypeFlattener
{
using FlattenedDataType = std::vector<ASTNodeDataType>;
ASTNodeDataTypeFlattener(ASTNode& node, FlattenedDataType& flattened_datatype);
};
#endif // AST_NODE_DATA_TYPE_FLATTENER_HPP
......@@ -3,7 +3,7 @@
#include <node_processor/AffectationProcessor.hpp>
#include <functional>
#include <ASTNodeDataTypeFlattener.hpp>
template <typename OperatorT>
void
......@@ -111,65 +111,8 @@ ASTNodeListAffectationExpressionBuilder::_buildListAffectationProcessor()
Assert(m_node.children[1]->is_type<language::expression_list>() or
m_node.children[1]->is_type<language::function_evaluation>());
using FlattenedRHSDataType = std::vector<ASTNodeDataType>;
std::function<void(ASTNode & rhs_node, FlattenedRHSDataType & flattened_rhs)> flatten_rhs =
[&](ASTNode& rhs_node, FlattenedRHSDataType& flattened_rhs) -> void {
if (rhs_node.is_type<language::expression_list>()) {
for (auto& rhs_child_node : rhs_node.children) {
flatten_rhs(*rhs_child_node, flattened_rhs);
}
} else if (rhs_node.is_type<language::function_evaluation>()) {
if (rhs_node.m_data_type != ASTNodeDataType::typename_t) {
flattened_rhs.push_back(rhs_node.m_data_type);
} else {
ASTNode& function_name_node = *rhs_node.children[0];
auto [i_function_symbol, found] = m_node.m_symbol_table->find(function_name_node.string(), m_node.begin());
Assert(found);
switch (i_function_symbol->attributes().dataType()) {
case ASTNodeDataType::function_t: {
uint64_t function_id = std::get<uint64_t>(i_function_symbol->attributes().value());
FunctionDescriptor& function_descriptor = m_node.m_symbol_table->functionTable()[function_id];
ASTNode& function_image_domain = *function_descriptor.domainMappingNode().children[1];
for (auto& image_sub_domain : function_image_domain.children) {
ASTNodeDataType data_type = ASTNodeDataType::undefined_t;
if (image_sub_domain->is_type<language::B_set>()) {
data_type = ASTNodeDataType::bool_t;
} else if (image_sub_domain->is_type<language::Z_set>()) {
data_type = ASTNodeDataType::int_t;
} else if (image_sub_domain->is_type<language::N_set>()) {
data_type = ASTNodeDataType::unsigned_int_t;
} else if (image_sub_domain->is_type<language::R_set>()) {
data_type = ASTNodeDataType::double_t;
} else if (image_sub_domain->is_type<language::string_type>()) {
data_type = ASTNodeDataType::string_t;
}
Assert(data_type != ASTNodeDataType::undefined_t);
flattened_rhs.push_back(data_type);
}
break;
}
// LCOV_EXCL_START
default: {
throw parse_error("unexpected function type", m_node.begin());
}
// LCOV_EXCL_STOP
}
}
} else {
flattened_rhs.push_back(rhs_node.m_data_type);
}
};
FlattenedRHSDataType flattened_rhs_data_type;
flatten_rhs(*m_node.children[1], flattened_rhs_data_type);
ASTNodeDataTypeFlattener::FlattenedDataType flattened_rhs_data_type;
ASTNodeDataTypeFlattener{*m_node.children[1], flattened_rhs_data_type};
ASTNode& name_list_node = *m_node.children[0];
......
......@@ -14,6 +14,7 @@ add_library(
ASTNodeBinaryOperatorExpressionBuilder.cpp
ASTNodeCFunctionExpressionBuilder.cpp
ASTNodeDataType.cpp
ASTNodeDataTypeFlattener.cpp
ASTNodeDataTypeBuilder.cpp
ASTNodeDataTypeChecker.cpp
ASTNodeDeclarationToAffectationConverter.cpp
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment