making parser
This commit is contained in:
42
ast.c
42
ast.c
@@ -4,24 +4,26 @@
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "ast.h"
|
||||
#include "parse.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;
|
||||
|
||||
tokenizer_tag* token_tags = NULL;
|
||||
ast_index* token_starts = NULL;
|
||||
ast_node_tag* nodes_tags = NULL;
|
||||
ast_token_index* main_tokens = NULL;
|
||||
ast_data* nodes_datas = NULL;
|
||||
ast_node_index* extra_data_arr = NULL;
|
||||
ast_node_index* 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(tokenizer_tag))))
|
||||
if (!(token_tags = calloc(estimated_token_count, sizeof(tokenizerTag))))
|
||||
goto err;
|
||||
|
||||
if (!(token_starts = calloc(estimated_token_count, sizeof(ast_index))))
|
||||
if (!(token_starts = calloc(estimated_token_count, sizeof(astIndex))))
|
||||
goto err;
|
||||
|
||||
tokenizer tok = tokenizer_init(source, len);
|
||||
@@ -31,26 +33,26 @@ ast ast_parse(const char* source, const uint32_t len, int* err)
|
||||
fprintf(stderr, "too many tokens, bump estimated_token_count\n");
|
||||
goto err;
|
||||
}
|
||||
tokenizer_token token = tokenizer_next(&tok);
|
||||
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(ast_node_tag))))
|
||||
if (!(nodes_tags = calloc(estimated_node_count, sizeof(astNodeTag))))
|
||||
goto err;
|
||||
|
||||
if (!(main_tokens = calloc(estimated_node_count, sizeof(ast_token_index))))
|
||||
if (!(main_tokens = calloc(estimated_node_count, sizeof(astTokenIndex))))
|
||||
goto err;
|
||||
|
||||
if (!(nodes_datas = calloc(estimated_node_count, sizeof(ast_data))))
|
||||
if (!(nodes_datas = calloc(estimated_node_count, sizeof(astData))))
|
||||
goto err;
|
||||
|
||||
if (!(extra_data_arr = calloc(16, sizeof(ast_token_index))))
|
||||
if (!(extra_data_arr = calloc(N, sizeof(astNodeIndex))))
|
||||
goto err;
|
||||
|
||||
if (!(scratch_arr = calloc(16, sizeof(ast_token_index))))
|
||||
if (!(scratch_arr = calloc(N, sizeof(astNodeIndex))))
|
||||
goto err;
|
||||
|
||||
parser p = (parser) {
|
||||
@@ -60,20 +62,20 @@ ast ast_parse(const char* source, const uint32_t len, int* err)
|
||||
.token_starts = token_starts,
|
||||
.tokens_len = tokens_len,
|
||||
.tok_i = 0,
|
||||
.nodes = (ast_node_list) {
|
||||
.nodes = (astNodeList) {
|
||||
.len = 0,
|
||||
.cap = estimated_node_count,
|
||||
.tags = nodes_tags,
|
||||
.main_tokens = main_tokens,
|
||||
.datas = nodes_datas,
|
||||
},
|
||||
.extra_data = (parser_node_index_slice) { .len = 0, .cap = 16, .arr = extra_data_arr },
|
||||
.scratch = (parser_node_index_slice) { .len = 0, .cap = 16, .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);
|
||||
|
||||
// TODO work
|
||||
parse_root(&p);
|
||||
|
||||
return (ast) {
|
||||
.source = source,
|
||||
|
||||
Reference in New Issue
Block a user