Goose  Artifact [d0b858f9ab]

Artifact d0b858f9ab9980df74a4b595658ce25cfe96a02c62801a74006f74b92c5e0405:

  • File bs/builtins/types/template/build.cpp — part of check-in [0cdc45df01] at 2019-02-27 22:05:38 on branch trunk — Templates:
    • Fixed the incorrect context passed to the template rule setup function, which caused TVars to create their bindings in the wrong place.
    • Added missing setup functions for decl template rules.
    (user: achavasse size: 1503)

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