summaryrefslogtreecommitdiffstats
path: root/src/ast.h
blob: 5c9fcdcee6c0cf1260cd6c108b9ab4237dc59e8c (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
183
184
185
186
187
188
189
190
191
192
193
194
#ifndef CCMMC_HEADER_AST_H
#define CCMMC_HEADER_AST_H

#include <stdbool.h>
#include <stddef.h>

typedef enum CcmmcAstNodeType_enum {
    CCMMC_AST_NODE_PROGRAM,
    CCMMC_AST_NODE_DECL,
    CCMMC_AST_NODE_ID,
    CCMMC_AST_NODE_PARAM_LIST,
    CCMMC_AST_NODE_NUL,
    CCMMC_AST_NODE_BLOCK,
    CCMMC_AST_NODE_VARIABLE_DECL_LIST,
    CCMMC_AST_NODE_STMT_LIST,
    CCMMC_AST_NODE_STMT,
    CCMMC_AST_NODE_EXPR,
    CCMMC_AST_NODE_CONST_VALUE, // ex: 1, 2, "constant string"
    CCMMC_AST_NODE_NONEMPTY_ASSIGN_EXPR_LIST,
    CCMMC_AST_NODE_NONEMPTY_RELOP_EXPR_LIST
} CcmmcAstNodeType;

typedef enum CcmmcAstValueType_enum {
    CCMMC_AST_VALUE_INT,
    CCMMC_AST_VALUE_FLOAT,
    CCMMC_AST_VALUE_VOID,
    CCMMC_AST_VALUE_INT_PTR, // for parameter passing
    CCMMC_AST_VALUE_FLOAT_PTR, // for parameter passing
    CCMMC_AST_VALUE_CONST_STRING, // for "const string"
    CCMMC_AST_VALUE_NONE, // for nodes like PROGRAM_NODE which has no type
    CCMMC_AST_VALUE_ERROR
} CcmmcAstValueType;

typedef enum CcmmcKindId_enum {
    CCMMC_KIND_ID_NORMAL, // function names, uninitialized scalar variables
    CCMMC_KIND_ID_ARRAY, // ID_NODE->child = dim
    CCMMC_KIND_ID_WITH_INIT, // ID_NODE->child = initializer
} CcmmcKindId;

typedef enum CcmmcKindOpBinary_enum {
    CCMMC_KIND_OP_BINARY_ADD,
    CCMMC_KIND_OP_BINARY_SUB,
    CCMMC_KIND_OP_BINARY_MUL,
    CCMMC_KIND_OP_BINARY_DIV,
    CCMMC_KIND_OP_BINARY_EQ,
    CCMMC_KIND_OP_BINARY_GE,
    CCMMC_KIND_OP_BINARY_LE,
    CCMMC_KIND_OP_BINARY_NE,
    CCMMC_KIND_OP_BINARY_GT,
    CCMMC_KIND_OP_BINARY_LT,
    CCMMC_KIND_OP_BINARY_AND,
    CCMMC_KIND_OP_BINARY_OR
} CcmmcKindOpBinary;

typedef enum CcmmcKindOpUnary_enum {
    CCMMC_KIND_OP_UNARY_POSITIVE,
    CCMMC_KIND_OP_UNARY_NEGATIVE,
    CCMMC_KIND_OP_UNARY_LOGICAL_NEGATION
} CcmmcKindOpUnary;

typedef enum CcmmcKindConst_enum {
    CCMMC_KIND_CONST_INT,
    CCMMC_KIND_CONST_FLOAT,
    CCMMC_KIND_CONST_STRING,
    CCMMC_KIND_CONST_ERROR
} CcmmcKindConst;

typedef enum CcmmcKindStmt_enum {
    CCMMC_KIND_STMT_WHILE,
    CCMMC_KIND_STMT_FOR,
    CCMMC_KIND_STMT_ASSIGN, // TODO: for simpler implementation, assign_expr also uses this
    CCMMC_KIND_STMT_IF,
    CCMMC_KIND_STMT_FUNCTION_CALL,
    CCMMC_KIND_STMT_RETURN,
} CcmmcKindStmt;

typedef enum CcmmcKindExpr_enum {
    CCMMC_KIND_EXPR_BINARY_OP,
    CCMMC_KIND_EXPR_UNARY_OP
} CcmmcKindExpr;

typedef enum CcmmcKindDecl_enum {
    CCMMC_KIND_DECL_VARIABLE,
    CCMMC_KIND_DECL_TYPE,
    CCMMC_KIND_DECL_FUNCTION,
    CCMMC_KIND_DECL_FUNCTION_PARAMETER
} CcmmcKindDecl;

typedef struct CcmmcValueStmt_struct {
    CcmmcKindStmt kind;
} CcmmcValueStmt;

typedef struct CcmmcValueExpr_struct {
    CcmmcKindExpr kind;
    bool is_const_eval;
    union {
        int const_int;
        float const_float;
    };
    union {
        CcmmcKindOpBinary op_binary;
        CcmmcKindOpUnary op_unary;
    };
} CcmmcValueExpr;

typedef struct CcmmcValueDecl_struct {
    CcmmcKindDecl kind;
} CcmmcValueDecl;

typedef struct CcmmcValueId_struct {
    CcmmcKindId kind;
    char *name;
    // struct SymbolTableEntry *symbolTableEntry;
} CcmmcValueId;

typedef struct CcmmcValueConst_struct {
    CcmmcKindConst kind;
    union {
        int const_int;
        float const_float;
        char *const_string;
    };
} CcmmcValueConst;

typedef struct CcmmcAst_struct {
    struct CcmmcAst_struct *parent;
    struct CcmmcAst_struct *child;
    struct CcmmcAst_struct *leftmost_sibling;
    struct CcmmcAst_struct *right_sibling;
    CcmmcAstNodeType type_node;
    CcmmcAstValueType type_value;
    size_t line_number;
    union {
        CcmmcValueId value_id;
        CcmmcValueStmt value_stmt;
        CcmmcValueDecl value_decl;
        CcmmcValueExpr value_expr;
        CcmmcValueConst value_const;
    };
} CcmmcAst;

static inline bool ccmmc_ast_expr_get_is_constant(CcmmcAst *expr_node) {
    return expr_node->value_expr.is_const_eval;
}
static inline void ccmmc_ast_expr_set_is_constant(CcmmcAst *expr_node, bool value) {
    expr_node->value_expr.is_const_eval = value;
}
static inline bool ccmmc_ast_expr_get_is_int(CcmmcAst *expr_node) {
    return expr_node->type_value == CCMMC_AST_VALUE_INT;
}
static inline void ccmmc_ast_expr_set_is_int(CcmmcAst *expr_node) {
    expr_node->type_value = CCMMC_AST_VALUE_INT;
}
static inline bool ccmmc_ast_expr_get_is_float(CcmmcAst *expr_node) {
    return expr_node->type_value == CCMMC_AST_VALUE_FLOAT;
}
static inline void ccmmc_ast_expr_set_is_float(CcmmcAst *expr_node) {
    expr_node->type_value = CCMMC_AST_VALUE_FLOAT;
}
static inline int ccmmc_ast_expr_get_int(CcmmcAst *expr_node) {
    return expr_node->value_expr.const_int;
}
static inline void ccmmc_ast_expr_set_int(CcmmcAst *expr_node, int value) {
    expr_node->value_expr.const_int = value;
}
static inline float ccmmc_ast_expr_get_float(CcmmcAst *expr_node) {
    return expr_node->value_expr.const_float;
}
static inline void ccmmc_ast_expr_set_float(CcmmcAst *expr_node, float value) {
    expr_node->value_expr.const_float = value;
}

CcmmcAst        *ccmmc_ast_new                      (CcmmcAstNodeType type_node,
                                                     size_t line_number);
CcmmcAst        *ccmmc_ast_new_id                   (char *lexeme,
                                                     CcmmcKindId kind,
                                                     size_t line_number);
CcmmcAst        *ccmmc_ast_new_stmt                 (CcmmcKindStmt kind,
                                                     size_t line_number);
CcmmcAst        *ccmmc_ast_new_decl                 (CcmmcKindDecl kind,
                                                     size_t line_number);
CcmmcAst        *ccmmc_ast_new_expr                 (CcmmcKindExpr kind,
                                                     int op_kind,
                                                     size_t line_number);
CcmmcAst        *ccmmc_ast_append_sibling           (CcmmcAst *node,
                                                     CcmmcAst *sibling);
CcmmcAst        *ccmmc_ast_append_child             (CcmmcAst *parent,
                                                     CcmmcAst *child);
CcmmcAst        *ccmmc_ast_append_children          (CcmmcAst *parent,
                                                     size_t children_count,
                                                     ...);

#endif
// vim: set sw=4 ts=4 sts=4 et: