summaryrefslogtreecommitdiffstats
path: root/src/ast.h
blob: 891d869fb42f8dccaffee8115a70e0c17501d6a1 (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#ifndef CCMMC_HEADER_AST_H
#define CCMMC_HEADER_AST_H

#define MAX_ARRAY_DIMENSION 7

typedef enum DATA_TYPE
{
    INT_TYPE,
    FLOAT_TYPE,
    VOID_TYPE,
    INT_PTR_TYPE,//for parameter passing
    FLOAT_PTR_TYPE,//for parameter passing
    CONST_STRING_TYPE,//for "const string"
    NONE_TYPE,//for nodes like PROGRAM_NODE which has no type
    ERROR_TYPE
} DATA_TYPE;

typedef enum IDENTIFIER_KIND
{
    NORMAL_ID, //function Name, uninitialized scalar variable
    ARRAY_ID, //ID_NODE->child = dim
    WITH_INIT_ID, //ID_NODE->child = initial value
} IDENTIFIER_KIND;

typedef enum BINARY_OPERATOR
{
    BINARY_OP_ADD,
    BINARY_OP_SUB,
    BINARY_OP_MUL,
    BINARY_OP_DIV,
    BINARY_OP_EQ,
    BINARY_OP_GE,
    BINARY_OP_LE,
    BINARY_OP_NE,
    BINARY_OP_GT,
    BINARY_OP_LT,
    BINARY_OP_AND,
    BINARY_OP_OR
} BINARY_OPERATOR;

typedef enum UNARY_OPERATOR
{
    UNARY_OP_POSITIVE,
    UNARY_OP_NEGATIVE,
    UNARY_OP_LOGICAL_NEGATION
} UNARY_OPERATOR;

//C_type= type of constant ex: 1, 3.3, "const string"
//do not modify, or lexer might break
typedef enum C_type {INTEGERC,FLOATC,STRINGC} C_type;

typedef enum STMT_KIND
{
    WHILE_STMT,
    FOR_STMT,
    ASSIGN_STMT, //TODO:for simpler implementation, assign_expr also uses this
    IF_STMT,
    FUNCTION_CALL_STMT,
    RETURN_STMT,
} STMT_KIND;

typedef enum EXPR_KIND
{
    BINARY_OPERATION,
    UNARY_OPERATION
} EXPR_KIND;

typedef enum DECL_KIND
{
    VARIABLE_DECL,
    TYPE_DECL,
    FUNCTION_DECL,
    FUNCTION_PARAMETER_DECL
} DECL_KIND;

typedef enum AST_TYPE
{
    PROGRAM_NODE,
    DECLARATION_NODE,
    IDENTIFIER_NODE,
    PARAM_LIST_NODE,
    NUL_NODE,
    BLOCK_NODE,
    VARIABLE_DECL_LIST_NODE,
    STMT_LIST_NODE,
    STMT_NODE,
    EXPR_NODE,
    CONST_VALUE_NODE, //ex:1, 2, "constant string"
    NONEMPTY_ASSIGN_EXPR_LIST_NODE,
    NONEMPTY_RELOP_EXPR_LIST_NODE
} AST_TYPE;

//*************************
// AST_NODE's semantic value
//*************************

typedef struct STMTSemanticValue
{
    STMT_KIND kind;
} STMTSemanticValue;

typedef struct EXPRSemanticValue
{
    EXPR_KIND kind;

    int isConstEval;

    union
    {
        int iValue;
        float fValue;
    } constEvalValue;

    union
    {
        BINARY_OPERATOR binaryOp;
        UNARY_OPERATOR unaryOp;
    } op;
} EXPRSemanticValue;

typedef struct DECLSemanticValue
{
    DECL_KIND kind;
} DECLSemanticValue;

struct SymbolAttribute;

typedef struct IdentifierSemanticValue
{
    char *identifierName;
    struct SymbolTableEntry *symbolTableEntry;
    IDENTIFIER_KIND kind;
} IdentifierSemanticValue;

typedef struct TypeSpecSemanticValue
{
    char *typeName;
} TypeSpecSemanticValue;

//don't modify or lexer may break
typedef struct CON_Type{
        C_type  const_type;
    union {
        int     intval;
        double  fval;
        char    *sc; }
        const_u;
} CON_Type;


struct AST_NODE {
    struct AST_NODE *child;
    struct AST_NODE *parent;
    struct AST_NODE *rightSibling;
    struct AST_NODE *leftmostSibling;
    AST_TYPE nodeType;
        DATA_TYPE dataType;
    int linenumber;
    union {
        IdentifierSemanticValue identifierSemanticValue;
        STMTSemanticValue stmtSemanticValue;
        DECLSemanticValue declSemanticValue;
        EXPRSemanticValue exprSemanticValue;
        CON_Type *const1;
    } semantic_value;
};
typedef struct AST_NODE AST_NODE;

AST_NODE *Allocate(AST_TYPE type);
AST_NODE* makeSibling(AST_NODE *a, AST_NODE *b);
AST_NODE* makeChild(AST_NODE *parent, AST_NODE *child);
AST_NODE* makeFamily(AST_NODE *parent, int childrenCount, ...);
AST_NODE* makeIDNode(char *lexeme, IDENTIFIER_KIND idKind);
AST_NODE* makeStmtNode(STMT_KIND stmtKind);
AST_NODE* makeDeclNode(DECL_KIND declKind);
AST_NODE* makeExprNode(EXPR_KIND exprKind, int operationEnumValue);
void semanticAnalysis(AST_NODE *root);

// Functions exported by draw.c
void printGV(AST_NODE *root, const char* fileName);

#endif