#include "parse.h"

using namespace empathy;
using namespace empathy::parse;

namespace empathy::ir
{
    const Term& Bridge< parse::Rule >::Type()
    {
        static auto type = ValueToIRExpr( Value( TypeType(), TSID( rule ) ) );
        return type;
    }

    Value Bridge< parse::Rule >::ToValue( parse::Rule&& r )
    {
        ptr< void > prule = make_shared< parse::Rule >( move( r ) );
        return Value( Type(), TERM( move( prule ) ) );
    }

    optional< ptr< parse::Rule > > Bridge< parse::Rule >::FromValue( const Value& v )
    {
        if( v.type() != Type() )
            return nullopt;

        const auto& p = get< ptr< void > >( v.val() );
        return static_pointer_cast< parse::Rule >( p );
    }
}

namespace empathy::parse
{
    void RegisterRule( sema::Env& env, const Term& identity, Rule&& rule )
    {
        auto ruleVal = ToValue( move( rule ) );
        auto ruleTerm = ValueToIRExpr( ruleVal );
        env.storeValue( identity, ANYTERM( _ ), ruleTerm );
    }
}
