diff --git a/simpleparser.c b/simpleparser.c new file mode 100644 index 0000000..e4022a0 --- /dev/null +++ b/simpleparser.c @@ -0,0 +1,80 @@ +#include "lexer.h" +#include + +typedef struct { + char* left; + char* right; + symbols node; + size_t prec; +} ASTNode; + +typedef struct { + ASTNode* nodes; + size_t size; + size_t capacity; +} ASTTree; + +void tree_init(ASTTree* a){ + if (a->capacity == 0) a->capacity = 128; + a->nodes = malloc(sizeof(*a->nodes)*a->capacity); +} + +void construct_nodes(ASTTree* a, Token t){ + if (a->capacity == 0) tree_init(a); + if (a->size >= a->capacity) { + a->capacity *=2; + a->nodes = realloc(a->nodes, sizeof(*a->nodes)*a->capacity); + } + size_t nc = 0; + for (size_t i=0; i 0 && i < t.size - 1); + a->nodes[nc].node = TOKEN_PLUS; + a->nodes[nc].left = t.text[i-1]; + a->nodes[nc].right = t.text[i+1]; + // add precedense later for large tree construction + nc++; + break; + case TOKEN_MINUS: + assert(i > 0 && i < t.size - 1); + a->nodes[nc].node = TOKEN_MINUS; + a->nodes[nc].left = t.text[i-1]; + a->nodes[nc].right = t.text[i+1]; + nc++; + break; + case TOKEN_DIV: + assert(i > 0 && i < t.size - 1); + a->nodes[nc].node = TOKEN_DIV; + a->nodes[nc].left = t.text[i-1]; + a->nodes[nc].right = t.text[i+1]; + nc++; + break; + case TOKEN_MUL: + assert(i > 0 && i < t.size - 1); + a->nodes[nc].node = TOKEN_MUL; + a->nodes[nc].left = t.text[i-1]; + a->nodes[nc].right = t.text[i+1]; + nc++; + break; + default: + + break; + } + } + a->size = nc; +} + +int main(int argc, char** argv){ + Token tokens = tokenize_all("1+2 3-4 1/2 2*7"); //invalid syntax + ASTTree tree = {0}; + construct_nodes(&tree, tokens); + printf("node count: %zu\n", tree.size); + for (size_t i=0; i