Misc stuff, probably crashes
This commit is contained in:
67
src/Stringify/BytecodeValue.c
Normal file
67
src/Stringify/BytecodeValue.c
Normal file
@@ -0,0 +1,67 @@
|
||||
#include "../../include/ground.h"
|
||||
#include <inttypes.h>
|
||||
|
||||
char* _GroundStringifyBytecodeValue(GroundBytecodeValue* value) {
|
||||
switch (value->type.type) {
|
||||
case GroundType_Int: {
|
||||
char* buf = malloc(snprintf(NULL, 0, "%" PRId64, value->as.Int) + 1);
|
||||
if (buf == NULL) {
|
||||
Ground.Flags.error = true;
|
||||
Ground.Log.Error("malloc failed in Ground.Stringify.Value");
|
||||
return NULL;
|
||||
}
|
||||
sprintf(buf, "%" PRId64, value->as.Int);
|
||||
return buf;
|
||||
}
|
||||
case GroundType_Double: {
|
||||
char* buf = malloc(snprintf(NULL, 0, "%f", value->as.Double) + 1);
|
||||
if (buf == NULL) {
|
||||
Ground.Flags.error = true;
|
||||
Ground.Log.Error("malloc failed in Ground.Stringify.Value");
|
||||
return NULL;
|
||||
}
|
||||
sprintf(buf, "%f", value->as.Double);
|
||||
return buf;
|
||||
}
|
||||
case GroundType_Char: {
|
||||
char* buf = malloc(snprintf(NULL, 0, "%c", value->as.Char) + 1);
|
||||
if (buf == NULL) {
|
||||
Ground.Flags.error = true;
|
||||
Ground.Log.Error("malloc failed in Ground.Stringify.Value");
|
||||
return NULL;
|
||||
}
|
||||
sprintf(buf, "%c", value->as.Char);
|
||||
return buf;
|
||||
}
|
||||
case GroundType_Bool: {
|
||||
char* buf = malloc(6); // max(len(true), len(false)) + 1
|
||||
if (buf == NULL) {
|
||||
Ground.Flags.error = true;
|
||||
Ground.Log.Error("malloc failed in Ground.Stringify.Value");
|
||||
return NULL;
|
||||
}
|
||||
sprintf(buf, value->as.Bool ? "true" : "false");
|
||||
return buf;
|
||||
}
|
||||
case GroundType_String: {
|
||||
return Ground.Stringify.String(&value->as.String);
|
||||
}
|
||||
case GroundType_List: {
|
||||
// TODO implement list stringification
|
||||
}
|
||||
case GroundType_Function: {
|
||||
// TODO implement function stringification
|
||||
}
|
||||
case GroundType_Struct: {
|
||||
// TODO implement struct stringification
|
||||
}
|
||||
case GroundType_Object: {
|
||||
// TODO implement object stringification
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Ground.Flags.error = true;
|
||||
Ground.Log.Error("FIXME implement all cases in Ground.Stringify.Value");
|
||||
return NULL;
|
||||
}
|
||||
Reference in New Issue
Block a user