Files
zig0/ast.c
2024-12-20 00:24:51 +02:00

101 lines
2.7 KiB
C

#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include "ast.h"
#include "parser.h"
#define N 1024
ast ast_parse(const char* source, const uint32_t len, int* err)
{
uint32_t estimated_token_count = len / 8;
tokenizerTag* token_tags = NULL;
astIndex* token_starts = NULL;
astNodeTag* nodes_tags = NULL;
astTokenIndex* main_tokens = NULL;
astData* nodes_datas = NULL;
astNodeIndex* extra_data_arr = NULL;
astNodeIndex* scratch_arr = NULL;
if (!(token_tags = calloc(estimated_token_count, sizeof(tokenizerTag))))
goto err;
if (!(token_starts = calloc(estimated_token_count, sizeof(astIndex))))
goto err;
tokenizer tok = tokenizer_init(source, len);
uint32_t tokens_len = 0;
for (; tokens_len <= estimated_token_count; tokens_len++) {
if (tokens_len == estimated_token_count) {
fprintf(stderr, "too many tokens, bump estimated_token_count\n");
goto err;
}
tokenizerToken token = tokenizer_next(&tok);
token_tags[tokens_len] = token.tag;
token_starts[tokens_len] = token.loc.start;
}
uint32_t estimated_node_count = (tokens_len + 2) / 2;
if (!(nodes_tags = calloc(estimated_node_count, sizeof(astNodeTag))))
goto err;
if (!(main_tokens = calloc(estimated_node_count, sizeof(astTokenIndex))))
goto err;
if (!(nodes_datas = calloc(estimated_node_count, sizeof(astData))))
goto err;
if (!(extra_data_arr = calloc(N, sizeof(astNodeIndex))))
goto err;
if (!(scratch_arr = calloc(N, sizeof(astNodeIndex))))
goto err;
parser p = (parser) {
.source = source,
.source_len = len,
.token_tags = token_tags,
.token_starts = token_starts,
.tokens_len = tokens_len,
.tok_i = 0,
.nodes = (astNodeList) {
.len = 0,
.cap = estimated_node_count,
.tags = nodes_tags,
.main_tokens = main_tokens,
.datas = nodes_datas,
},
.extra_data = (parserNodeIndexSlice) { .len = 0, .cap = N, .arr = extra_data_arr },
.scratch = (parserNodeIndexSlice) { .len = 0, .cap = N, .arr = scratch_arr },
};
free(scratch_arr);
parse_root(&p);
return (ast) {
.source = source,
.tokens.tags = token_tags,
.tokens.starts = token_starts,
.nodes = p.nodes,
.extra_data = p.extra_data.arr,
.extra_data_len = p.extra_data.len,
};
err:
free(token_tags);
free(token_starts);
free(nodes_tags);
free(main_tokens);
free(nodes_datas);
free(extra_data_arr);
free(scratch_arr);
*err = 1;
return (ast) {};
}