diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2025-04-01 16:55:24 -0400 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2025-04-01 16:55:24 -0400 |
| commit | 6de2d68a700a137bbe55668e036c62280ece8bb5 (patch) | |
| tree | eb1e3cee37cd9b2f1458b9ceff0141bfbd7a91a9 /src/parse.c | |
| parent | a32c3747568562251d6c390faf325bf3ed3946e6 (diff) | |
Moved RNG out of the compiler and into a standalone library
Diffstat (limited to 'src/parse.c')
| -rw-r--r-- | src/parse.c | 24 |
1 files changed, 21 insertions, 3 deletions
diff --git a/src/parse.c b/src/parse.c index 2e3e2ece..97576131 100644 --- a/src/parse.c +++ b/src/parse.c @@ -2442,8 +2442,9 @@ ast_t *parse_file(const char *path, jmp_buf *on_err) { // If cache is getting too big, evict a random entry: if (cached.entries.length > PARSE_CACHE_SIZE) { - uint32_t i = arc4random_uniform(cached.entries.length); - struct {const char *path; ast_t *ast; } *to_remove = Table$entry(cached, i+1); + // FIXME: this currently evicts the first entry, but it should be more like + // an LRU cache + struct {const char *path; ast_t *ast; } *to_remove = Table$entry(cached, 1); Table$str_remove(&cached, to_remove->path); } @@ -2484,7 +2485,24 @@ ast_t *parse(const char *str) { pos = ast->end; whitespace(&pos); if (pos < file->text + file->len && *pos != '\0') - parser_err(&ctx, pos, pos + strlen(pos), "I couldn't parse this part of the file"); + parser_err(&ctx, pos, pos + strlen(pos), "I couldn't parse this part of the string"); + return ast; +} + +ast_t *parse_expression(const char *str) { + file_t *file = spoof_file("<string>", str); + parse_ctx_t ctx = { + .file=file, + .on_err=NULL, + }; + + const char *pos = file->text; + whitespace(&pos); + ast_t *ast = parse_extended_expr(&ctx, pos); + pos = ast->end; + whitespace(&pos); + if (pos < file->text + file->len && *pos != '\0') + parser_err(&ctx, pos, pos + strlen(pos), "I couldn't parse this part of the string"); return ast; } |
