updated math config, added int to standard config, added bool to standard config

This commit is contained in:
herbglitch 2025-03-22 03:38:28 -06:00
parent ce9e6a1c9a
commit 0dab5f336b
5 changed files with 299 additions and 252 deletions

View file

@ -566,7 +566,7 @@ void ARC_Config_Create(ARC_Config **config){
"<type> -> <variable>\n"
"<value> -> <variable> | <numberSign> | <string> | <nestedValue>\n"
"<nestedValue> -> OPEN_CURLY_BRACE <whitespace> <valueArgs> <whitespace> CLOSE_CURLY_BRACE\n"
"<valueArgs> -> <value> COMMA <valueArgs> | <value>\n"
"<valueArgs> -> <value> <whitespace> COMMA <whitespace> <valueArgs> | <value>\n"
"<variable> -> ALPHA_UPPER_CHAR <variableName> | ALPHA_LOWER_CHAR <variableName> | UNDERSCORE <variableName>\n"
"<variableName> -> <variableChar> <variableName> | LAMBDA\n"
@ -760,15 +760,15 @@ void ARC_Config_UnloadFromFile(ARC_Config *config, ARC_String *path){
ARC_Parser_ParseFile(config->parser, path);
}
void ARC_Config_InitStdTypes(ARC_Config *config){
//ARC_Config_RegisterTypeWithCStr(config, "bool" , (ARC_ConfigType){ ARC_ConfigType_BoolCopyFn , ARC_ConfigType_BoolDestroyFn , NULL });
//ARC_Config_RegisterTypeWithCStr(config, "int8" , (ARC_ConfigType){ ARC_ConfigType_Int8CopyFn , ARC_ConfigType_Int8DestroyFn , NULL });
void ARC_Config_InitStd(ARC_Config *config){
ARC_Config_RegisterTypeWithCStr(config, "bool" , (ARC_ConfigType){ ARC_ConfigType_BoolCopyFn , ARC_ConfigType_BoolDestroyFn , NULL });
ARC_Config_RegisterTypeWithCStr(config, "int8" , (ARC_ConfigType){ ARC_ConfigType_Int8CopyFn , ARC_ConfigType_Int8DestroyFn , NULL });
ARC_Config_RegisterTypeWithCStr(config, "uint8" , (ARC_ConfigType){ ARC_ConfigType_Uint8CopyFn , ARC_ConfigType_Uint8DestroyFn , NULL });
//ARC_Config_RegisterTypeWithCStr(config, "int16" , (ARC_ConfigType){ ARC_ConfigType_Int16CopyFn , ARC_ConfigType_Int16DestroyFn , NULL });
ARC_Config_RegisterTypeWithCStr(config, "int16" , (ARC_ConfigType){ ARC_ConfigType_Int16CopyFn , ARC_ConfigType_Int16DestroyFn , NULL });
ARC_Config_RegisterTypeWithCStr(config, "uint16" , (ARC_ConfigType){ ARC_ConfigType_Uint16CopyFn, ARC_ConfigType_Uint16DestroyFn, NULL });
//ARC_Config_RegisterTypeWithCStr(config, "int32" , (ARC_ConfigType){ ARC_ConfigType_Int32CopyFn , ARC_ConfigType_Int32DestroyFn , NULL });
ARC_Config_RegisterTypeWithCStr(config, "int32" , (ARC_ConfigType){ ARC_ConfigType_Int32CopyFn , ARC_ConfigType_Int32DestroyFn , NULL });
ARC_Config_RegisterTypeWithCStr(config, "uint32" , (ARC_ConfigType){ ARC_ConfigType_Uint32CopyFn, ARC_ConfigType_Uint32DestroyFn, NULL });
//ARC_Config_RegisterTypeWithCStr(config, "int64" , (ARC_ConfigType){ ARC_ConfigType_Int64CopyFn , ARC_ConfigType_Int64DestroyFn , NULL });
ARC_Config_RegisterTypeWithCStr(config, "int64" , (ARC_ConfigType){ ARC_ConfigType_Int64CopyFn , ARC_ConfigType_Int64DestroyFn , NULL });
ARC_Config_RegisterTypeWithCStr(config, "uint64" , (ARC_ConfigType){ ARC_ConfigType_Uint64CopyFn, ARC_ConfigType_Uint64DestroyFn, NULL });
//ARC_Config_RegisterTypeWithCStr(config, "float" , (ARC_ConfigType){ ARC_ConfigType_FloatCopyFn , ARC_ConfigType_FloatDestroyFn , NULL });
//ARC_Config_RegisterTypeWithCStr(config, "double" , (ARC_ConfigType){ ARC_ConfigType_DoubleCopyFn, ARC_ConfigType_DoubleDestroyFn, NULL });
@ -776,16 +776,129 @@ void ARC_Config_InitStdTypes(ARC_Config *config){
}
void ARC_ConfigType_BoolCopyFn(void **type, ARC_ParserTagToken *parsedData, ARC_Config *config, void *userdata){
//TODO: write this
//go into the <variable> tag
ARC_ParserTagToken *childTagToken = (ARC_ParserTagToken *)ARC_Vector_Get(parsedData->tagTokens, 0);
if(childTagToken->id != ARC_CONFIG_VARIABLE){
arc_errno = ARC_ERRNO_DATA;
ARC_DEBUG_LOG_ERROR("ARC_ConfigType_BoolNumberHelperCopyFn(type, parsedData, config), parsed data was not a <numberSign>");
*type = NULL;
return;
}
//get the value as a string
ARC_String *value;
ARC_String_Create(&value, NULL, 0);
ARC_ParserData_HelperRecurseStringAdd(&value, childTagToken);
if(ARC_String_EqualsCStringWithStrlen(value, "true") == ARC_True){
ARC_Bool *boolValue = (ARC_Bool *)malloc(sizeof(ARC_Bool));
*boolValue = ARC_True;
ARC_String_Destroy(value);
*type = boolValue;
return;
}
if(ARC_String_EqualsCStringWithStrlen(value, "false") == ARC_True){
ARC_Bool *boolValue = (ARC_Bool *)malloc(sizeof(ARC_Bool));
*boolValue = ARC_False;
ARC_String_Destroy(value);
*type = boolValue;
return;
}
//error if true or false was not passed in
arc_errno = ARC_ERRNO_DATA;
ARC_DEBUG_LOG_ERROR_WITH_VARIABLES("ARC_ConfigType_BoolNumberHelperCopyFn(type, parsedData, config), bool can only be \"true\" or \"false\" but was given \"%s\"", value->data);
ARC_String_Destroy(value);
*type = NULL;
}
void ARC_ConfigType_BoolDestroyFn(void *type){
//TODO: write this
free((ARC_Bool *)type);
}
//private function to make checking ints much easier
void ARC_ConfigType_IntNumberHelperCopyFn(void **type, ARC_ParserTagToken *parsedData, uint64_t maxSize){
//TODO: write this
//go into the <numberSign> tag
ARC_ParserTagToken *childTagToken = (ARC_ParserTagToken *)ARC_Vector_Get(parsedData->tagTokens, 0);
if(childTagToken->id != ARC_CONFIG_NUMBER_SIGN){
arc_errno = ARC_ERRNO_DATA;
ARC_DEBUG_LOG_ERROR("ARC_ConfigType_IntNumberHelperCopyFn(type, parsedData, config), parsed data was not a <numberSign>");
*type = NULL;
return;
}
//get the max positive size
uint64_t maxPositiveSize = maxSize / 2;
//set the max size to its max for positive or negative
int64_t maxSignSize = maxSize / 2;
if(childTagToken->id != ARC_CONFIG_NUMBER_SIGN){
maxSize *= 1;
maxSize--;
}
//check if the first tag is a minus sign and create a string starting with that if it is
childTagToken = (ARC_ParserTagToken *)ARC_Vector_Get(childTagToken->tagTokens, 0);
ARC_String *intString;
ARC_String_Create(&intString, NULL, 0);
//get the number as a string
ARC_ParserData_HelperRecurseStringAdd(&intString, parsedData);
//TODO: check that this gives the right number
//get the length of the max size
uint32_t maxSizeLength = 0;
for(uint32_t size = maxPositiveSize; 0 < size; size /= 10){
maxSizeLength++;
}
if(childTagToken->id != ARC_CONFIG_NUMBER_SIGN){
maxSizeLength++;
}
//if the string is bigger than the possible size return NULL and error
if(intString->length > maxSizeLength){
arc_errno = ARC_ERRNO_DATA;
ARC_DEBUG_LOG_ERROR_WITH_VARIABLES("ARC_ConfigType_IntNumberHelperCopyFn(type, parsedData, config), size \"%s\" was bigger or smaller than than the max %lu or min %lu", intString->data, maxPositiveSize, (maxPositiveSize * -1) - 1);
*type = NULL;
ARC_String_Destroy(intString);
return;
}
if(intString->length == maxSize){
//the max size of a uint64 is -9,223,372,036,854,775,808 so 20 will be big enough for the size
char maxintCStr[20];
sprintf(maxintCStr, "%ld", maxSignSize);
printf("value %s\n", maxintCStr);
//check that the number is less than the max
int8_t stringIndex = maxSize;
for(int8_t index = maxSize + 1; index >= 0; index--, stringIndex--){
if(intString->data[stringIndex] > maxintCStr[index]){
arc_errno = ARC_ERRNO_DATA;
ARC_DEBUG_LOG_ERROR_WITH_VARIABLES("ARC_ConfigType_IntNumberHelperCopyFn(type, parsedData, config), size \"%s\" was bigger than the max of the signed value %lu", intString->data, maxSize);
*type = NULL;
ARC_String_Destroy(intString);
return;
}
//if the number is smaller it is safe to stop checking
if(intString->data[stringIndex] < maxintCStr[index]){
break;
}
}
}
//copy the int value
*type = malloc(maxSizeLength);
uint64_t valueAsUint64 = ARC_String_ToInt64_t(intString);
memcpy(*type, &valueAsUint64, maxSizeLength);
//cleanup
ARC_String_Destroy(intString);
}
//private function to make checking uints much easier
@ -831,8 +944,8 @@ void ARC_ConfigType_UintNumberHelperCopyFn(void **type, ARC_ParserTagToken *pars
}
if(uintString->length == maxSize){
//the max size of a uint64 is 8,446,744,073,709,551,615 so 19 will be big enough for the size
char maxuintCStr[19];
//the max size of a uint64 is 8,446,744,073,709,551,615 so 20 will be big enough for the size
char maxuintCStr[20];
sprintf(maxuintCStr, "%lu", maxSize);
//check that the number is less than the max