Goose  Artifact [8592ab3820]

Artifact 8592ab3820fc29bc521cdba2b00705c0794be8a3c1afb0ec5df57e85fa179e95:

  • File bs/g0api/extensibility/value.cpp — part of check-in [1ad61a2717] at 2021-11-11 20:05:58 on branch trunk — Refactored the code builder: it is now carried around as a Value, and accessed through a bunch of extension points, so we can have different builders (and even user defined ones) later to make classes etc. (user: zlodo size: 4562)

#include "g0api/g0api.h"
#include "eir/eir.h"
#include "parse/parse.h"

using namespace goose;
using namespace goose::parse;
using namespace goose::g0api;

namespace goose::g0api
{
    void SetupValueExtensibilityFuncs( Env& e )
    {
        DefineConstant( e, "PoisonValue"_sid, ValueToEIR( ToValue( TypeWrapper< Value >( PoisonValue() ) ) ) );

        ////////////////////////////
        // MkValue overloads
        ////////////////////////////
        auto MkValue = CreateOverloadSet( e, "MkValue"_sid );

        RegisterBuiltinFunc< TypeWrapper< Value > ( TypeWrapper< Term >, TypeWrapper< Term > ) >( e, MkValue,
            []( const TypeWrapper< Term >& type, const TypeWrapper< Term >& val ) -> TypeWrapper< Value >
            {
                return Value( type.get(), val.get() );
            } );

        RegisterBuiltinFunc< TypeWrapper< Value > ( TypeWrapper< Term >, TypeWrapper< ptr< Instruction > > ) >( e, MkValue,
            []( const TypeWrapper< Term >& type, const TypeWrapper< ptr< Instruction > >& cir ) -> TypeWrapper< Value >
            {
                return Value( type.get(), cir.get() );
            } );

        RegisterBuiltinFunc< TypeWrapper< Term > ( TypeWrapper< Value > ) >( e, "GetValueType"_sid,
            []( const TypeWrapper< Value >& val ) -> TypeWrapper< Term >
            {
                return val.get().type();
            } );

        RegisterBuiltinFunc< bool ( TypeWrapper< Value >, TermRef< TypeWrapper< Term > > ) >( e, "GetValueVal"_sid,
            []( const TypeWrapper< Value >& val, TermRef< TypeWrapper< Term > >& out ) -> bool
            {
                if( !val.get().isConstant() )
                    return false;

                out = val.get().val();
                return true;
            } );

        RegisterBuiltinFunc< bool ( TypeWrapper< Value >, TermRef< TypeWrapper< ptr< Instruction > > > ) >( e, "GetValueCIR"_sid,
            []( const TypeWrapper< Value >& val, TermRef< TypeWrapper< ptr< Instruction > > >& out ) -> bool
            {
                if( val.get().isConstant() )
                    return false;

                out = val.get().cir();
                return true;
            } );

        RegisterBuiltinFunc< TypeWrapper< LocationId > ( TypeWrapper< Value > ) >( e, "GetValueLocation"_sid,
            []( const TypeWrapper< Value >& val ) -> TypeWrapper< LocationId >
            {
                return val.get().locationId();
            } );

        RegisterBuiltinFunc< bool ( TypeWrapper< Value > ) >( e, "IsPoisonValue"_sid,
            []( const TypeWrapper< Value >& val )
            {
                return val.get().isPoison();
            } );

        RegisterBuiltinFunc< bool ( TypeWrapper< Value > ) >( e, "IsConstantValue"_sid,
            []( const TypeWrapper< Value >& val )
            {
                return val.get().isConstant();
            } );

        RegisterBuiltinFunc< bool ( TypeWrapper< Value > ) >( e, "IsTypeValue"_sid,
            []( const TypeWrapper< Value >& val )
            {
                return val.get().isType();
            } );

        RegisterBuiltinFunc< TypeWrapper< Value > ( TypeWrapper< Value >, TypeWrapper< LocationId > ) >( e, "SetValueLocation"_sid,
            []( const TypeWrapper< Value >& v, const TypeWrapper< LocationId >& loc ) -> TypeWrapper< Value >
            {
                Value val( v );
                return val.setLocationId( loc );
            } );

        RegisterBuiltinFunc< TypeWrapper< Term > ( TypeWrapper< Value > ) >( e, "ValueToEIR"_sid,
            []( const TypeWrapper< Value >& val ) -> TypeWrapper< Term >
            {
                return ValueToEIR( val.get() );
            } );

        RegisterBuiltinFunc< bool ( TypeWrapper< Term >, TermRef< TypeWrapper< Value > > ) >( e, "EIRToValue"_sid,
            []( const TypeWrapper< Term >& t, TermRef< TypeWrapper< Value > >& out )
            {
                auto val = EIRToValue( t.get() );

                if( !val )
                    return false;

                out = move( *val );
                return true;
            } );

        RegisterBuiltinFunc< TypeWrapper< Value > ( CustomPattern< Value, ValuePatternT > ) >( e, "WrapValue"_sid,
            []( const Value& v ) -> TypeWrapper< Value >
            {
                return v;
            } );

        RegisterBuiltinFunc< Value ( TypeWrapper< Value > ) >( e, "UnwrapValue"_sid,
            []( const TypeWrapper< Value >& v )
            {
                return v.get();
            } );
    }
}