Goose  Artifact [3ddc25266c]

Artifact 3ddc25266cb57ecee637bebd0c41dce2de6fa2da1939a6bb0555798faf9a51b0:

  • File bs/sema/substitute.cpp — part of check-in [c3f897359f] at 2020-06-20 14:32:02 on branch trunk —
    • Got rid of the gross system of performing unifications twice in all cases. It's only really needed when invoking template functions.
    • Since the above had the unexpected side effect of fixing the tuple Initialize() overloads not being called, fixed those (which were actually broken).
    (user: achavasse size: 969)

#include "sema.h"

namespace goose::sema
{
    Term Substitute( const Term& src, const UnificationContext& context )
    {
        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( !hole.name.isNumerical() )
                return src;

            const auto& optVal = context.getValue( hole.name.id() );
            if( !optVal )
                return HOLE( "_"_sid );

            return Substitute( *optVal, context );
        }

        if( !holds_alternative< pvec >( src ) )
            return src;

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