json layer refactor

This commit is contained in:
jacob 2025-07-30 18:28:23 -05:00
parent ca07cde1a9
commit 9e353dd1c9
9 changed files with 570 additions and 376 deletions

View File

@ -409,7 +409,7 @@ void gp_startup(void)
G.fenced_releases_arena = AllocArena(Gibi(64)); G.fenced_releases_arena = AllocArena(Gibi(64));
/* Initialize embedded shader archive */ /* Initialize embedded shader archive */
String embedded_data = inc_dxc_tar(); String embedded_data = INC_GetDxcTar();
if (embedded_data.len <= 0) { if (embedded_data.len <= 0) {
P_Panic(Lit("No embedded shaders found")); P_Panic(Lit("No embedded shaders found"));
} }

View File

@ -5,7 +5,7 @@
#if RESOURCES_EMBEDDED #if RESOURCES_EMBEDDED
IncbinInclude(res_tar, IncbinDir "res.tar"); IncbinInclude(res_tar, IncbinDir "res.tar");
String inc_res_tar(void) String INC_GetResTar(void)
{ {
return IncbinGet(res_tar); return IncbinGet(res_tar);
} }
@ -13,7 +13,7 @@ String inc_res_tar(void)
IncbinInclude(dxc_tar, IncbinDir "dxc.tar"); IncbinInclude(dxc_tar, IncbinDir "dxc.tar");
String inc_dxc_tar(void) String INC_GetDxcTar(void)
{ {
return IncbinGet(dxc_tar); return IncbinGet(dxc_tar);
} }

View File

@ -1,5 +1,5 @@
#if RESOURCES_EMBEDDED #if RESOURCES_EMBEDDED
String inc_res_tar(void); String INC_GetResTar(void);
#endif #endif
String inc_dxc_tar(void); String INC_GetDxcTar(void);

File diff suppressed because it is too large Load Diff

View File

@ -1,14 +1,18 @@
////////////////////////////////
//~ Blob types
typedef i32 JSON_Type; enum { typedef i32 JSON_Type; enum
JSON_TYPE_NULL, {
JSON_TYPE_BOOL, JSON_Type_Null,
JSON_TYPE_NUMBER, JSON_Type_Bool,
JSON_TYPE_STRING, JSON_Type_Number,
JSON_TYPE_ARRAY, JSON_Type_String,
JSON_TYPE_OBJECT JSON_Type_Array,
JSON_Type_Object
}; };
Struct(JSON_Blob) { Struct(JSON_Blob)
{
JSON_Type type; JSON_Type type;
String key; String key;
@ -17,29 +21,139 @@ Struct(JSON_Blob) {
JSON_Blob *child_first; JSON_Blob *child_first;
JSON_Blob *child_last; JSON_Blob *child_last;
union { union
{
String string; String string;
f64 number; f64 number;
b32 boolean; b32 boolean;
} value; } value;
}; };
Struct(JSON_Error) { Struct(JSON_Error)
{
String msg; String msg;
u64 start; u64 start;
u64 end; u64 end;
JSON_Error *next; JSON_Error *next;
}; };
Struct(JSON_ErrorList) { Struct(JSON_ErrorList)
{
u64 count; u64 count;
JSON_Error *first; JSON_Error *first;
JSON_Error *last; JSON_Error *last;
}; };
Struct(JSON_Result) { Struct(JSON_Result)
{
JSON_Blob *root; JSON_Blob *root;
JSON_ErrorList errors; JSON_ErrorList errors;
}; };
JSON_Result json_from_string(Arena *arena, String src); ////////////////////////////////
//~ Lexer types
#define JSON_Case_Newline \
case 0x0A: /* Line feed or New line */ \
case 0x0D /* Carriage return */
#define JSON_Case_Space \
case 0x20: /* Space */ \
case 0x09 /* Horizontal tab */
#define JSON_Case_Digit0Through9 \
case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9'
#define JSON_Case_Digit1Through9 \
case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9'
#define JSON_Case_Symbol \
case ',': case ':': case '[': case ']': case '{': case '}'
typedef i32 JSON_TokenKind; enum
{
JSON_TokenKind_Unknown,
JSON_TokenKind_Number,
JSON_TokenKind_String,
JSON_TokenKind_KeywordTrue,
JSON_TokenKind_KeywordFalse,
JSON_TokenKind_KeywordNull,
JSON_TokenKind_Comma,
JSON_TokenKind_Colon,
JSON_TokenKind_SquareBraceOpen,
JSON_TokenKind_SquareBraceClose,
JSON_TokenKind_CurlyBraceOpen,
JSON_TokenKind_CurlyBraceClose,
JSON_TokenKind_Bof,
JSON_TokenKind_Eof
};
Struct(JSON_Token)
{
JSON_TokenKind kind;
u64 start;
u64 end;
JSON_Token *next;
};
Struct(JSON_TokenList)
{
JSON_Token *token_first;
JSON_Token *token_last;
};
typedef i32 JSON_LexNumberState; enum
{
JSON_LexNumberState_Whole,
JSON_LexNumberState_Fraction,
JSON_LexNumberState_Exponent
};
Global Readonly String JSON_keyword_strings[] = {
['t'] = LitNoCast("true"),
['f'] = LitNoCast("false"),
['n'] = LitNoCast("null")
};
Global Readonly JSON_TokenKind JSON_keyword_types[] = {
['t'] = JSON_TokenKind_KeywordTrue,
['f'] = JSON_TokenKind_KeywordFalse,
['n'] = JSON_TokenKind_KeywordNull
};
////////////////////////////////
//~ Parser types
Struct(JSON_Parser)
{
/* Input */
String src;
JSON_Token *at;
/* Output */
JSON_Blob *root;
JSON_ErrorList errors;
};
////////////////////////////////
//~ Lex
JSON_Token *JSON_PushToken(Arena *arena, JSON_TokenList *list);
JSON_TokenList JSON_TokensFromString(Arena *arena, String src);
////////////////////////////////
//~ Interpret
f64 interpret_number(String src);
String interpret_string(Arena *arena, String src, String *error);
////////////////////////////////
//~ Parse
void JSON_PushError(Arena *arena, JSON_Parser *p, JSON_Token *t, String msg);
void JSON_Parse(Arena *arena, JSON_Parser *p);
JSON_Result JSON_BlobFromString(Arena *arena, String src);

View File

@ -3,38 +3,6 @@
P_SharedLogCtx P_shared_log_ctx = ZI; P_SharedLogCtx P_shared_log_ctx = ZI;
Readonly P_LogLevelSettings P_log_settings[P_LogLevel_Count] = {
[P_LogLevel_Critical] = {
LitNoCast("CRITICAL"),
ColorPurple
},
[P_LogLevel_Error] = {
LitNoCast("ERROR"),
ColorRed
},
[P_LogLevel_Warning] = {
LitNoCast("WARNING"),
ColorYellow
},
[P_LogLevel_Success] = {
LitNoCast("SUCCESS"),
ColorGreen
},
[P_LogLevel_Info] = {
LitNoCast("INFO"),
ColorWhite
},
[P_LogLevel_Debug] = {
LitNoCast("DEBUG"),
ColorBlue
}
};
//////////////////////////////// ////////////////////////////////
//~ Startup //~ Startup

View File

@ -81,7 +81,38 @@ Struct(P_LogLevelSettings)
u32 color; u32 color;
}; };
extern Readonly P_LogLevelSettings P_log_settings[P_LogLevel_Count]; Global Readonly P_LogLevelSettings P_log_settings[P_LogLevel_Count] = {
[P_LogLevel_Critical] = {
LitNoCast("CRITICAL"),
ColorPurple
},
[P_LogLevel_Error] = {
LitNoCast("ERROR"),
ColorRed
},
[P_LogLevel_Warning] = {
LitNoCast("WARNING"),
ColorYellow
},
[P_LogLevel_Success] = {
LitNoCast("SUCCESS"),
ColorGreen
},
[P_LogLevel_Info] = {
LitNoCast("INFO"),
ColorWhite
},
[P_LogLevel_Debug] = {
LitNoCast("DEBUG"),
ColorBlue
}
};
//////////////////////////////// ////////////////////////////////
//~ Startup //~ Startup

View File

@ -22,7 +22,7 @@ R_StartupReceipt resource_startup(void)
G.arena = AllocArena(Gibi(64)); G.arena = AllocArena(Gibi(64));
#if RESOURCES_EMBEDDED #if RESOURCES_EMBEDDED
String embedded_data = inc_res_tar(); String embedded_data = INC_GetResTar();
if (embedded_data.len <= 0) { if (embedded_data.len <= 0) {
P_Panic(Lit("No embedded resources found")); P_Panic(Lit("No embedded resources found"));
} }

View File

@ -46,7 +46,7 @@ P_WindowSettings *settings_deserialize(Arena *arena, String src, String *error_o
JSON_Error json_error = ZI; JSON_Error json_error = ZI;
P_WindowSettings *settings = PushStruct(arena, P_WindowSettings); P_WindowSettings *settings = PushStruct(arena, P_WindowSettings);
JSON_Result parse_result = json_from_string(scratch.arena, src); JSON_Result parse_result = JSON_BlobFromString(scratch.arena, src);
if (parse_result.errors.count > 0) { if (parse_result.errors.count > 0) {
json_error = *parse_result.errors.first; json_error = *parse_result.errors.first;
@ -60,7 +60,7 @@ P_WindowSettings *settings_deserialize(Arena *arena, String src, String *error_o
} }
JSON_Blob *window = root->child_first; JSON_Blob *window = root->child_first;
if (!window || window->type != JSON_TYPE_OBJECT || !EqString(window->key, Lit("window"))) { if (!window || window->type != JSON_Type_Object || !EqString(window->key, Lit("window"))) {
error = Lit("\"window\" object not found"); error = Lit("\"window\" object not found");
goto abort; goto abort;
} }
@ -75,7 +75,7 @@ P_WindowSettings *settings_deserialize(Arena *arena, String src, String *error_o
String key = child->key; String key = child->key;
if (EqString(key, Lit("maximized"))) { if (EqString(key, Lit("maximized"))) {
if (child->type != JSON_TYPE_BOOL) { if (child->type != JSON_Type_Bool) {
error = Lit("Expected boolean for \"maximized\""); error = Lit("Expected boolean for \"maximized\"");
goto abort; goto abort;
} }
@ -84,7 +84,7 @@ P_WindowSettings *settings_deserialize(Arena *arena, String src, String *error_o
} }
found_maximized = 1; found_maximized = 1;
} else if (EqString(key, Lit("fullscreen"))) { } else if (EqString(key, Lit("fullscreen"))) {
if (child->type != JSON_TYPE_BOOL) { if (child->type != JSON_Type_Bool) {
error = Lit("Expected boolean for \"fulscreen\""); error = Lit("Expected boolean for \"fulscreen\"");
goto abort; goto abort;
} }
@ -93,28 +93,28 @@ P_WindowSettings *settings_deserialize(Arena *arena, String src, String *error_o
} }
found_fullscreen = 1; found_fullscreen = 1;
} else if (EqString(key, Lit("x"))) { } else if (EqString(key, Lit("x"))) {
if (child->type != JSON_TYPE_NUMBER) { if (child->type != JSON_Type_Number) {
error = Lit("Expected number for \"x\""); error = Lit("Expected number for \"x\"");
goto abort; goto abort;
} }
settings->floating_x = RoundF32ToI32(child->value.number); settings->floating_x = RoundF32ToI32(child->value.number);
found_x = 1; found_x = 1;
} else if (EqString(key, Lit("y"))) { } else if (EqString(key, Lit("y"))) {
if (child->type != JSON_TYPE_NUMBER) { if (child->type != JSON_Type_Number) {
error = Lit("Expected number for \"y\""); error = Lit("Expected number for \"y\"");
goto abort; goto abort;
} }
settings->floating_y = RoundF32ToI32(child->value.number); settings->floating_y = RoundF32ToI32(child->value.number);
found_y = 1; found_y = 1;
} else if (EqString(key, Lit("width"))) { } else if (EqString(key, Lit("width"))) {
if (child->type != JSON_TYPE_NUMBER) { if (child->type != JSON_Type_Number) {
error = Lit("Expected number for \"width\""); error = Lit("Expected number for \"width\"");
goto abort; goto abort;
} }
settings->floating_width = RoundF32ToI32(child->value.number); settings->floating_width = RoundF32ToI32(child->value.number);
found_width = 1; found_width = 1;
} else if (EqString(key, Lit("height"))) { } else if (EqString(key, Lit("height"))) {
if (child->type != JSON_TYPE_NUMBER) { if (child->type != JSON_Type_Number) {
error = Lit("Expected number for \"height\""); error = Lit("Expected number for \"height\"");
goto abort; goto abort;
} }