2018-03-31 16:58:13 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
/* ABSTRACT SYNTAX TREE - INLINE CODE******************************************/
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
#ifndef _H_EBCL_INLINE_SYNTAXTREE
|
|
|
|
#define _H_EBCL_INLINE_SYNTAXTREE
|
|
|
|
#include <ebcl/SyntaxTree.hh>
|
|
|
|
namespace ebcl {
|
|
|
|
|
|
|
|
|
|
|
|
/*= T_SyntaxTree =============================================================*/
|
|
|
|
|
2018-03-31 22:38:26 +02:00
|
|
|
template< typename T , typename L , size_t S >
|
|
|
|
T_SyntaxTree< T , L , S >::T_Node::T_Node(
|
2018-03-31 22:11:00 +02:00
|
|
|
T_Self const& tree ,
|
|
|
|
const uint32_t node ) noexcept
|
|
|
|
: tree{ tree } , node{ node }
|
|
|
|
{
|
|
|
|
// EMPTY
|
|
|
|
}
|
|
|
|
|
2018-03-31 22:38:26 +02:00
|
|
|
template< typename T , typename L , size_t S >
|
|
|
|
T_SyntaxTree< T , L , S >::T_Node::T_Node(
|
2018-03-31 22:11:00 +02:00
|
|
|
T_Node const& other ) noexcept
|
|
|
|
: tree{ other.tree } , node{ other.node }
|
|
|
|
{
|
|
|
|
// EMPTY
|
|
|
|
}
|
|
|
|
|
2018-03-31 22:38:26 +02:00
|
|
|
template< typename T , typename L , size_t S >
|
|
|
|
T_SyntaxTree< T , L , S >::T_Node::T_Node(
|
2018-03-31 22:11:00 +02:00
|
|
|
T_Node&& other ) noexcept
|
|
|
|
: T_Node( other )
|
|
|
|
{
|
|
|
|
// EMPTY
|
|
|
|
}
|
|
|
|
|
2018-03-31 22:38:26 +02:00
|
|
|
template< typename T , typename L , size_t S >
|
|
|
|
typename T_SyntaxTree< T , L , S >::T_Node& T_SyntaxTree< T , L , S >::T_Node::operator =(
|
|
|
|
T_SyntaxTree< T , L , S >::T_Node const& other ) noexcept
|
2018-03-31 22:11:00 +02:00
|
|
|
{
|
|
|
|
assert( &other.tree == &tree );
|
|
|
|
node = other.node;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2018-03-31 22:38:26 +02:00
|
|
|
template< typename T , typename L , size_t S >
|
|
|
|
typename T_SyntaxTree< T , L , S >::T_Node& T_SyntaxTree< T , L , S >::T_Node::operator =(
|
|
|
|
T_SyntaxTree< T , L , S >::T_Node&& other ) noexcept
|
2018-03-31 22:11:00 +02:00
|
|
|
{
|
|
|
|
assert( &other.tree == &tree );
|
|
|
|
node = other.node;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
2018-03-31 22:38:26 +02:00
|
|
|
template< typename T , typename L , size_t S >
|
|
|
|
T_SyntaxTree< T , L , S >::T_Node_::T_Node_(
|
2018-03-31 16:58:13 +02:00
|
|
|
const T nodeType ,
|
|
|
|
const uint32_t parent ) noexcept
|
2018-03-31 22:38:26 +02:00
|
|
|
: type{ nodeType } , parent{ parent } , location{ }
|
2018-03-31 16:58:13 +02:00
|
|
|
{
|
|
|
|
// EMPTY
|
|
|
|
}
|
|
|
|
|
2018-03-31 22:38:26 +02:00
|
|
|
template< typename T , typename L , size_t S >
|
|
|
|
T_SyntaxTree< T , L , S >::T_Node_::T_Node_(
|
2018-03-31 16:58:13 +02:00
|
|
|
T_Node_&& other ) noexcept
|
|
|
|
: type{ other.type } , parent{ other.parent } ,
|
2018-03-31 22:38:26 +02:00
|
|
|
location{ other.location } ,
|
2018-03-31 16:58:13 +02:00
|
|
|
children{ std::move( other.children ) } ,
|
|
|
|
data{ std::move( other.data ) }
|
|
|
|
{
|
|
|
|
// EMPTY
|
|
|
|
}
|
|
|
|
|
2018-03-31 22:38:26 +02:00
|
|
|
template< typename T , typename L , size_t S >
|
|
|
|
typename T_SyntaxTree< T , L , S >::T_Node_& T_SyntaxTree< T , L , S >::T_Node_::operator =(
|
2018-03-31 16:58:13 +02:00
|
|
|
T_Node_&& other ) noexcept
|
|
|
|
{
|
|
|
|
type = other.type;
|
|
|
|
parent = other.parent;
|
2018-03-31 22:38:26 +02:00
|
|
|
location = other.location;
|
2018-03-31 16:58:13 +02:00
|
|
|
children = std::move( other.children );
|
|
|
|
data = std::move( other.data );
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
2018-03-31 22:38:26 +02:00
|
|
|
template< typename T , typename L , size_t S >
|
|
|
|
T_SyntaxTree< T , L , S >::T_SyntaxTree(
|
2018-03-31 16:58:13 +02:00
|
|
|
const T rootType ) noexcept
|
|
|
|
: nodes_( 64 )
|
|
|
|
{
|
|
|
|
nodes_.addNew( rootType , 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
2018-03-31 22:38:26 +02:00
|
|
|
template< typename T , typename L , size_t S >
|
|
|
|
T_SyntaxTree< T , L , S >::T_SyntaxTree(
|
|
|
|
T_SyntaxTree< T , L , S >&& other ) noexcept
|
2018-03-31 16:58:13 +02:00
|
|
|
: nodes_{ std::move( other.nodes_ ) }
|
|
|
|
{
|
|
|
|
// EMPTY
|
|
|
|
}
|
|
|
|
|
2018-03-31 22:38:26 +02:00
|
|
|
template< typename T , typename L , size_t S >
|
|
|
|
T_SyntaxTree< T , L , S >& T_SyntaxTree< T , L , S >::operator =(
|
|
|
|
T_SyntaxTree< T , L , S >&& other ) noexcept
|
2018-03-31 16:58:13 +02:00
|
|
|
{
|
|
|
|
nodes_ = std::move( other.nodes_ );
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
2018-03-31 22:38:26 +02:00
|
|
|
template< typename T , typename L , size_t S >
|
|
|
|
T_SyntaxTree< T , L , S >& T_SyntaxTree< T , L , S >::swap(
|
|
|
|
T_SyntaxTree< T , L , S >& other ) noexcept
|
2018-03-31 16:58:13 +02:00
|
|
|
{
|
|
|
|
using std::swap;
|
|
|
|
swap( nodes_ , other.nodes_ );
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2018-03-31 22:38:26 +02:00
|
|
|
template< typename T , typename L , size_t S >
|
|
|
|
void swap( T_SyntaxTree< T , L , S >& lhs ,
|
|
|
|
T_SyntaxTree< T , L , S >& rhs ) noexcept
|
2018-03-31 16:58:13 +02:00
|
|
|
{
|
|
|
|
lhs.swap( rhs );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
2018-03-31 22:38:26 +02:00
|
|
|
template< typename T , typename L , size_t S >
|
|
|
|
T T_SyntaxTree< T , L , S >::typeOf(
|
2018-03-31 16:58:13 +02:00
|
|
|
const uint32_t node ) const noexcept
|
|
|
|
{
|
|
|
|
return nodes_[ node ].type;
|
|
|
|
}
|
|
|
|
|
2018-03-31 22:38:26 +02:00
|
|
|
template< typename T , typename L , size_t S >
|
|
|
|
uint32_t T_SyntaxTree< T , L , S >::parentOf(
|
2018-03-31 16:58:13 +02:00
|
|
|
const uint32_t node ) const noexcept
|
|
|
|
{
|
|
|
|
return nodes_[ node ].parent;
|
|
|
|
}
|
|
|
|
|
2018-03-31 22:38:26 +02:00
|
|
|
template< typename T , typename L , size_t S >
|
|
|
|
T_AutoArray< uint32_t , 8 > const& T_SyntaxTree< T , L , S >::childrenOf(
|
2018-03-31 16:58:13 +02:00
|
|
|
const uint32_t node ) const noexcept
|
|
|
|
{
|
|
|
|
return nodes_[ node ].children;
|
|
|
|
}
|
|
|
|
|
2018-03-31 22:38:26 +02:00
|
|
|
template< typename T , typename L , size_t S >
|
|
|
|
bool T_SyntaxTree< T , L , S >::hasData(
|
2018-03-31 16:58:13 +02:00
|
|
|
const uint32_t node ) const noexcept
|
|
|
|
{
|
|
|
|
return bool( nodes_[ node ].data );
|
|
|
|
}
|
|
|
|
|
2018-03-31 22:38:26 +02:00
|
|
|
template< typename T , typename L , size_t S >
|
2018-03-31 16:58:13 +02:00
|
|
|
template< typename D >
|
2018-03-31 22:38:26 +02:00
|
|
|
D const& T_SyntaxTree< T , L , S >::dataOf(
|
2018-03-31 16:58:13 +02:00
|
|
|
const uint32_t node ) const
|
|
|
|
{
|
|
|
|
return nodes_[ node ].data.template value< D >( );
|
|
|
|
}
|
|
|
|
|
2018-03-31 22:38:26 +02:00
|
|
|
template< typename T , typename L , size_t S >
|
|
|
|
L const& T_SyntaxTree< T , L , S >::locationOf(
|
|
|
|
const uint32_t node ) const noexcept
|
|
|
|
{
|
|
|
|
return nodes_[ node ].location;
|
|
|
|
}
|
|
|
|
|
2018-03-31 16:58:13 +02:00
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
2018-03-31 22:38:26 +02:00
|
|
|
template< typename T , typename L , size_t S >
|
|
|
|
uint32_t T_SyntaxTree< T , L , S >::addChild(
|
2018-03-31 16:58:13 +02:00
|
|
|
const uint32_t parent ,
|
|
|
|
const E_Node type ) noexcept
|
|
|
|
{
|
|
|
|
T_Node_ node{ type , parent };
|
|
|
|
const auto idx{ nodes_.add( std::move( node ) ) };
|
|
|
|
assert( idx != parent );
|
|
|
|
nodes_[ parent ].children.add( idx );
|
|
|
|
return idx;
|
|
|
|
}
|
|
|
|
|
2018-03-31 22:38:26 +02:00
|
|
|
template< typename T , typename L , size_t S >
|
2018-03-31 16:58:13 +02:00
|
|
|
template< typename D , typename ... ArgTypes >
|
2018-03-31 22:38:26 +02:00
|
|
|
D& T_SyntaxTree< T , L , S >::newData(
|
2018-03-31 16:58:13 +02:00
|
|
|
const uint32_t node ,
|
|
|
|
ArgTypes&& ... args )
|
|
|
|
{
|
|
|
|
auto& data{ nodes_[ node ].data };
|
|
|
|
data.template setNew< D >( std::forward< ArgTypes >( args ) ... );
|
|
|
|
return data.template value< D >( );
|
|
|
|
}
|
|
|
|
|
2018-03-31 22:38:26 +02:00
|
|
|
template< typename T , typename L , size_t S >
|
2018-03-31 16:58:13 +02:00
|
|
|
template< typename D >
|
2018-03-31 22:38:26 +02:00
|
|
|
D& T_SyntaxTree< T , L , S >::dataOf(
|
2018-03-31 16:58:13 +02:00
|
|
|
const uint32_t node )
|
|
|
|
{
|
|
|
|
return nodes_[ node ].data.template value< D >( );
|
|
|
|
}
|
|
|
|
|
2018-03-31 22:38:26 +02:00
|
|
|
template< typename T , typename L , size_t S >
|
|
|
|
L& T_SyntaxTree< T , L , S >::locationOf(
|
|
|
|
const uint32_t node ) noexcept
|
|
|
|
{
|
|
|
|
return nodes_[ node ].location;
|
|
|
|
}
|
|
|
|
|
2018-03-31 22:11:00 +02:00
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
2018-03-31 22:38:26 +02:00
|
|
|
template< typename T , typename L , size_t S >
|
|
|
|
typename T_SyntaxTree< T , L , S >::T_Visitor T_SyntaxTree< T , L , S >::visitor( ) const noexcept
|
2018-03-31 22:11:00 +02:00
|
|
|
{
|
|
|
|
return T_Visitor{
|
|
|
|
[this]( T_Node node , uint32_t child ) -> T_Optional< T_Node > {
|
|
|
|
auto const& n{ nodes_[ node.node ] };
|
|
|
|
if ( child >= n.children.size( ) ) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
return T_Node{ node.tree , n.children[ child ] };
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-03-31 22:38:26 +02:00
|
|
|
template< typename T , typename L , size_t S >
|
|
|
|
typename T_SyntaxTree< T , L , S >::T_Node T_SyntaxTree< T , L , S >::root( ) const noexcept
|
2018-03-31 22:11:00 +02:00
|
|
|
{
|
|
|
|
return T_Node{ *this , 0 };
|
|
|
|
}
|
|
|
|
|
2018-03-31 16:58:13 +02:00
|
|
|
|
|
|
|
} // namespace
|
|
|
|
#endif // _H_EBCL_INLINE_SYNTAXTREE
|