//------------------------------------------------------------------------------
// gb_string_to_semiring: convert a string to a GraphBLAS semiring
//------------------------------------------------------------------------------

// SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2026, All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

//------------------------------------------------------------------------------

// Only built-in GraphBLAS types and operators are supported.

GrB_Info gb_string_to_semiring          // return a GrB semiring from a string
(
    // output:
    GrB_Semiring *semiring,
    // input/output:
    char *semiring_string,              // string defining the semiring
    // inputs:
    const GrB_Type atype,               // type of A
    const GrB_Type btype,               // type of B
    char err [ERRLEN]
)
{

    //--------------------------------------------------------------------------
    // get the string and parse it
    //--------------------------------------------------------------------------

    int32_t position [2] ;
    gb_find_dot (position, semiring_string) ;

    // semiring string must have at least one dot
    CHECK_ERROR (position [0] < 0,
        "invalid semiring; must have the form 'add.mult' or 'add.mult.type'") ;

    // get the name of the add operator
    char *add_name  = semiring_string ;
    semiring_string [position [0]] = '\0' ;

    // get the name and optional type of the mult operator
    char *mult_name = semiring_string + position [0] + 1 ;
    char *mult_typename = NULL ;
    if (position [1] >= 0)
    { 
        semiring_string [position [1]] = '\0' ;
        mult_typename = semiring_string + position [1] + 1 ;
    }

    //--------------------------------------------------------------------------
    // get the mult operator
    //--------------------------------------------------------------------------

    bool type_not_given = (mult_typename == NULL) ;
    GrB_Type mult_type ;
    if (mult_typename == NULL)
    { 
        mult_type = gb_default_type (atype, btype) ;
    }
    else
    { 
        mult_type = gb_string_to_type (mult_typename) ;
    }

    GrB_BinaryOp mult = NULL ;
    OK (gb_string_and_type_to_binop_or_idxunop (&mult,
        /* no index unary op: */ NULL, NULL,
        mult_name, mult_type, type_not_given, err)) ;
    CHECK_ERROR (mult == NULL, "invalid semiring (unknown multiply operator)") ;

    //--------------------------------------------------------------------------
    // get the add operator
    //--------------------------------------------------------------------------

    GrB_Type add_type = NULL ;
    OK (gb_binaryop_ztype (&add_type, mult, err)) ;
    GrB_BinaryOp add = NULL ;
    OK (gb_string_and_type_to_binop_or_idxunop (&add,
        /* no index unary op: */ NULL, NULL,
        add_name, add_type, false, err)) ;
    CHECK_ERROR (add == NULL, "invalid semiring (unknown add operator)") ;

    //--------------------------------------------------------------------------
    // convert the add and mult operators to a semiring and return result
    //--------------------------------------------------------------------------

    OK (gb_semiring (semiring, add, mult, err)) ;
    return (GrB_SUCCESS) ;
}

