aboutsummaryrefslogtreecommitdiff
path: root/ast.h
blob: d438b922700a88c0bb4045d236f1a53c32f7fa5d (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
#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

typedef struct CNode {
    enum {
        /* Top Level */
        PROG,  FUNC_DEF, PARAMS, 
        DECL, /* declaration */
        DECLR, /* declarator */
        DECLRS, 
        INIT_DECLRS, INIT_DECLR, 
        INITR, /* initializer */
        TYPE_SPEC,
        STRUCT, UNION, 
        PLAIN_DECL, PLAIN_DECLR,

        /* Statments */
        EXP_STMT, /* expression-statment */
        COMP_STMT, IF_STMT, /* selection-statment */
        WHILE_STMT, FOR_STMT,
        CONT_STMT , BREAK_STMT, RET_STMT, /* 'continue', 'break', 'return' */

        /* Expressions (expressions use their token ID to denote their types */
        EXP,
        TYPE_NAME,
        ID, /* identifier */
        INT, /* INT_CONST */
        CHAR,
        STR
    } type;
    union {
        int intval;
        int subtype;
        char *strval;
    } rec;
    struct CNode *chd, *next;
    /* For error reporting */
    struct Location {
        int row, col;
    } loc;
} CNode;

CNode *cnode_create_exp(int exp_type, int pnum, ...);
CNode *cnode_create_type_spec(int spec_type, int pnum, ...);
CNode *cnode_append(CNode *node, CNode *tail);
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);
CNode *cnode_debug_print(CNode *ast);

extern CNode *ast_root;

#endif