Goose  build.cpp at [0cdc45df01]

File bs/builtins/types/template/build.cpp artifact d0b858f9ab part of check-in 0cdc45df01


#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 TFuncType( ValueToIRExpr( returnType ), TERM( make_shared< Vector >( vt.persistent(), false ) ) );
    }

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

        // Build the signature
        immer::vector< Term > v;
        auto vt = v.transient();

        bool success = true;

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

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

        if( !success )
            return nullopt;

        auto pToks = make_shared< vector< Term > >( move( body ) );
        return ToValue( TFunc( move( funcType ),
            TERM( make_shared< Vector >( vt.persistent(), false ) ),
            AppendToVectorTerm( c.identity(), TERM( id ) ),
            move( pToks ) ) );
    }
}