77 lines
2.0 KiB
C
77 lines
2.0 KiB
C
#include <stdbool.h>
|
|
#include <stdint.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
#include "ast.h"
|
|
#include "common.h"
|
|
#include "parser.h"
|
|
|
|
#define N 1024
|
|
|
|
Ast astParse(const char* source, const uint32_t len) {
|
|
uint32_t estimated_token_count = len / 8;
|
|
|
|
// Initialize token list
|
|
AstTokenList tokens = {
|
|
.len = 0,
|
|
.cap = estimated_token_count,
|
|
.tags = ARR_INIT(TokenizerTag, estimated_token_count),
|
|
.starts = ARR_INIT(AstIndex, estimated_token_count)
|
|
};
|
|
|
|
// Tokenize
|
|
Tokenizer tok = tokenizerInit(source, len);
|
|
while (true) {
|
|
if (tokens.len >= tokens.cap) {
|
|
fprintf(stderr, "too many tokens, bump estimated_token_count\n");
|
|
exit(1);
|
|
}
|
|
TokenizerToken token = tokenizerNext(&tok);
|
|
tokens.tags[tokens.len] = token.tag;
|
|
tokens.starts[tokens.len] = token.loc.start;
|
|
tokens.len++;
|
|
if (token.tag == TOKENIZER_TAG_EOF)
|
|
break;
|
|
}
|
|
|
|
// Initialize node list
|
|
uint32_t estimated_node_count = (tokens.len + 2) / 2;
|
|
AstNodeList nodes = {
|
|
.len = 0,
|
|
.cap = estimated_node_count,
|
|
.tags = ARR_INIT(AstNodeTag, estimated_node_count),
|
|
.main_tokens = ARR_INIT(AstTokenIndex, estimated_node_count),
|
|
.datas = ARR_INIT(AstData, estimated_node_count)
|
|
};
|
|
|
|
// Initialize parser
|
|
Parser p = {
|
|
.source = source,
|
|
.source_len = len,
|
|
.token_tags = tokens.tags,
|
|
.token_starts = tokens.starts,
|
|
.tokens_len = tokens.len,
|
|
.tok_i = 0,
|
|
.nodes = nodes,
|
|
.extra_data = SLICE_INIT(AstNodeIndex, N),
|
|
.scratch = SLICE_INIT(AstNodeIndex, N)
|
|
};
|
|
|
|
free(p.scratch.arr); // Parser takes ownership
|
|
|
|
parseRoot(&p);
|
|
|
|
return (Ast) {
|
|
.source = source,
|
|
.source_len = len,
|
|
.tokens = tokens,
|
|
.nodes = p.nodes,
|
|
.extra_data = {
|
|
.len = p.extra_data.len,
|
|
.cap = p.extra_data.cap,
|
|
.arr = p.extra_data.arr,
|
|
},
|
|
};
|
|
}
|