-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathast.h
193 lines (159 loc) · 4.47 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
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
// AST definitions
#ifndef __ast_h__
#define __ast_h__
struct _CommandList {
struct _Command* cmd;
struct _CommandList* next;
};
struct _Command {
enum {
E_IF,
E_WHILE,
E_VAR,
E_ASG,
E_PRINT,
E_SCAN
} kind;
union {
struct _VarList* list;
struct _AsgList* asg_list;
struct _IFexpression* ifnext;
struct _WHILEexpression* whilenext;
struct _PRINTF_EXP* printnext;
struct _SCANF_EXP* scannext;
} content;
};
struct _IFexpression {
enum {
E_IF_EXPR,
E_IF_ELSE
} kind;
union {
struct {
struct _Expr* expr;
struct _CommandList* list;
} if_type;
struct {
struct _Expr* expr;
struct _CommandList* list;
struct _CommandList* else_list;
} if_else_type;
} content;
};
struct _WHILEexpression {
struct _Expr* expr;
struct _CommandList* list;
};
struct _DeclarationList {
int type;
enum {
E_ASSIGNMENT,
E_DECLARATION
} op;
char* name;
union {
struct _Expr* expression;
} asg;
struct _DeclarationList* next;
};
struct _AsgList {
char* name;
struct _Expr* expression;
struct _AsgList* next;
};
struct _VarList {
int type;
struct _DeclarationList* list;
};
struct _PRINTF_EXP {
struct _TYPES* string_of_types;
struct _PrintVarsList* vars;
};
struct _PrintVarsList {
char* name;
struct _PrintVarsList* next;
};
struct _SCANF_EXP {
struct _TYPES* string_of_types;
struct _ScanDeclarationList* vars;
};
struct _ScanDeclarationList {
char* declaration;
struct _ScanDeclarationList* next;
};
struct _TYPES {
char* types;
};
// AST for expressions
struct _Expr {
enum {
E_OPERATION,
E_VARIABLE,
E_NUM,
} kind;
enum {
E_EXPR_FLOAT,
E_EXPR_INT
} type;
union {
int numberint;
float numberfloat;
char* variable;
struct {
int operator;
struct _Expr* left;
struct _Expr* right;
} op;
} attr;
};
typedef struct _CommandList CommandList;
typedef struct _Command Command;
typedef struct _IFexpression IFexpression;
typedef struct _WHILEexpression WHILEexpression;
typedef struct _DeclarationList DeclarationList;
typedef struct _PRINTF_EXP PRINTF_EXP;
typedef struct _PrintVarsList PrintVarsList;
typedef struct _SCANF_EXP SCANF_EXP;
typedef struct _Expr Expr;
typedef struct _ScanDeclarationList ScanDeclarationList;
typedef struct _VarList varList;
typedef struct _TYPES TYPES_STR;
typedef struct _NUMBER NUMBER;
typedef struct _AsgList AsgList;
//------- Command list -----------------
CommandList* ast_commandList(Command* cmd, CommandList* next);
//------- Command functions -------------
Command* if_declaration(IFexpression* ifnext);
Command* while_declaration(WHILEexpression* whilenext);
Command* variable_declaration(varList* list);
Command* printf_declaration(PRINTF_EXP* printnext);
Command* scanf_declaration(SCANF_EXP* scannext);
Command* assignment_declaration(AsgList* asg_list);
//------- IF expressions ----------------
IFexpression* if_command(Expr* expr, Command* cmd);
IFexpression* if_command_else_command(Expr* expr, Command* cmd, Command* else_cmd);
IFexpression* if_commands(Expr* expr, CommandList* list);
IFexpression* if_commands_else_command(Expr* expr, CommandList* list, Command* else_cmd);
IFexpression* if_commands_else_commands(Expr* expr, CommandList* list, CommandList* else_list);
IFexpression* if_command_else_commands(Expr* expr, Command* cmd, CommandList* else_list);
//------- WHILE expressions ----------------
WHILEexpression* while_command(Expr* expr, Command* cmd);
WHILEexpression* while_commands(Expr* expr, CommandList* list);
//------- INPUT/OUTPUT expressions ----------------
TYPES_STR* ast_string_of_types(char* type);
PRINTF_EXP* ast_printf(TYPES_STR* types, PrintVarsList* vars);
SCANF_EXP* ast_scanf(TYPES_STR* type, ScanDeclarationList* vars);
ScanDeclarationList* ast_scanlist(char* var, ScanDeclarationList* next);
PrintVarsList* ast_printlist(char* var, PrintVarsList* next);
//------- Declarations/Assignments expressions ----------------
varList* ast_varlist(int type, DeclarationList* next);
DeclarationList* ast_declaration(char* name, DeclarationList* next);
DeclarationList* ast_assignment(char* name, Expr* expression, DeclarationList* next);
AsgList* ast_assignmentList(char* name, Expr* expression, AsgList* next);
//------- Expressions functions -------------
Expr* ast_integer(int v);
Expr* ast_float(float v);
Expr* ast_variable_float(char* v);
Expr* ast_variable_int(char* v);
Expr* ast_operation(int operator, Expr* left, Expr* right);
#endif