minor changes for
This commit is contained in:
3
include/ast_util.h
Normal file
3
include/ast_util.h
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
#include "globals.h"
|
||||||
|
|
||||||
|
ASTNode *ast_node_new(NodeKind kind, Token token);
|
||||||
@@ -6,6 +6,8 @@
|
|||||||
* Token Definitions
|
* Token Definitions
|
||||||
*/
|
*/
|
||||||
typedef enum {
|
typedef enum {
|
||||||
|
PROGRAM, /* use for syntax */
|
||||||
|
|
||||||
LBRACK,
|
LBRACK,
|
||||||
RBRACK,
|
RBRACK,
|
||||||
LCURLY,
|
LCURLY,
|
||||||
@@ -48,8 +50,29 @@ typedef struct {
|
|||||||
/**
|
/**
|
||||||
* AST Node Definitions
|
* 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 {
|
typedef struct ASTNode {
|
||||||
|
NodeKind kind;
|
||||||
|
|
||||||
Token token;
|
Token token;
|
||||||
struct ASTNode **children;
|
struct ASTNode **children;
|
||||||
size_t child_count;
|
size_t child_count;
|
||||||
|
size_t capacity;
|
||||||
|
|
||||||
|
|
||||||
} ASTNode;
|
} ASTNode;
|
||||||
96
src/ast_util.c
Normal file
96
src/ast_util.c
Normal file
@@ -0,0 +1,96 @@
|
|||||||
|
#include "ast_util.h"
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
24
src/parse.c
24
src/parse.c
@@ -29,10 +29,30 @@ static void parser_expect(Parser *parser, TokenType type) {
|
|||||||
if (parser->current.type == type) {
|
if (parser->current.type == type) {
|
||||||
parser_next(parser);
|
parser_next(parser);
|
||||||
} else {
|
} else {
|
||||||
parser->flag_error = 1;
|
parser->flag_error = 1; // TODO: 실패했을 떄, 예외전파
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
IMPL. PARSER PARSE FUNCTIONS
|
IMPL. PARSER PARSE FUNCTIONS
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user