#include "builtins/builtins.h"
using namespace goose::builtins;
namespace goose::builtins
{
bool IsTDecl( const Value& td )
{
return td.type() == GetValueType< TDecl >();
}
Term TDeclSigPattern( const Term& name, Hole::Behavior bhv, Term&& sig )
{
if( bhv == Hole::Behavior::Any )
return VEC( TSID( tdecl_sig ), name, ANYTERM( _ ), move( sig ) );
else
return VEC( TSID( tdecl_sig ), name, TERM( HoleBhvToSid( bhv ) ), move( sig ) );
}
tuple< StringId, Hole::Behavior, Term > DecomposeTDSig( const Term& tdsig )
{
auto result = Decompose( tdsig,
Vec(
Lit( "tdecl_sig"_sid ),
Val< StringId >(),
SubTerm(),
SubTerm()
)
);
auto&& [name,bhvTerm,sig] = *result;
if( bhvTerm == ANYTERM( _ ) )
return { name, Hole::Behavior::Any, sig };
return { name, SidToHoleBhv( get< StringId >( bhvTerm ) ), sig };
}
}
namespace goose::eir
{
const Term& Bridge< TDecl >::Type()
{
static auto type = ValueToEIR( Value( TypeType(), VEC( TSID( texpr ), TSID( tdecl ) ) ) );
return type;
}
Value Bridge< TDecl >::ToValue( const TDecl& td )
{
return Value( Type(), VEC( td.type(), TERM( td.name() ), TERM( HoleBhvToSid( td.behavior() ) ) ) );
}
optional< TDecl > Bridge< TDecl >::FromValue( const Value& v )
{
if( !IsTDecl( v ) )
return nullopt;
auto result = Decompose( v.val(),
Vec(
SubTerm(),
Val< StringId >(),
Val< StringId >()
)
);
if( !result )
return nullopt;
auto&& [type,name,bhvSid] = *result;
return TDecl( type, name, SidToHoleBhv( bhvSid ) );
}
}