Goose  Artifact [45e5c97c04]

Artifact 45e5c97c042b6568f38b8a7f14e1b3506419a44f5de5641403e4d540c4bad938:

  • File bs/sema/substitute.cpp — part of check-in [f2ca82dfce] at 2019-08-16 23:52:52 on branch trunk — Simplified ir::Vector, use plain std::vector to store the terms. immer::vector was pointless for that since we pretty much never actually take advantage of them being CoW in this case. (user: achavasse size: 2327)

#include "sema.h"

namespace empathy::sema
{
    Term Substitute( const Term& src, const UnificationContext& context )
    {
        if( !holds_alternative< pvec >( src ) )
            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 );

        auto outputTerms = make_shared< Vector >();
        outputTerms->reserve( vec.terms().size() );

        for( auto&& t : vec.terms() )
            outputTerms->append( Substitute( t, context ) );

        return outputTerms;
    }

    Term SubstituteNamed( const Term& src, const UnificationContext& context )
    {
        if( !holds_alternative< pvec >( src ) )
            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 );

        auto outputTerms = make_shared< Vector >();
        outputTerms->reserve( vec.terms().size() );

        for( auto&& t : vec.terms() )
            outputTerms->append( SubstituteNamed( t, context ) );

        return outputTerms;
    }
}