From 1a925371d3a7e6180eb659f5cf27cd757350b2f7 Mon Sep 17 00:00:00 2001 From: John Alanbrook Date: Sat, 7 Feb 2026 15:38:36 -0600 Subject: [PATCH] faster parsing --- source/cell.c | 24 +++++++++++++++++++++--- source/quickjs.c | 31 ++++++++++++------------------- 2 files changed, 33 insertions(+), 22 deletions(-) diff --git a/source/cell.c b/source/cell.c index faa974fb..b58d513c 100644 --- a/source/cell.c +++ b/source/cell.c @@ -402,8 +402,14 @@ int cell_init(int argc, char **argv) char *json = JS_AST(script, strlen(script), filename); if (json) { int has_errors = print_json_errors(json); - printf("%s\n", json); + cJSON *root = cJSON_Parse(json); free(json); + if (root) { + char *pretty = cJSON_Print(root); + cJSON_Delete(root); + printf("%s\n", pretty); + free(pretty); + } free(allocated_script); return has_errors ? 1 : 0; } else { @@ -445,8 +451,14 @@ int cell_init(int argc, char **argv) char *json = JS_Tokenize(script, strlen(script), filename); if (json) { int has_errors = print_json_errors(json); - printf("%s\n", json); + cJSON *root = cJSON_Parse(json); free(json); + if (root) { + char *pretty = cJSON_Print(root); + cJSON_Delete(root); + printf("%s\n", pretty); + free(pretty); + } free(allocated_script); return has_errors ? 1 : 0; } else { @@ -507,8 +519,14 @@ int cell_init(int argc, char **argv) return 1; } - printf("%s\n", mcode_json); + cJSON *root = cJSON_Parse(mcode_json); free(mcode_json); + if (root) { + char *pretty = cJSON_Print(root); + cJSON_Delete(root); + printf("%s\n", pretty); + free(pretty); + } free(allocated_script); return 0; diff --git a/source/quickjs.c b/source/quickjs.c index 574640f4..87b3693b 100644 --- a/source/quickjs.c +++ b/source/quickjs.c @@ -28342,11 +28342,12 @@ typedef struct ASTParseState { /* Add a length-delimited string to a cJSON object (source pointers aren't null-terminated) */ static void cjson_add_strn (cJSON *obj, const char *key, const char *str, size_t len) { - char *tmp = sys_malloc (len + 1); + char buf[256]; + char *tmp = (len < sizeof (buf)) ? buf : sys_malloc (len + 1); memcpy (tmp, str, len); tmp[len] = '\0'; cJSON_AddStringToObject (obj, key, tmp); - sys_free (tmp); + if (tmp != buf) sys_free (tmp); } /* Compare a length-delimited token string against a null-terminated literal */ @@ -29724,16 +29725,9 @@ static const ASTBinOp ast_binops[] = { { 0, NULL, 0 } }; -static int ast_get_binop_prec (int token) { +static const ASTBinOp *ast_get_binop (int token) { for (int i = 0; ast_binops[i].kind; i++) { - if (ast_binops[i].token == token) return ast_binops[i].prec; - } - return 0; -} - -static const char *ast_get_binop_kind (int token) { - for (int i = 0; ast_binops[i].kind; i++) { - if (ast_binops[i].token == token) return ast_binops[i].kind; + if (ast_binops[i].token == token) return &ast_binops[i]; } return NULL; } @@ -29744,17 +29738,16 @@ static cJSON *ast_parse_binary (ASTParseState *s, int min_prec) { for (;;) { const uint8_t *start = s->token_ptr; - int prec = ast_get_binop_prec (s->token_val); - if (prec == 0 || prec < min_prec) break; + const ASTBinOp *op = ast_get_binop (s->token_val); + if (!op || op->prec < min_prec) break; - const char *kind = ast_get_binop_kind (s->token_val); ast_next_token (s); /* Right associativity for ** */ - int next_prec = (prec == 14) ? prec : prec + 1; + int next_prec = (op->prec == 14) ? op->prec : op->prec + 1; cJSON *right = ast_parse_binary (s, next_prec); - cJSON *node = ast_node (s, kind, start); + cJSON *node = ast_node (s, op->kind, start); cJSON_AddItemToObject (node, "left", left); cJSON_AddItemToObject (node, "right", right); ast_node_end (s, node, s->buf_ptr); @@ -31213,7 +31206,7 @@ char *JS_AST (const char *source, size_t len, const char *filename) { } /* Convert to JSON string */ - char *json = cJSON_Print (ast); + char *json = cJSON_PrintUnformatted (ast); cJSON_Delete (ast); return json; @@ -31316,7 +31309,7 @@ char *JS_Tokenize (const char *source, size_t len, const char *filename) { cJSON_AddItemToObject (root, "errors", s.errors); } - char *json = cJSON_Print (root); + char *json = cJSON_PrintUnformatted (root); cJSON_Delete (root); return json; } @@ -35643,7 +35636,7 @@ char *JS_Mcode (const char *ast_json) { if (s.errors) cJSON_AddItemToObject (mach, "errors", s.errors); - char *json = cJSON_Print (mach); + char *json = cJSON_PrintUnformatted (mach); cJSON_Delete (mach); return json; }