Goose  Artifact [a050f96efc]

Artifact a050f96efcc8a3b1847830ee2e413f317bc42215f9511d1efab894218eddfcec:

  • File bs/builtins/types/template/build.cpp — part of check-in [ee15f9081f] at 2019-03-28 21:00:54 on branch trunk — Higher order functions: plain lambdas can be passed to template function type parameters. (user: achavasse size: 3067)

#include "builtins/builtins.h"

namespace empathy::builtins
{
    TFuncType BuildTFuncType( const Value& returnType, const Value& params )
    {
        immer::vector< Term > v;
        auto vt = v.transient();

        ForEachInTuple( params, [&]( auto&& param )
        {
            vt.push_back( ValueToIRExpr( param ) );
            return true;
        } );

        return TFuncType( ValueToIRExpr( returnType ), TERM( make_shared< Vector >( vt.persistent(), false ) ) );
    }

    optional< Term > BuildTFuncSignature( const Context& c, const TFuncType& tft )
    {
        immer::vector< Term > v;
        auto vt = v.transient();

        bool success = true;
        ForEachInVectorTerm( tft.params(), [&]( auto&& param )
        {
            auto teSig = BuildTemplateSignature( c, param );
            if( !teSig )
            {
                cout << "Invalid template parameter.\n";
                success = false;
                return false;
            }

            vt.push_back( move( *teSig ) );
            return true;
        } );

        if( !success )
            return nullopt;

        auto rtSig = BuildTemplateSignature( c, tft.returnType() );
        if( !rtSig )
        {
            cout << "Invalid template return type or texpr.\n";
            return nullopt;
        }

        return TVEC( TERM( make_shared< Vector >( vt.persistent(), false ) ),
            *rtSig );
    }

    optional< Value > BuildTFunc( const Context& c, const StringId& id, const Value& returnType, const Value& params, vector< Term >&& body )
    {
        auto funcType = BuildTFuncType( returnType, params );

        auto sig = BuildTFuncSignature( c, funcType );
        if( !sig )
            return nullopt;

        auto pToks = make_shared< vector< Term > >( move( body ) );

        return ToValue( TFunc( move( funcType ),
            *sig,
            AppendToVectorTerm( c.identity(), TERM( id ) ),
            move( pToks ) ) );
    }

    optional< Term > BuildArgPatternFromTFuncType( const Context& c, const Value& tfuncType )
    {
        const auto& ftype = FromValue< TFuncType >( tfuncType );
        assert( ftype );

        immer::vector< Term > apv;
        auto apvt = apv.transient();

        bool success = true;
        ForEachInVectorTerm( ftype->params(), [&]( auto&& param )
        {
            auto teArgPat = BuildTemplateArgPattern( c, param );
            if( !teArgPat )
            {
                cout << "Invalid template parameter.\n";
                success = false;
                return false;
            }

            apvt.push_back( move( *teArgPat ) );
            return true;
        } );

        if( !success )
            return nullopt;

        auto rtArgPat = BuildTemplateArgPattern( c, ftype->returnType() );
        if( !rtArgPat )
        {
            cout << "Invalid template return type or texpr.\n";
            return nullopt;
        }

        return TVEC(
            TERM( make_shared< Vector >( apvt.persistent(), false ) ),
            *rtArgPat
        );
    }
}