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

Continue C-function expression management

- add a ASTNodeDataType::c_function_t
- define switch to discriminate function expression evaluation and C-function
  expression evaluation
parent 6e4b1e77
No related branches found
No related tags found
1 merge request!37Feature/language
Showing with 132 additions and 36 deletions
...@@ -17,7 +17,20 @@ ASTModulesImporter::_importModule(ASTNode& import_node) ...@@ -17,7 +17,20 @@ ASTModulesImporter::_importModule(ASTNode& import_node)
std::cout << " * importing '" << rang::fgB::green << module_name << rang::style::reset << "' module\n"; std::cout << " * importing '" << rang::fgB::green << module_name << rang::style::reset << "' module\n";
if (module_name == "math") { if (module_name == "math") {
std::cout << rang::fgB::red << "importing math module NIY!" << rang::style::reset << '\n'; std::string symbol_name{"sin"};
auto [i_symbol, success] = m_symbol_table.add(symbol_name, import_node.begin());
if (not success) {
std::ostringstream error_message;
error_message << "cannot add symbol '" << symbol_name << "' it is already defined";
throw parse_error(error_message.str(), import_node.begin());
}
i_symbol->attributes().setDataType(ASTNodeDataType::c_function_t);
i_symbol->attributes().setIsInitialized();
i_symbol->attributes().value() = static_cast<uint64_t>(0);
} else { } else {
throw parse_error(std::string{"could not find module "} + module_name, std::vector{module_name_node.begin()}); throw parse_error(std::string{"could not find module "} + module_name, std::vector{module_name_node.begin()});
} }
...@@ -35,7 +48,7 @@ ASTModulesImporter::_importAllModules(ASTNode& node) ...@@ -35,7 +48,7 @@ ASTModulesImporter::_importAllModules(ASTNode& node)
} }
} }
ASTModulesImporter::ASTModulesImporter(ASTNode& root_node) ASTModulesImporter::ASTModulesImporter(ASTNode& root_node) : m_symbol_table{*root_node.m_symbol_table}
{ {
Assert(root_node.is_root()); Assert(root_node.is_root());
this->_importAllModules(root_node); this->_importAllModules(root_node);
......
...@@ -3,8 +3,12 @@ ...@@ -3,8 +3,12 @@
#include <ASTNode.hpp> #include <ASTNode.hpp>
class SymbolTable;
class ASTModulesImporter class ASTModulesImporter
{ {
SymbolTable& m_symbol_table;
void _importModule(ASTNode& import_node); void _importModule(ASTNode& import_node);
void _importAllModules(ASTNode& node); void _importAllModules(ASTNode& node);
......
#include <ASTNodeCFunctionExpressionBuilder.hpp>
#include <PEGGrammar.hpp>
#include <FunctionTable.hpp>
#include <SymbolTable.hpp>
ASTNodeCFunctionExpressionBuilder::ASTNodeCFunctionExpressionBuilder(ASTNode& node)
{
auto [i_function_symbol, found] = node.m_symbol_table->find(node.children[0]->string(), node.begin());
Assert(found);
Assert(i_function_symbol->attributes().dataType() == ASTNodeDataType::c_function_t);
// uint64_t function_id = std::get<uint64_t>(i_function_symbol->attributes().value());
throw parse_error("CFunction processor not implemented yet", node.begin());
}
#ifndef AST_NODE_C_FUNCTION_EXPRESSION_BUILDER_HPP
#define AST_NODE_C_FUNCTION_EXPRESSION_BUILDER_HPP
#include <ASTNode.hpp>
#include <node_processor/INodeProcessor.hpp>
class ASTNodeCFunctionExpressionBuilder
{
public:
ASTNodeCFunctionExpressionBuilder(ASTNode& node);
};
#endif // AST_NODE_C_FUNCTION_EXPRESSION_BUILDER_HPP
...@@ -29,6 +29,9 @@ dataTypeName(const ASTNodeDataType& data_type) ...@@ -29,6 +29,9 @@ dataTypeName(const ASTNodeDataType& data_type)
case ASTNodeDataType::function_t: case ASTNodeDataType::function_t:
name = "function"; name = "function";
break; break;
case ASTNodeDataType::c_function_t:
name = "c_function";
break;
case ASTNodeDataType::void_t: case ASTNodeDataType::void_t:
name = "void"; name = "void";
break; break;
......
...@@ -14,6 +14,7 @@ enum class ASTNodeDataType : int32_t ...@@ -14,6 +14,7 @@ enum class ASTNodeDataType : int32_t
string_t = 5, string_t = 5,
typename_t = 10, typename_t = 10,
function_t = 11, function_t = 11,
c_function_t = 12,
void_t = std::numeric_limits<int32_t>::max() void_t = std::numeric_limits<int32_t>::max()
}; };
......
...@@ -219,13 +219,7 @@ ASTNodeDataTypeBuilder::_buildNodeDataTypes(ASTNode& n) ...@@ -219,13 +219,7 @@ ASTNodeDataTypeBuilder::_buildNodeDataTypes(ASTNode& n)
throw parse_error(message.str(), n.begin()); throw parse_error(message.str(), n.begin());
} }
} else if (n.is<language::function_evaluation>()) { } else if (n.is<language::function_evaluation>()) {
if (n.children[0]->m_data_type != ASTNodeDataType::function_t) { if (n.children[0]->m_data_type == ASTNodeDataType::function_t) {
std::ostringstream message;
message << "invalid function call\n"
<< "note: '" << n.children[0]->string() << "' is not a function!" << std::ends;
throw parse_error(message.str(), n.begin());
}
const std::string& function_name = n.children[0]->string(); const std::string& function_name = n.children[0]->string();
auto [i_function_symbol, success] = n.m_symbol_table->find(function_name, n.children[0]->begin()); auto [i_function_symbol, success] = n.m_symbol_table->find(function_name, n.children[0]->begin());
...@@ -259,6 +253,17 @@ ASTNodeDataTypeBuilder::_buildNodeDataTypes(ASTNode& n) ...@@ -259,6 +253,17 @@ ASTNodeDataTypeBuilder::_buildNodeDataTypes(ASTNode& n)
Assert(data_type != ASTNodeDataType::undefined_t); // LCOV_EXCL_LINE Assert(data_type != ASTNodeDataType::undefined_t); // LCOV_EXCL_LINE
n.m_data_type = data_type; n.m_data_type = data_type;
} else if (n.children[0]->m_data_type == ASTNodeDataType::c_function_t) {
std::cout << __FILE__ << ':' << __LINE__ << ": " << rang::fgB::red << "datatype is incorrectly defined!"
<< rang::style::reset << '\n';
n.m_data_type = ASTNodeDataType::double_t;
} else {
std::ostringstream message;
message << "invalid function call\n"
<< "note: '" << n.children[0]->string() << "' (type: " << dataTypeName(n.children[0]->m_data_type)
<< ") is not a function!" << std::ends;
throw parse_error(message.str(), n.begin());
}
} else if (n.is<language::B_set>() or n.is<language::Z_set>() or n.is<language::N_set>() or } else if (n.is<language::B_set>() or n.is<language::Z_set>() or n.is<language::N_set>() or
n.is<language::R_set>() or n.is<language::string_type>()) { n.is<language::R_set>() or n.is<language::string_type>()) {
n.m_data_type = ASTNodeDataType::typename_t; n.m_data_type = ASTNodeDataType::typename_t;
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
#include <ASTNodeAffectationExpressionBuilder.hpp> #include <ASTNodeAffectationExpressionBuilder.hpp>
#include <ASTNodeBinaryOperatorExpressionBuilder.hpp> #include <ASTNodeBinaryOperatorExpressionBuilder.hpp>
#include <ASTNodeFunctionExpressionBuilder.hpp> #include <ASTNodeFunctionEvaluationExpressionBuilder.hpp>
#include <ASTNodeIncDecExpressionBuilder.hpp> #include <ASTNodeIncDecExpressionBuilder.hpp>
#include <ASTNodeUnaryOperatorExpressionBuilder.hpp> #include <ASTNodeUnaryOperatorExpressionBuilder.hpp>
...@@ -32,7 +32,7 @@ ASTNodeExpressionBuilder::_buildExpression(ASTNode& n) ...@@ -32,7 +32,7 @@ ASTNodeExpressionBuilder::_buildExpression(ASTNode& n)
n.m_node_processor = std::make_unique<FakeProcessor>(); n.m_node_processor = std::make_unique<FakeProcessor>();
} else if (n.is<language::function_evaluation>()) { } else if (n.is<language::function_evaluation>()) {
ASTNodeFunctionExpressionBuilder{n}; ASTNodeFunctionEvaluationExpressionBuilder{n};
} else if (n.is<language::real>()) { } else if (n.is<language::real>()) {
n.m_node_processor = std::make_unique<FakeProcessor>(); n.m_node_processor = std::make_unique<FakeProcessor>();
......
#include <ASTNodeFunctionEvaluationExpressionBuilder.hpp>
#include <ASTNodeCFunctionExpressionBuilder.hpp>
#include <ASTNodeFunctionExpressionBuilder.hpp>
#include <SymbolTable.hpp>
ASTNodeFunctionEvaluationExpressionBuilder::ASTNodeFunctionEvaluationExpressionBuilder(ASTNode& node)
{
auto [i_function_symbol, found] = node.m_symbol_table->find(node.children[0]->string(), node.begin());
Assert(found);
switch (i_function_symbol->attributes().dataType()) {
case ASTNodeDataType::function_t: {
ASTNodeFunctionExpressionBuilder{node};
break;
}
case ASTNodeDataType::c_function_t: {
ASTNodeCFunctionExpressionBuilder{node};
break;
}
default: {
throw parse_error("unexpected function type", node.begin());
}
}
}
#ifndef AST_NODE_FUNCTION_EVALUATION_EXPRESSION_BUILDER_HPP
#define AST_NODE_FUNCTION_EVALUATION_EXPRESSION_BUILDER_HPP
#include <ASTNode.hpp>
struct ASTNodeFunctionEvaluationExpressionBuilder
{
ASTNodeFunctionEvaluationExpressionBuilder(ASTNode& node);
};
#endif // AST_NODE_FUNCTION_EVALUATION_EXPRESSION_BUILDER_HPP
...@@ -158,6 +158,8 @@ ASTNodeFunctionExpressionBuilder::ASTNodeFunctionExpressionBuilder(ASTNode& node ...@@ -158,6 +158,8 @@ ASTNodeFunctionExpressionBuilder::ASTNodeFunctionExpressionBuilder(ASTNode& node
{ {
auto [i_function_symbol, found] = node.m_symbol_table->find(node.children[0]->string(), node.begin()); auto [i_function_symbol, found] = node.m_symbol_table->find(node.children[0]->string(), node.begin());
Assert(found); Assert(found);
Assert(i_function_symbol->attributes().dataType() == ASTNodeDataType::function_t);
uint64_t function_id = std::get<uint64_t>(i_function_symbol->attributes().value()); uint64_t function_id = std::get<uint64_t>(i_function_symbol->attributes().value());
FunctionDescriptor& function_descriptor = node.m_symbol_table->functionTable()[function_id]; FunctionDescriptor& function_descriptor = node.m_symbol_table->functionTable()[function_id];
......
...@@ -12,6 +12,7 @@ add_library( ...@@ -12,6 +12,7 @@ add_library(
ASTModulesImporter.cpp ASTModulesImporter.cpp
ASTNodeAffectationExpressionBuilder.cpp ASTNodeAffectationExpressionBuilder.cpp
ASTNodeBinaryOperatorExpressionBuilder.cpp ASTNodeBinaryOperatorExpressionBuilder.cpp
ASTNodeCFunctionExpressionBuilder.cpp
ASTNodeDataType.cpp ASTNodeDataType.cpp
ASTNodeDataTypeBuilder.cpp ASTNodeDataTypeBuilder.cpp
ASTNodeDataTypeChecker.cpp ASTNodeDataTypeChecker.cpp
...@@ -19,6 +20,7 @@ add_library( ...@@ -19,6 +20,7 @@ add_library(
ASTNodeEmptyBlockCleaner.cpp ASTNodeEmptyBlockCleaner.cpp
ASTNodeExpressionBuilder.cpp ASTNodeExpressionBuilder.cpp
ASTNodeFunctionExpressionBuilder.cpp ASTNodeFunctionExpressionBuilder.cpp
ASTNodeFunctionEvaluationExpressionBuilder.cpp
ASTNodeIncDecExpressionBuilder.cpp ASTNodeIncDecExpressionBuilder.cpp
ASTNodeJumpPlacementChecker.cpp ASTNodeJumpPlacementChecker.cpp
ASTNodeUnaryOperatorExpressionBuilder.cpp ASTNodeUnaryOperatorExpressionBuilder.cpp
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment