#include "parse.h"

using namespace empathy;
using namespace empathy::parse;

namespace empathy::parse
{
    const Term& RuleType()
    {
        static auto type = ValueToIRExpr( Value( TSID( type ), TSID( rule ) ) );
        return type;
    }

    Value RuleToValue( Rule&& r )
    {
        return Value( RuleType(), make_shared< Rule >( move( r ) ) );
    }

    ptr< Rule > RuleFromValue( const Value& v )
    {
        if( v.type() != RuleType() )
            return nullptr;

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

    void RegisterRule( sema::Env& env, const StringId& name, Rule&& rule )
    {
        auto ruleVal = RuleToValue( move( rule ) );
        auto ruleTerm = ValueToIRExpr( ruleVal );
        env.storeValue( TVEC( TSID( e0 ), TSID( name ) ), ANYTERM( x ), ruleTerm );
    }
}
