aboutsummaryrefslogtreecommitdiff
path: root/ast.h
blob: 41aad77d2ac184f61f5a873c21aeeaf2fd2be528 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#ifndef AST_H
#define AST_H

#define EXP_POSTFIX     1024
#define POSTFIX_ARR     1025
#define POSTFIX_CALL    1026
#define POSTFIX_DOT     1027
#define POSTFIX_PTR     1028
#define EXP_CAST        1029
#define INITR_NORM      1030
#define INITR_ARR       1031
#define DECLR_FUNC      1032
#define DECLR_ARR       1033
#define STMT_EXP        1034
#define STMT_COMP       1035
#define STMT_IF         1036
#define STMT_WHILE      1037
#define STMT_FOR        1038
#define STMT_CONT       1039
#define STMT_BREAK      1040
#define STMT_RET        1041

typedef struct CNode {
    enum {
        /* Top Level */
        PROG,
        FUNC_DEF,
        DECL, /* declaration */
        DECLR, /* declarator */
        DECLRS,
        INIT_DECLR, 
        INIT_DECLRS,
        INITR, /* initializer */
        TYPE_SPEC,
        FIELD, /* struct-or-union field */
        PLAIN_DECL,
        DECLS,
        FUNCS,

        /* Statments */
        STMT,

        /* Expressions  */
        EXP,
        TYPE_NAME,
        ID, /* identifier */
        INT, /* INT_CONST */
        CHAR,
        STR,
        NOP,

        COMP_STMTS,
        COMP_DECLS,
        ARGS,
        PARAMS
    } type;
    union {
        int intval;
        int subtype;
        char *strval;
    } rec;
    struct CNode *chd, *next;
    /* For error reporting */
    struct Location {
        int row, col;
    } loc;
} CNode;

void cnode_init();
CNode *cnode_create_nop();
CNode *cnode_create_general(int type, int subtype, int pnum, va_list ap); 
CNode *cnode_list_append(CNode *list, CNode *tail);
CNode *cnode_list_wrap(int type, CNode *list);

CNode *cnode_create_exp(int exp_type, int pnum, ...);
CNode *cnode_create_type_spec(int spec_type, int pnum, ...);
CNode *cnode_create_declr(int declr_type, int pnum, ...);
CNode *cnode_create_stmt(int stmt_type, int pnum, ...);
CNode *cnode_create_initr(int initr_type, CNode *body);

CNode *cnode_create_decl(CNode *type, CNode *init_declrs);
CNode *cnode_create_func(CNode *type, CNode *plain_decl, CNode *params, CNode *stmt);
CNode *cnode_create_init_declr(CNode *declr, CNode *initr);
CNode *cnode_create_struct_field(CNode *type_spec, CNode *declrs);
CNode *cnode_create_plain_decl(CNode *type_spec, CNode *declr);

CNode *cnode_create_identifier(char *val);
CNode *cnode_create_int_const(int val);
CNode *cnode_create_char_const(int val);
CNode *cnode_create_str_const(char *val);

void cnode_debug_print(CNode *ast);

extern CNode *ast_root;
extern CNode *null;

#endif