-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecl.c
237 lines (198 loc) · 5.57 KB
/
decl.c
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#include "decl.h"
#include "debug.h"
#include "symtab.h"
#include "stmt.h"
#include "expr.h"
#include "datatype.h"
#include "llist_definitions.h"
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
static void decl_id(Scanner_t *scanner, Token_t *tok);
static ASTNode_t *decl_function(Scanner_t *scanner);
static void *args_decl(Scanner_t *scanner, LList_t *args_list);
int decl_current_func = DECL_NO_FUNC;
static void decl_id(Scanner_t *scanner, Token_t *tok)
{
scanner_scan(scanner, tok);
if (tok->type != TOK_ID)
{
debug_print(
SEV_ERROR,
"[DECL] Expected token TOK_ID, found %s", TokToString(*tok));
exit(1);
}
}
ASTNode_t *decl_declarations(Scanner_t *scanner)
{
ASTNode_t *root = NULL;
ASTNode_t *head = NULL;
ASTNode_t *current = NULL;
TokenType_e type;
bool need_to_flatten = false;
if (scanner == NULL)
return NULL;
while (1)
{
while (1)
{
type = scanner_cache_tok(scanner);
if (
type == TOK_SEMICOLON || type == TOK_EMPTY || type == TOK_LPAREN || type == TOK_EOF)
break;
}
if (type == TOK_EOF)
break;
switch (type)
{
case TOK_LPAREN:
current = decl_function(scanner);
break;
default:
current = decl_var(scanner);
need_to_flatten = true;
break;
}
if (root == NULL)
root = current;
if (head != NULL)
head->next = current;
head = current;
// Move the list tail to the last item in the var decl in case of any
// This will handle multiple var declarations in the same line
// int a, b, c;
if (need_to_flatten)
{
head = ast_flatten(head);
need_to_flatten = false;
}
}
return root;
}
// TODO: Make sure there is no duplicate argument name
// TODO: Create symbol table for each function
// TODO: Create a visualization code for SymbolTables
static ASTNode_t *decl_function(Scanner_t *scanner)
{
Token_t tok;
ASTNode_t *stmts;
ASTNode_t *func;
Datatype_t *return_type;
int symbol_index;
return_type = datatype_get_type(scanner);
decl_id(scanner, &tok);
symbol_index = symtab_add_global_symbol(
tok.value.str_value,
SYMBOL_FUNC,
return_type);
decl_current_func = symbol_index;
scanner_match(scanner, TOK_LPAREN);
args_decl(scanner, &((SymbolFunc_t *)symtab_get_symbol(symbol_index))->args);
scanner_match(scanner, TOK_RPAREN);
stmts = stmt_block(scanner);
decl_current_func = DECL_NO_FUNC;
func = ast_create_node(
AST_FUNC_DECL,
stmts,
NULL,
symbol_index);
func->expr_type = return_type;
return func;
}
ASTNode_t *decl_var(Scanner_t *scanner)
{
Datatype_t *var_type;
ASTNode_t *var_list_head = NULL;
ASTNode_t *var_list_tail = NULL;
ASTNode_t *current_var;
Token_t tok;
int symbol_index;
var_type = datatype_get_type(scanner);
do
{
decl_id(scanner, &tok);
symbol_index = symtab_add_global_symbol(
tok.value.str_value,
SYMBOL_VAR,
var_type);
current_var = ast_create_leaf_node(AST_VAR_DECL, symbol_index);
current_var->expr_type = var_type;
if (var_list_head == NULL)
var_list_head = current_var;
if (var_list_tail != NULL)
var_list_tail->next = current_var;
var_list_tail = current_var;
scanner_peek(scanner, &tok);
if (tok.type == TOK_ASSIGN)
{
scanner_scan(scanner, &tok);
current_var->left = expr_expression(scanner);
datatype_check_assign_expr_type(
symtab_get_symbol(symbol_index)->data_type,
current_var->left->expr_type);
scanner_peek(scanner, &tok);
}
} while (tok.type == TOK_COMMA);
scanner_match(scanner, TOK_SEMICOLON);
return var_list_head;
}
ASTNode_t *args(Scanner_t *scanner)
{
ASTNode_t *args = NULL;
ASTNode_t *args_head = NULL;
Token_t tok;
scanner_peek(scanner, &tok);
if (tok.type == TOK_RPAREN)
return NULL;
do
{
if (args == NULL)
args = expr_expression(scanner);
else
{
args->next = expr_expression(scanner);
args = args->next;
}
if (args_head == NULL)
args_head = args;
scanner_peek(scanner, &tok);
if (tok.type != TOK_COMMA)
break;
scanner_scan(scanner, &tok);
} while (1);
return args_head;
}
static void *args_decl(Scanner_t *scanner, LList_t *args_list)
{
Token_t tok;
Datatype_t *type;
while (true)
{
scanner_peek(scanner, &tok);
if (tok.type == TOK_RPAREN)
break;
type = datatype_get_type(scanner);
scanner_scan(scanner, &tok);
if (tok.type != TOK_ID)
{
debug_print(SEV_ERROR, "[DECL] Expected an identifier, found %s", TokToString(tok));
}
SymbolFuncArg_t *argument = (SymbolFuncArg_t *)malloc(sizeof(SymbolFuncArg_t));
argument->arg_name = strdup(tok.value.str_value);
argument->arg_type = type;
LList_SymbolFuncArg_append(args_list, argument);
scanner_peek(scanner, &tok);
if (tok.type == TOK_COMMA)
scanner_scan(scanner, &tok);
}
}
int args_count(ASTNode_t *args)
{
int i = 0;
while (args)
{
i++;
args = args->next;
}
return i;
}