refactor(hmpl): eval

This commit is contained in:
2025-03-22 19:45:17 +00:00
parent f64636fe9e
commit 972e2d2968
5 changed files with 133 additions and 34 deletions

View File

@@ -268,13 +268,13 @@ Json *json_parse(Arena *arena, const char **s) {
}
char *json_to_string(Arena *arena, Json *item) {
return json_to_string_with_opts(arena, item, 0);
return json_to_string_with_opts(arena, item, JSON_NORAW);
}
/* Minimal JSON printer with raw output option.
When raw is non-zero and the item is a JSON_STRING, it is printed without quotes.
*/
char *json_to_string_with_opts(Arena *arena, Json *item, int raw) {
char *json_to_string_with_opts(Arena *arena, Json *item, JsonRawOpt raw) {
char *out = arena_alloc(arena, 1024);
if (!out)
return NULL;
@@ -303,7 +303,7 @@ char *json_to_string_with_opts(Arena *arena, Json *item, int raw) {
}
sprintf(ptr, "]");
} else if (item->type == JSON_STRING) {
if (raw)
if ((int)raw)
sprintf(ptr, "%s", item->string);
else
sprintf(ptr, "\"%s\"", item->string);

View File

@@ -202,13 +202,18 @@ void substr(const char *src, char *dest, size_t start, size_t len);
// -- Json --
// ----------
typedef enum {
JSON_NORAW = 0,
JSON_RAW = 1,
} JsonRawOpt;
typedef enum {
JSON_NULL,
JSON_BOOL,
JSON_NUMBER,
JSON_STRING,
JSON_ARRAY,
JSON_OBJECT
JSON_OBJECT,
} JsonType;
/* Full JSON structure */
@@ -228,7 +233,7 @@ Json *json_parse(Arena *arena, const char **s);
char *json_to_string(Arena *arena, Json *item);
char *json_to_string_with_opts(Arena *arena, Json *item, int raw);
char *json_to_string_with_opts(Arena *arena, Json *item, JsonRawOpt raw);
/* Retrieve an object item by key (case-sensitive) */
Json *json_get_object_item(Json *object, const char *key);