minor implementation for parser

(simple type and defn)
This commit is contained in:
2025-11-23 22:07:04 +09:00
parent 3682559a56
commit d8c0b2a762
9 changed files with 476 additions and 13 deletions

36
include/ast_util.h Normal file
View File

@@ -0,0 +1,36 @@
#pragma once
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include "globals.h"
void ast_node_add_child(ASTNode *parent, ASTNode *child);
void ast_node_free(ASTNode *node);
void ast_node_print(ASTNode *node, int depth);
/*
NODE SPECIFIC FUNCTIONS
*/
ASTNode *ast_node_program();
ASTNode *ast_node_defn(Token tok_val, ASTNode *type, ASTNode *id, ASTNode *expr);
ASTNode *ast_node_type_simple(Token tok_id);
ASTNode *ast_node_type_complex(Token tok_bracket, ASTNode *type_param, ASTNode *type_out);
ASTNode *ast_node_type_param();
ASTNode *ast_node_type_out();
ASTNode *ast_node_type_star(Token tok_star);
ASTNode *ast_node_type_void();
ASTNode *ast_node_id(Token id);

View File

@@ -31,7 +31,14 @@ typedef enum {
ELSE,
EOF_TOKEN,
ERROR
ERROR,
// for parser use
PARSER_USE,
VOID,
} TokenType;
typedef struct {
@@ -48,8 +55,39 @@ typedef struct {
/**
* AST Node Definitions
*/
typedef enum {
NODE_PROGRAM,
NODE_DEFN,
NODE_TYPE_SIMPLE,
NODE_TYPE_COMPLEX,
NODE_TYPE_PARAM,
NODE_TYPE_OUT,
NODE_TYPE_STAR,
NODE_EXPR,
NODE_NUM,
NODE_ID,
NODE_PARAM_LIST,
NODE_PARAM,
NODE_LAMBDA,
NODE_COMPOUND,
NODE_STMT_RETURN,
NODE_STMT_EXPR,
} NodeKind;
typedef struct ASTNode {
NodeKind kind;
Token token;
struct ASTNode **children;
size_t child_count;
size_t count;
size_t capacity;
} ASTNode;

View File

@@ -1,8 +1,10 @@
#pragma once
#include <stdint.h>
#include <stdbool.h>
#include "globals.h"
#include "lex.h"
#include "ast_util.h"
typedef struct Parser {
Lexer *lexer;
@@ -17,10 +19,6 @@ Parser *parser_new(Lexer *lexer);
void parser_free(Parser *parser);
static void parser_next(Parser *parser);
static void parser_expect(Parser *parser, TokenType type);
/*
PARSER PARSE FUNCTIONS
*/
@@ -30,6 +28,10 @@ ASTNode *parser_parse_defn(Parser *parser);
ASTNode *parser_parse_type(Parser *parser);
ASTNode *parser_parse_type_simple(Parser *parser);
ASTNode *parser_parse_type_complex(Parser *parser);
ASTNode *parser_parse_expr(Parser *parser);
ASTNode *parser_parse_atom(Parser *parser);