rename types
This commit is contained in:
72
ast.c
72
ast.c
@@ -8,74 +8,74 @@
|
||||
|
||||
#define N 1024
|
||||
|
||||
ast ast_parse(const char* source, const uint32_t len, int* err)
|
||||
ast astParse(const char* source, const uint32_t len)
|
||||
{
|
||||
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;
|
||||
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_tags = calloc(estimated_token_count, sizeof(TokenizerTag))))
|
||||
exit(1);
|
||||
|
||||
if (!(token_starts = calloc(estimated_token_count, sizeof(astIndex))))
|
||||
goto err;
|
||||
if (!(token_starts = calloc(estimated_token_count, sizeof(AstIndex))))
|
||||
exit(1);
|
||||
|
||||
tokenizer tok = tokenizer_init(source, len);
|
||||
Tokenizer tok = tokenizerInit(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;
|
||||
exit(1);
|
||||
}
|
||||
tokenizerToken token = tokenizer_next(&tok);
|
||||
TokenizerToken token = tokenizerNext(&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 (!(nodes_tags = calloc(estimated_node_count, sizeof(AstNodeTag))))
|
||||
exit(1);
|
||||
|
||||
if (!(main_tokens = calloc(estimated_node_count, sizeof(astTokenIndex))))
|
||||
goto err;
|
||||
if (!(main_tokens = calloc(estimated_node_count, sizeof(AstTokenIndex))))
|
||||
exit(1);
|
||||
|
||||
if (!(nodes_datas = calloc(estimated_node_count, sizeof(astData))))
|
||||
goto err;
|
||||
if (!(nodes_datas = calloc(estimated_node_count, sizeof(AstData))))
|
||||
exit(1);
|
||||
|
||||
if (!(extra_data_arr = calloc(N, sizeof(astNodeIndex))))
|
||||
goto err;
|
||||
if (!(extra_data_arr = calloc(N, sizeof(AstNodeIndex))))
|
||||
exit(1);
|
||||
|
||||
if (!(scratch_arr = calloc(N, sizeof(astNodeIndex))))
|
||||
goto err;
|
||||
if (!(scratch_arr = calloc(N, sizeof(AstNodeIndex))))
|
||||
exit(1);
|
||||
|
||||
parser p = (parser) {
|
||||
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) {
|
||||
.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 },
|
||||
.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);
|
||||
parseRoot(&p);
|
||||
|
||||
return (ast) {
|
||||
.source = source,
|
||||
@@ -85,16 +85,4 @@ ast ast_parse(const char* source, const uint32_t len, int* err)
|
||||
.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) {};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user