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

Prepare function evaluation mechanisms

- create a global function table, which will contain functions described by
  their domains mapping and their definition

- this table will be directly accessed by the id of the function which will be
  associated to the function variable. These ids will be stored as uint64_t (for
  the sake of simplicity) in the ASTNodeDataVariant, but use of a FunctionId
  type could be considered (cleaner approach) ...
parent 5d57361b
No related branches found
No related tags found
1 merge request!37Feature/language
...@@ -79,7 +79,8 @@ ASTSymbolTableBuilder::buildSymbolTable(ASTNode& n, std::shared_ptr<SymbolTable> ...@@ -79,7 +79,8 @@ ASTSymbolTableBuilder::buildSymbolTable(ASTNode& n, std::shared_ptr<SymbolTable>
ASTSymbolTableBuilder::ASTSymbolTableBuilder(ASTNode& node) ASTSymbolTableBuilder::ASTSymbolTableBuilder(ASTNode& node)
{ {
Assert(node.is_root()); Assert(node.is_root());
std::shared_ptr symbol_table = std::make_shared<SymbolTable>(); std::shared_ptr function_table = std::make_shared<FunctionTable>();
std::shared_ptr symbol_table = std::make_shared<SymbolTable>(function_table);
node.m_symbol_table = symbol_table; node.m_symbol_table = symbol_table;
......
#ifndef FUNCTION_TABLE_HPP
#define FUNCTION_TABLE_HPP
#include <ASTNodeDataType.hpp>
#include <ASTNodeDataVariant.hpp>
#include <ASTNode.hpp>
#include <PugsAssert.hpp>
#include <pegtl/position.hpp>
#include <iostream>
class FunctionTable
{
class FunctionDescriptor
{
std::unique_ptr<ASTNode> m_domain_mapping_node;
std::unique_ptr<ASTNode> m_expression_node;
public:
FunctionDescriptor& operator=(const FunctionDescriptor&) = default;
FunctionDescriptor& operator=(FunctionDescriptor&&) = default;
FunctionDescriptor(FunctionDescriptor&&) = default;
FunctionDescriptor() = default;
~FunctionDescriptor() = default;
};
private:
std::vector<FunctionDescriptor> m_function_descriptor_list;
public:
PUGS_INLINE
FunctionDescriptor& operator[](size_t function_id)
{
Assert(function_id < m_function_descriptor_list.size());
return m_function_descriptor_list[function_id];
}
PUGS_INLINE
const FunctionDescriptor& operator[](size_t function_id) const
{
Assert(function_id < m_function_descriptor_list.size());
return m_function_descriptor_list[function_id];
}
size_t
add(FunctionDescriptor&& function_descriptor)
{
m_function_descriptor_list.emplace_back(std::move(function_descriptor));
return m_function_descriptor_list.size() - 1;
}
FunctionTable() = default;
~FunctionTable() = default;
};
#endif // FUNCTION_TABLE_HPP
...@@ -10,6 +10,8 @@ ...@@ -10,6 +10,8 @@
#include <iostream> #include <iostream>
#include <FunctionTable.hpp>
class SymbolTable class SymbolTable
{ {
public: public:
...@@ -129,6 +131,7 @@ class SymbolTable ...@@ -129,6 +131,7 @@ class SymbolTable
private: private:
std::vector<Symbol> m_symbol_list; std::vector<Symbol> m_symbol_list;
std::shared_ptr<SymbolTable> m_parent_table; std::shared_ptr<SymbolTable> m_parent_table;
std::shared_ptr<FunctionTable> m_function_table;
public: public:
friend std::ostream& friend std::ostream&
...@@ -177,7 +180,14 @@ class SymbolTable ...@@ -177,7 +180,14 @@ class SymbolTable
true); true);
} }
SymbolTable(const std::shared_ptr<SymbolTable>& parent_table = nullptr) : m_parent_table(parent_table) SymbolTable(const std::shared_ptr<SymbolTable>& parent_table)
: m_parent_table(parent_table), m_function_table(parent_table->m_function_table)
{
;
}
SymbolTable(const std::shared_ptr<FunctionTable>& function_table)
: m_parent_table(nullptr), m_function_table(function_table)
{ {
; ;
} }
......
...@@ -9,7 +9,8 @@ TEST_CASE("SymbolTable", "[language]") ...@@ -9,7 +9,8 @@ TEST_CASE("SymbolTable", "[language]")
{ {
SECTION("Simple Symbol Table") SECTION("Simple Symbol Table")
{ {
std::shared_ptr root_st = std::make_shared<SymbolTable>(); std::shared_ptr function_table = std::make_shared<FunctionTable>();
std::shared_ptr root_st = std::make_shared<SymbolTable>(function_table);
using namespace TAO_PEGTL_NAMESPACE; using namespace TAO_PEGTL_NAMESPACE;
position begin_position{internal::iterator{"fixture"}, "fixture"}; position begin_position{internal::iterator{"fixture"}, "fixture"};
...@@ -97,7 +98,8 @@ TEST_CASE("SymbolTable", "[language]") ...@@ -97,7 +98,8 @@ TEST_CASE("SymbolTable", "[language]")
SECTION("Hierarchy Symbol Table") SECTION("Hierarchy Symbol Table")
{ {
std::shared_ptr root_st = std::make_shared<SymbolTable>(); std::shared_ptr function_table = std::make_shared<FunctionTable>();
std::shared_ptr root_st = std::make_shared<SymbolTable>(function_table);
using namespace TAO_PEGTL_NAMESPACE; using namespace TAO_PEGTL_NAMESPACE;
position begin_position{internal::iterator{"fixture"}, "fixture"}; position begin_position{internal::iterator{"fixture"}, "fixture"};
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment