2026-08-03 21:01:34 +10:00
|
|
|
#include "value.h"
|
|
|
|
|
|
|
|
|
|
uint32_t globalValueIncrementer = 0;
|
|
|
|
|
|
|
|
|
|
char* getIncrementedName(void) {
|
|
|
|
|
char* buffer = malloc(32);
|
|
|
|
|
if (buffer == NULL) return buffer;
|
|
|
|
|
|
|
|
|
|
snprintf(buffer, 32, "%%%d", globalValueIncrementer);
|
|
|
|
|
|
|
|
|
|
globalValueIncrementer++;
|
|
|
|
|
|
|
|
|
|
return buffer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GravityValue gravityNewValue(const char* name, GravityType type) {
|
|
|
|
|
GravityValue newValue = malloc(sizeof(struct GravityOpaqueValue));
|
|
|
|
|
if (newValue == NULL) return newValue;
|
|
|
|
|
|
|
|
|
|
newValue->kind = GravityLocalValueKind;
|
|
|
|
|
newValue->type = type;
|
|
|
|
|
|
|
|
|
|
if (name == NULL) {
|
|
|
|
|
name = getIncrementedName();
|
|
|
|
|
} else {
|
|
|
|
|
name = strdup(name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
newValue->name = name;
|
|
|
|
|
|
|
|
|
|
return newValue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GravityValue gravityTypeToTypeValue(GravityType type) {
|
|
|
|
|
GravityType valueType = gravityCreateType();
|
|
|
|
|
valueType->typeKind = GravityTypeTypeKind;
|
|
|
|
|
valueType->typeData = type;
|
|
|
|
|
|
|
|
|
|
GravityValue newValue = gravityNewValue(NULL, valueType);
|
|
|
|
|
if (newValue == NULL) return newValue;
|
|
|
|
|
|
|
|
|
|
return newValue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char* gravityValueToCStr(GravityValue value) {
|
|
|
|
|
switch (value->kind) {
|
|
|
|
|
case GravityLocalValueKind:
|
|
|
|
|
|
|
|
|
|
return strdup(value->name);
|
|
|
|
|
|
|
|
|
|
case GravityConstantValueKind: {
|
|
|
|
|
StringBuffer buff;
|
|
|
|
|
initStringBuff(&buff);
|
|
|
|
|
|
|
|
|
|
switch (value->type->typeKind) {
|
|
|
|
|
case GravityIntTypeKind: {
|
|
|
|
|
sbAppend(&buff, "%ld", value->constant.intValue);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case GravityTypeTypeKind: {
|
|
|
|
|
sbAppend(&buff, "%s", gravityTypeToCStr(value->type->typeData));
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
default: assert(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return buff.data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
default: assert(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GravityValue gravityNewConstant(GravityType type) {
|
|
|
|
|
GravityValue newValue = malloc(sizeof(struct GravityOpaqueValue));
|
|
|
|
|
if (newValue == NULL) return newValue;
|
|
|
|
|
|
|
|
|
|
newValue->kind = GravityConstantValueKind;
|
|
|
|
|
newValue->type = type;
|
|
|
|
|
newValue->name = NULL;
|
|
|
|
|
|
|
|
|
|
return newValue;
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-03 21:24:29 +10:00
|
|
|
GravityValue gravityNewIntConstant(GravityType intType, int64_t value) {
|
2026-08-03 21:01:34 +10:00
|
|
|
assert(intType->typeKind == GravityIntTypeKind);
|
|
|
|
|
|
|
|
|
|
GravityValue newValue = gravityNewConstant(intType);
|
|
|
|
|
if (newValue == NULL) return newValue;
|
|
|
|
|
|
|
|
|
|
newValue->constant.intValue = value;
|
|
|
|
|
|
|
|
|
|
return newValue;
|
|
|
|
|
}
|