Goose  Artifact [cf56bf9d7f]

Artifact cf56bf9d7f1971b170ee01d891b0c9eda936ec684e4012e26eb89a11f050ce4e:

  • File bs/sema/postprocess.cpp — part of check-in [373a6ebd57] at 2022-09-02 21:42:42 on branch trunk —
    • Implemented a test for variadic functions
    • Fixed a million issues and missing things preventing the above from working
    • Implemented equality operators for types
    (user: zlodo size: 2449)

#include "sema.h"

namespace goose::sema
{
    Term WrapWithPostprocFunc( const Term& t, const ptr< PostProcFunc >& pp )
    {
        return SetWeight( VEC( TSID( postproc ),
            TERM( static_pointer_cast< void >( pp ) ),
            t ), GetWeight( t ) );
    }

    Term WrapWithPostprocFunc( const Term& t, PostProcFunc&& pp )
    {
        return SetWeight( VEC( TSID( postproc ),
            TERM( static_pointer_cast< void >( make_shared< PostProcFunc >( move( pp ) ) ) ),
            t ), GetWeight( t ) );
    }

    optional< pair< Term, ptr< PostProcFunc > > > UnwrapPostprocFunc( const Term& ppt )
    {
        auto result = Decompose( ppt,
            Vec(
                Lit( "postproc"_sid ),
                Val< ptr< void > >(),
                SubTerm()
            )
        );

        if( !result )
            return nullopt;

        auto&& [pPP, t] = *result;
        return make_pair( t, static_pointer_cast< PostProcFunc >( pPP ) );
    }

    optional< Term > Postprocess( const Term& src, TypeCheckingContext& tcc )
    {
        if( auto optHole = HoleFromIRExpr( src ) )
        {
            const auto& hole = *optHole;

            if( !hole.name().isNumerical() )
                    return src;
            else
            {
                const auto& optVal = tcc.getValue( hole.name().id() );
                if( !optVal )
                    return HOLE( "_"_sid );

                return Postprocess( *optVal, tcc );
            }
        }

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

        if( auto optPP = UnwrapPostprocFunc( src ) )
        {
            auto val = Postprocess( optPP->first, tcc );
            if( !val )
                return nullopt;

            return ( *optPP->second )( *val, tcc );
        }

        const auto& vec = *get< pvec >( src );

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

        for( auto&& t : vec.terms() )
        {
            auto newT = Postprocess( t, tcc );
            if( !newT )
                return nullopt;

            outputTerms->append( move( *newT ) );
        }

        if( auto rpt = vec.repetitionTerm() )
        {
            auto newRpt = Postprocess( *rpt, tcc );
            if( !newRpt )
                return nullopt;

            outputTerms->setRepetitionTerm( move( *newRpt ) );
        }

        return outputTerms;
    }
}