src/vm/opts.c
/**
 * Parses the input string to an array of register values
 * @param registers the total amount of registers
 * @param str the string to parse; the format is reg:val[,...]
 * @return the array containing the register values (0 for unspecified registers)
 */
Parses the input string to an array of register values
@param registers the total amount of registers
@param str the string to parse; the format is reg:val[,...]
@return the array containing the register values (0 for unspecified registers)
Parses the input string to an array of register values @param registers the total amount of registers @param str the string to parse ; the format is reg : val [ ... ] @return the array containing the register values ( 0 for unspecified registers )
int *parse_register_values(int registers, char *str) {
    int *parsed_values = calloc(registers, sizeof(int));
    if (str == NULL) return parsed_values;
    int mode = 0;
    int reg = 0;
    for (int i = 0; str[i] != 0; i++) {
        char c = str[i];
        switch (mode) {
            case 0: 
                if (c == ':') {
                    if (reg >= registers) goto register_error;
                    if (str[i + 1] == 0) goto parse_error;
                    mode = 1;
                    continue;
                } else if (!isdigit(c)) goto parse_error;
                reg = reg * 10 + c - '0';
                break;
            case 1: 
                if (c == ',') {
                    if (str[i + 1] == 0) goto parse_error;
                    mode = 0;
                    reg = 0;
                    continue;
                } else if (!isdigit(c)) goto parse_error;
                parsed_values[reg] = parsed_values[reg] * 10 + c - '0';
                break;
            default:
                abort();
        }
    }
    if (mode != 1) goto parse_error;
    return parsed_values;
    parse_error:
    fprintf(stderr, "Cannot parse `%s` as a valid --register-values <VAL>\n", str);
    memset(parsed_values, 0, registers * sizeof(int));
    return parsed_values;
    register_error:
    fprintf(stderr, "Register `%i` is not a valid register for --register-values <VAL>\n", reg);
    memset(parsed_values, 0, registers * sizeof(int));
    return parsed_values;
}
int * parse_register_values ( int registers , char * str ) { int * parsed_values = calloc ( registers , sizeof ( int ) ) ; if ( str == NULL ) return parsed_values ; int mode = 0 ; int reg = 0 ; for ( int i = 0 ; str [ i ] != 0 ; i ++ ) { char c = str [ i ] ; switch ( mode ) { case 0 : if ( c == ' ' ) { if ( reg >= registers ) goto register_error ; if ( str [ i + 1 ] == 0 ) goto parse_error ; mode = 1 ; continue ; } else if ( ! isdigit ( c ) ) goto parse_error ; reg = reg * 10 + c - ' ' ; break ; case 1 : if ( c == ' ' ) { if ( str [ i + 1 ] == 0 ) goto parse_error ; mode = 0 ; reg = 0 ; continue ; } else if ( ! isdigit ( c ) ) goto parse_error ; parsed_values [ reg ] = parsed_values [ reg ] * 10 + c - ' ' ; break ; default : abort ( ) ; } } if ( mode != 1 ) goto parse_error ; return parsed_values ; parse_error : fprintf ( stderr , " \n " , str ) ; memset ( parsed_values , 0 , registers * sizeof ( int ) ) ; return parsed_values ; register_error : fprintf ( stderr , " \n " , reg ) ; memset ( parsed_values , 0 , registers * sizeof ( int ) ) ; return parsed_values ; }
Parses the input string to an array of register values
 @param registers the total amount of registers
 @param str the string to parse; the format is reg:val[,...]
 @return the array containing the register values (0 for unspecified registers)
Parses the input string to an array of register values @param registers the total amount of registers @param str the string to parse ; the format is reg : val [ ... ] @return the array containing the register values ( 0 for unspecified registers )
// we are parsing a register // we are parsing a value