#include "builtins/builtins.h"
#include "precedence.h"
#include "helpers.h"
using namespace empathy;
using namespace empathy::ir;
using namespace empathy::parse;
namespace empathy::builtins
{
void SetupCommaOp( Env& e )
{
using LocVarOfAnyType =
CustomPattern< LocalVar, LocalVar::PatternAny >;
BuildParseRule( e, ","_sid,
LeftAssInfixOp( "operator_comma"_sid, precedence::CommaOp,
// Overload: any tuple, any tuple
ForTypes< CustomPattern< Value, TuplePattern >, CustomPattern< Value, TuplePattern > >(
[]( auto&& lhs, auto&& rhs ) -> Value
{
if( IsOpenTuple( lhs ) )
{
if( IsOpenTuple( rhs ) )
return ConcatenateTuples( lhs, rhs );
return AppendToTuple( lhs, rhs );
}
if( IsOpenTuple( rhs ) )
return PrependToTuple( lhs, rhs );
return AppendToTuple( AppendToTuple( EmptyTuple(), lhs ), rhs );
} ),
// Overload: any tuple, anything
ForTypes< CustomPattern< Value, TuplePattern >, Value >(
[]( auto&& lhs, auto&& rhs ) -> Value
{
if( IsOpenTuple( lhs ) )
return AppendToTuple( lhs, rhs );
return AppendToTuple( AppendToTuple( EmptyTuple(), lhs ), rhs );
} ),
// Overload: anything, any tuple
ForTypes< Value, CustomPattern< Value, TuplePattern > >(
[]( auto&& lhs, auto&& rhs ) -> Value
{
if( IsOpenTuple( rhs ) )
return PrependToTuple( lhs, rhs );
return AppendToTuple( AppendToTuple( EmptyTuple(), lhs ), rhs );
} ),
// Overload: anything, anything
ForType< Value >(
[]( auto&& lhs, auto&& rhs ) -> Value
{
return AppendToTuple( AppendToTuple( EmptyTuple(), lhs ), rhs );
} ),
// By default, locvars return their content during unification.
// Since we want to keep them as is in tuples, we need specific overloads that will
// get locvars as such.
ForType< LocVarOfAnyType >(
[]( auto&& lhs, auto&& rhs ) -> Value
{
return AppendToTuple( AppendToTuple( EmptyTuple(), lhs ), rhs );
} ),
ForTypes< CustomPattern< Value, TuplePattern >, LocVarOfAnyType >(
[]( auto&& lhs, auto&& rhs ) -> Value
{
if( IsOpenTuple( lhs ) )
return AppendToTuple( lhs, rhs );
return AppendToTuple( AppendToTuple( EmptyTuple(), lhs ), rhs );
} ),
ForTypes< LocVarOfAnyType, CustomPattern< Value, TuplePattern > >(
[]( auto&& lhs, auto&& rhs ) -> Value
{
if( IsOpenTuple( rhs ) )
return PrependToTuple( lhs, rhs );
return AppendToTuple( AppendToTuple( EmptyTuple(), lhs ), rhs );
} )
)
);
}
}