#include "sema.h"
namespace empathy::sema
{
Term Substitute( const Term& src, const UnificationContext& context )
{
if( !holds_alternative< pvec >( src.content() ) )
return src;
if( auto optHole = HoleFromIRExpr( src ) )
{
const auto& hole = *optHole;
// We only substitute indexed holes. If we encounter a named hole,
// output it as is.
if( !holds_alternative< uint32_t >( hole ) )
return src;
const auto& optVal = context.getValue( get< uint32_t >( hole ) );
if( !optVal )
return MkHole( "_"_sid );
return Substitute( *optVal, context );
}
const auto& vec = *get< pvec >( src.content() );
immer::vector< Term > outputTerms;
auto vt = outputTerms.transient();
bool faulty = false;
for( auto&& t : vec.terms() )
{
auto newT = Substitute( t, context );
faulty = faulty || newT.isFaulty();
vt.push_back( move( newT ) );
}
return Term( src.location(), make_shared< Vector >( vt.persistent(), faulty ) );
}
Term SubstituteNamed( const Term& src, const UnificationContext& context )
{
if( !holds_alternative< pvec >( src.content() ) )
return src;
if( auto optHole = HoleFromIRExpr( src ) )
{
const auto& hole = *optHole;
if( holds_alternative< StringId >( hole ) )
{
// If the name is not found, output it as is.
auto index = context.getLHSHoleIndex( get< StringId >( hole ) );
if( index == UnificationContext::InvalidIndex )
return src;
const auto& val = context.getValue( index );
if( !val )
return src;
return SubstituteNamed( *val, context );
}
else
{
const auto& optVal = context.getValue( get< uint32_t >( hole ) );
if( !optVal )
return MkHole( "_"_sid );
return SubstituteNamed( *optVal, context );
}
}
const auto& vec = *get< pvec >( src.content() );
immer::vector< Term > outputTerms;
auto vt = outputTerms.transient();
bool faulty = false;
for( auto&& t : vec.terms() )
{
auto newT = SubstituteNamed( t, context );
faulty = faulty || newT.isFaulty();
vt.push_back( move( newT ) );
}
return Term( src.location(), make_shared< Vector >( vt.persistent(), faulty ) );
}
}