From 227c94c2ea41997b5a51f85ecee32320286342d2 Mon Sep 17 00:00:00 2001 From: yenru0 Date: Thu, 20 Nov 2025 13:15:24 +0900 Subject: [PATCH] minor changes for --- include/ast_util.h | 3 ++ include/globals.h | 23 +++++++++++ src/ast_util.c | 96 ++++++++++++++++++++++++++++++++++++++++++++++ src/parse.c | 24 +++++++++++- 4 files changed, 144 insertions(+), 2 deletions(-) create mode 100644 include/ast_util.h create mode 100644 src/ast_util.c diff --git a/include/ast_util.h b/include/ast_util.h new file mode 100644 index 0000000..bc44b9e --- /dev/null +++ b/include/ast_util.h @@ -0,0 +1,3 @@ +#include "globals.h" + +ASTNode *ast_node_new(NodeKind kind, Token token); \ No newline at end of file diff --git a/include/globals.h b/include/globals.h index db5c8ec..dfd7fd0 100644 --- a/include/globals.h +++ b/include/globals.h @@ -6,6 +6,8 @@ * Token Definitions */ typedef enum { + PROGRAM, /* use for syntax */ + LBRACK, RBRACK, LCURLY, @@ -48,8 +50,29 @@ typedef struct { /** * AST Node Definitions */ + +typedef enum NodeKind { + NODE_PROGRAM, + NODE_DEFN, + NODE_TYPE, + NODE_EXPR, + NODE_ATOM, + NODE_STMT, + NODE_BLOCK, + NODE_LAMBDA, + NODE_COMPOUND, + NODE_PARAMS + + +} NodeKind; + typedef struct ASTNode { + NodeKind kind; + Token token; struct ASTNode **children; size_t child_count; + size_t capacity; + + } ASTNode; \ No newline at end of file diff --git a/src/ast_util.c b/src/ast_util.c new file mode 100644 index 0000000..cbf1da8 --- /dev/null +++ b/src/ast_util.c @@ -0,0 +1,96 @@ +#include "ast_util.h" + +#include + +ASTNode *ast_node_new(NodeKind kind, Token token) { + ASTNode *node = malloc(sizeof(ASTNode)); + if (node == NULL) { + return NULL; + } + + node->kind = kind; + node->token = token; + node->children = NULL; + node->capacity = 0; + node->child_count = 0; + + return node; +} + +ASTNode *ast_node_new_with_child(NodeKind kind, Token token, int child_count) { + ASTNode *node = ast_node_new(kind, token); + if (node == NULL) { + return NULL; + } + + node->child_count = child_count; + node->capacity = child_count; + node->children = calloc(child_count, sizeof(ASTNode *)); + if (node->children == NULL) { + free(node); + return NULL; + } + + return node; +} + +void ast_node_add_child(ASTNode *parent, ASTNode *child) { + if (parent->child_count >= parent->capacity) { + size_t new_capacity = parent->capacity == 0 ? 2 : parent->capacity * 2; + ASTNode **new_children = realloc(parent->children, new_capacity * sizeof(ASTNode *)); + if (new_children == NULL) { + return; + } + parent->children = new_children; + parent->capacity = new_capacity; + } + parent->children[parent->child_count++] = child; +} + +void ast_node_free(ASTNode *node) { + if (node == NULL) { + return; + } + + for (size_t i = 0; i < node->child_count; i++) { + ast_node_free(node->children[i]); + } + free(node->children); + free(node); +} + +ASTNode *ast_node_new_program() { + ASTNode *node = ast_node_new(NODE_PROGRAM, (Token) {PROGRAM, 0, {0, NULL}}); + return node; +} + +ASTNode *ast_node_new_defn(Token token, ASTNode *type_node, ASTNode *expr_node /* nullable */) { + + ASTNode *node; + + if (expr_node == NULL) { + node = ast_node_new_with_child(NODE_DEFN, token, 1); + } else { + node = ast_node_new_with_child(NODE_DEFN, token, 2); + } + + if (node == NULL) { + return NULL; + } + + node->children[0] = type_node; + if (expr_node != NULL) { + node->children[1] = expr_node; + } + + return node; +} + +ASTNode *ast_node_new_type_simple(Token token) { + ASTNode *node = ast_node_new(NODE_TYPE, token); +} + +ASTNode *ast_node_new_type_functional(Token token, ASTNode *type_node_from, ASTNode *type_node_to) { + ASTNode *node = ast_node_new_with_child( + NODE_TYPE, token, 2); +} diff --git a/src/parse.c b/src/parse.c index 726a158..80fe545 100644 --- a/src/parse.c +++ b/src/parse.c @@ -29,10 +29,30 @@ static void parser_expect(Parser *parser, TokenType type) { if (parser->current.type == type) { parser_next(parser); } else { - parser->flag_error = 1; + parser->flag_error = 1; // TODO: 실패했을 떄, 예외전파 } } /* IMPL. PARSER PARSE FUNCTIONS -*/ \ No newline at end of file +*/ + +ASTNode *parser_parse_defn(Parser *parser) { + parser_expect(parser, ID); + + Token id_token = parser->current; + + ASTNode *type_node = parser_parse_type(parser); + + Token id = parser->current; + parser_expect(parser, ID); + + ASTNode *expr = NULL; + if (parser->current.type != SEMI) { + expr = parser_parse_expr(parser); + } + + parser_expect(parser, SEMI); + + return NULL; +} \ No newline at end of file