Goose  Artifact [481f365550]

Artifact 481f365550c1edc6ad79e4a331937986a97a4b7e2f23d16227785fd57dddbe38:

  • File bs/builtins/types/func/invoke.cpp — part of check-in [25d0ee65d4] at 2019-03-31 20:42:15 on branch trunk — Higher order functions: template lambdas can be passed to template function parameters. (user: achavasse size: 3603)

#include "builtins/builtins.h"

using namespace empathy::sema;

namespace empathy::builtins
{
    class FunctionInvocationRule : public InvocationRule
    {
        public:
            virtual optional< Value > resolveInvocation( const Context& c, const Value& callee, const Value& args ) const final
            {
                UnificationContext uc( c );

                optional< UnificationContext > bestUC;
                optional< Term > bestSol;
                bool ambiguous = false;
                auto&& [sig, rtype] = GetFuncSigAndRType( callee );

                auto callPat = TVEC( args.val(), MkHole( "_"_sid ) );

                for( auto&& [s, uc] : Unify( sig, callPat, uc ) )
                {
                    if( uc.numUnknownValues() )
                        continue;

                    auto ssol = Substitute( s, uc );
                    if( !ssol )
                        continue;

                    if( !bestSol || uc.score() > bestUC->score() )
                    {
                        bestUC = uc;
                        bestSol = ssol;
                        ambiguous = false;
                        continue;
                    }

                    if( uc.score() < bestUC->score() )
                        continue;

                    ambiguous = true;
                }

                if( ambiguous )
                {
                    // TODO error mgmt
                    cout << "ambiguous function call.\n";
                    return nullopt;
                }

                if( !bestSol )
                {
                    // TODO error mgmt
                    cout << "function arguments mismatch.\n";
                    return nullopt;
                }

                return invoke( c, callee, *bestSol, *bestUC );
            }

            optional< Value > invoke( const Context& c, const Value& callee, const Term& unifiedCallPat, UnificationContext& uc ) const final
            {
                const auto& env = c.env();

                if( !IsBuiltinFunc( callee ) )
                    PerformLazyFuncParsing( env, callee );

                auto ppCallPat = Postprocess( unifiedCallPat, uc );
                if( !ppCallPat )
                    return nullopt;

                auto callDecomp = Decompose( *ppCallPat,
                    Vec(
                        SubTerm(),  // args
                        SubTerm()   // return type
                    )
                );

                auto&& [unifiedArgs, unifiedRType] = *callDecomp;
                return Value( unifiedRType, make_shared< llr::Element >( llr::Call( callee, unifiedArgs ) ) );
            }

            virtual optional< Term > getSignature( const Value& callee ) const final
            {
                return GetFuncSigAndRType( callee ).first;
            }

            virtual optional< Value > prepareFunc( const Context& c, const Value& callee, const Term& unifiedCallPat, UnificationContext& uc ) const final
            {
                const auto& env = c.env();

                if( !IsBuiltinFunc( callee ) )
                    PerformLazyFuncParsing( env, callee );

                return callee;
            }
    };

    void SetupFunctionInvocationRule( Env& e )
    {
        e.invocationRuleSet()->addRule(
            ValueToIRExpr( ValuePattern( ANYTERM( _ ),
                ValueToIRExpr( Value( TypeType(), TVEC( TSID( func ),
                ANYTERM( _ ), ANYTERM( _ ), ANYTERM( _ ) ) ) ),
                ANYTERM( _ ) ) ),
            make_shared< FunctionInvocationRule >() );
    }
}