Goose  Artifact [c179d0d5b3]

Artifact c179d0d5b39af486a5a3bda60e9c97016620870042517130ed52b00820b86719:

  • File bs/sema/substitute.cpp — part of check-in [121c78d7e5] at 2019-04-01 20:37:04 on branch trunk — sema: implemented SubstituteNamed. (user: achavasse size: 2815)

#include "sema.h"

namespace empathy::sema
{
    optional< 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 src;

            return Substitute( *optVal, context );
        }

        if( auto optCB = UnwrapCallback( src ) )
        {
            auto val = Substitute( optCB->first, context );
            if( !val )
                return nullopt;

            return ( *optCB->second )( *val );
        }

        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 );
            if( !newT )
                return nullopt;

            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;

            // We only substitute named holes. If we encounter an indexed hole,
            // output it as is.
            if( !holds_alternative< StringId >( hole ) )
                return src;

            // 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 );
        }

        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 ) );
    }
}