#include #include #include #include #include static void expected(enum token_type type) { err("Expected token of type %d while creating tree!\n", type); } static void unexpected(struct ctx *ctx, struct token *token) { token_print(ctx, token); err("Unexpected token while creating tree!\n"); } static struct token *next(struct token *token, size_t i) { return token + i; } static struct token *parse_declaration(struct ctx *ctx, struct token *token) { if (next(token, 1)->type != TYPEDELIM) unexpected(ctx, next(token, 1)); if (next(token, 2)->type != PARAM) unexpected(ctx, next(token, 2)); // Search for equal sign struct token *iterator = token; while ((iterator = next(iterator, 1))) { if (iterator->type == EQUAL) break; if (iterator->type == EOL || iterator->type == END) expected(EQUAL); } struct node_declaration *node = malloc(sizeof(*node)); node->callee.type = token->string; node->callee.name = next(token, 2)->string; // Magic size_t diff = iterator - token - 3; assert(diff % 3 == 0); node->parameters = malloc((diff / 3 + 1) * sizeof(*node->parameters)); for (size_t i = 0; i < diff / 3; i++) { struct token *param = token + (i + 1) * 3; assert(param->type == TYPE); assert(next(param, 2)->type == PARAM); node->parameters[i].type = param->string; node->parameters[i].name = next(param, 2)->string; } return next(iterator, 1); } static struct token *parse(struct ctx *ctx, struct token *token) { switch (token->type) { case TYPE: return parse_declaration(ctx, token); case UNKNOWN: case TYPEDELIM: case PARAM: case IDENT: case OPERATOR: case LPAREN: case RPAREN: case EQUAL: case NEWLINE: case EOL: case END: default: unexpected(ctx, token); } return NULL; } struct tree *tree_create(void) { struct tree *tree = malloc(sizeof(*tree)); tree->node = NULL; return tree; } void tree_destroy(struct tree *tree) { // TODO: Destroy nodes free(tree); } void treeify(struct ctx *ctx) { struct token *token = &ctx->tokens[1]; while (token) { assert(token->type != UNKNOWN); if (token->type == NEWLINE || token->type == EOL) { token = next(token, 1); continue; } if (token->type == END) break; token = parse(ctx, token); } }