-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathast.h
134 lines (102 loc) · 3.25 KB
/
ast.h
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
#ifndef AST_H
#define AST_H
struct ast_node;
struct ast_declaration;
struct ast_expression;
struct ast_statement_list;
struct ast_compound_statement;
struct ast_selection_statement;
struct ast_while_statement;
struct ast_translation_unit;
#define AST_NODE 1
#define AST_DECLARATION 2
#define AST_EXPRESSION 3
#define AST_STATEMENT_LIST 4
#define AST_COMPOUND_STATEMENT 5
#define AST_SELECTION_STATEMENT 6
#define AST_WHILE_STATEMENT 7
#define AST_TRANSLATION_UNIT 8
struct ast_node {
int tag;
};
struct ast_declaration {
int tag;
#define TYPE_INT 1
#define TYPE_FLOAT 2
int type_specifier;
const char *identifier;
};
struct ast_expression {
int tag;
#define AST_ADD 1 /* '+' */
#define AST_SUB 2 /* '-' */
#define AST_MUL 3 /* '*' */
#define AST_DIV 4 /* '/' */
#define AST_GT 5 /* '>' */
#define AST_LT 6 /* '<' */
#define AST_EQ 7 /* '==' */
#define AST_NE 8 /* '!=' */
#define AST_LE 9 /* '<=' */
#define AST_GE 10 /* '>=' */
#define AST_ASSIGN 11 /* '=' */
#define AST_INT_CONSTANT 12
#define AST_FLOAT_CONSTANT 13
#define AST_IDENTIFIER 14
int operator;
struct ast_expression *subexpr[2];
union {
int int_constant;
float float_constant;
const char *identifier;
} primary_expr;
};
struct ast_statement_list {
int tag;
struct ast_node **statement;
int number_of_statements;
};
struct ast_compound_statement {
int tag;
struct ast_statement_list *statement_list;
};
struct ast_selection_statement {
int tag;
/* if (condition) { then_body } else { else_body } */
struct ast_expression *condition;
struct ast_compound_statement *then_body;
struct ast_compound_statement *else_body;
};
struct ast_while_statement {
int tag;
/* while (condition) { body } */
struct ast_expression *condition;
struct ast_compound_statement *body;
};
struct ast_translation_unit {
int tag;
struct ast_statement_list *statement_list;
};
struct ast_declaration *
create_declaration(int type_specifier, const char *identifier);
struct ast_expression *
create_expression(int operation, struct ast_expression *lhs,
struct ast_expression *rhs);
struct ast_statement_list *
create_statement_list(struct ast_node *statement);
struct ast_statement_list *
statement_list_add_statement(struct ast_statement_list *statement_list,
struct ast_node *statement);
struct ast_compound_statement *
create_compound_statement(struct ast_statement_list *statement_list);
struct ast_selection_statement *
create_selection_statement(struct ast_expression *condition,
struct ast_compound_statement *then_body,
struct ast_compound_statement *else_body);
struct ast_while_statement *
create_while_statement(struct ast_expression *condition,
struct ast_compound_statement *body);
struct ast_translation_unit *
create_translation_unit(struct ast_statement_list *statement_list);
void
print_node(struct ast_node *node);
#endif /* AST_H */