faster parsing
This commit is contained in:
@@ -402,8 +402,14 @@ int cell_init(int argc, char **argv)
|
|||||||
char *json = JS_AST(script, strlen(script), filename);
|
char *json = JS_AST(script, strlen(script), filename);
|
||||||
if (json) {
|
if (json) {
|
||||||
int has_errors = print_json_errors(json);
|
int has_errors = print_json_errors(json);
|
||||||
printf("%s\n", json);
|
cJSON *root = cJSON_Parse(json);
|
||||||
free(json);
|
free(json);
|
||||||
|
if (root) {
|
||||||
|
char *pretty = cJSON_Print(root);
|
||||||
|
cJSON_Delete(root);
|
||||||
|
printf("%s\n", pretty);
|
||||||
|
free(pretty);
|
||||||
|
}
|
||||||
free(allocated_script);
|
free(allocated_script);
|
||||||
return has_errors ? 1 : 0;
|
return has_errors ? 1 : 0;
|
||||||
} else {
|
} else {
|
||||||
@@ -445,8 +451,14 @@ int cell_init(int argc, char **argv)
|
|||||||
char *json = JS_Tokenize(script, strlen(script), filename);
|
char *json = JS_Tokenize(script, strlen(script), filename);
|
||||||
if (json) {
|
if (json) {
|
||||||
int has_errors = print_json_errors(json);
|
int has_errors = print_json_errors(json);
|
||||||
printf("%s\n", json);
|
cJSON *root = cJSON_Parse(json);
|
||||||
free(json);
|
free(json);
|
||||||
|
if (root) {
|
||||||
|
char *pretty = cJSON_Print(root);
|
||||||
|
cJSON_Delete(root);
|
||||||
|
printf("%s\n", pretty);
|
||||||
|
free(pretty);
|
||||||
|
}
|
||||||
free(allocated_script);
|
free(allocated_script);
|
||||||
return has_errors ? 1 : 0;
|
return has_errors ? 1 : 0;
|
||||||
} else {
|
} else {
|
||||||
@@ -507,8 +519,14 @@ int cell_init(int argc, char **argv)
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("%s\n", mcode_json);
|
cJSON *root = cJSON_Parse(mcode_json);
|
||||||
free(mcode_json);
|
free(mcode_json);
|
||||||
|
if (root) {
|
||||||
|
char *pretty = cJSON_Print(root);
|
||||||
|
cJSON_Delete(root);
|
||||||
|
printf("%s\n", pretty);
|
||||||
|
free(pretty);
|
||||||
|
}
|
||||||
|
|
||||||
free(allocated_script);
|
free(allocated_script);
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
@@ -28342,11 +28342,12 @@ typedef struct ASTParseState {
|
|||||||
|
|
||||||
/* Add a length-delimited string to a cJSON object (source pointers aren't null-terminated) */
|
/* 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) {
|
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);
|
memcpy (tmp, str, len);
|
||||||
tmp[len] = '\0';
|
tmp[len] = '\0';
|
||||||
cJSON_AddStringToObject (obj, key, tmp);
|
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 */
|
/* Compare a length-delimited token string against a null-terminated literal */
|
||||||
@@ -29724,16 +29725,9 @@ static const ASTBinOp ast_binops[] = {
|
|||||||
{ 0, NULL, 0 }
|
{ 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++) {
|
for (int i = 0; ast_binops[i].kind; i++) {
|
||||||
if (ast_binops[i].token == token) return ast_binops[i].prec;
|
if (ast_binops[i].token == token) return &ast_binops[i];
|
||||||
}
|
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@@ -29744,17 +29738,16 @@ static cJSON *ast_parse_binary (ASTParseState *s, int min_prec) {
|
|||||||
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
const uint8_t *start = s->token_ptr;
|
const uint8_t *start = s->token_ptr;
|
||||||
int prec = ast_get_binop_prec (s->token_val);
|
const ASTBinOp *op = ast_get_binop (s->token_val);
|
||||||
if (prec == 0 || prec < min_prec) break;
|
if (!op || op->prec < min_prec) break;
|
||||||
|
|
||||||
const char *kind = ast_get_binop_kind (s->token_val);
|
|
||||||
ast_next_token (s);
|
ast_next_token (s);
|
||||||
|
|
||||||
/* Right associativity for ** */
|
/* 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 *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, "left", left);
|
||||||
cJSON_AddItemToObject (node, "right", right);
|
cJSON_AddItemToObject (node, "right", right);
|
||||||
ast_node_end (s, node, s->buf_ptr);
|
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 */
|
/* Convert to JSON string */
|
||||||
char *json = cJSON_Print (ast);
|
char *json = cJSON_PrintUnformatted (ast);
|
||||||
cJSON_Delete (ast);
|
cJSON_Delete (ast);
|
||||||
|
|
||||||
return json;
|
return json;
|
||||||
@@ -31316,7 +31309,7 @@ char *JS_Tokenize (const char *source, size_t len, const char *filename) {
|
|||||||
cJSON_AddItemToObject (root, "errors", s.errors);
|
cJSON_AddItemToObject (root, "errors", s.errors);
|
||||||
}
|
}
|
||||||
|
|
||||||
char *json = cJSON_Print (root);
|
char *json = cJSON_PrintUnformatted (root);
|
||||||
cJSON_Delete (root);
|
cJSON_Delete (root);
|
||||||
return json;
|
return json;
|
||||||
}
|
}
|
||||||
@@ -35643,7 +35636,7 @@ char *JS_Mcode (const char *ast_json) {
|
|||||||
if (s.errors)
|
if (s.errors)
|
||||||
cJSON_AddItemToObject (mach, "errors", s.errors);
|
cJSON_AddItemToObject (mach, "errors", s.errors);
|
||||||
|
|
||||||
char *json = cJSON_Print (mach);
|
char *json = cJSON_PrintUnformatted (mach);
|
||||||
cJSON_Delete (mach);
|
cJSON_Delete (mach);
|
||||||
return json;
|
return json;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user