-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
368 lines (313 loc) · 10.6 KB
/
main.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
// Simplified token types
typedef enum {
TOKEN_IDENTIFIER,
TOKEN_KEYWORD,
TOKEN_INTEGER,
TOKEN_OPERATOR,
TOKEN_PUNCTUATION,
TOKEN_EOF
} TokenType;
// Token structure
typedef struct {
TokenType type;
char* value;
int line;
int column;
} Token;
// Lexer structure
typedef struct {
char* source;
int position;
int line;
int column;
} Lexer;
// Abstract Syntax Tree (AST) node types
typedef enum {
AST_PROGRAM,
AST_FUNCTION_DECLARATION,
AST_RETURN_STATEMENT,
AST_INTEGER_LITERAL
} ASTNodeType;
// AST Node structure
typedef struct ASTNode {
ASTNodeType type;
union {
char* identifier;
int integer_value;
struct {
struct ASTNode* body;
char* name;
} function_decl;
struct {
struct ASTNode* return_value;
} return_stmt;
} data;
struct ASTNode* next;
} ASTNode;
// Function prototypes
Lexer* create_lexer(char* source);
void free_lexer(Lexer* lexer);
Token* tokenize(Lexer* lexer);
ASTNode* parse(Token* tokens);
void generate_code(ASTNode* ast, FILE* output);
void free_ast(ASTNode* node);
// Create a new lexer
Lexer* create_lexer(char* source) {
Lexer* lexer = malloc(sizeof(Lexer));
lexer->source = strdup(source);
lexer->position = 0;
lexer->line = 1;
lexer->column = 1;
return lexer;
}
// Free lexer memory
void free_lexer(Lexer* lexer) {
free(lexer->source);
free(lexer);
}
// Check if a character is a valid identifier start
int is_identifier_start(char c) {
return isalpha(c) || c == '_';
}
// Check if a character is a valid identifier continuation
int is_identifier_char(char c) {
return isalnum(c) || c == '_';
}
// Tokenization function
Token* tokenize(Lexer* lexer) {
// In a real compiler, this would be dynamically allocated
static Token tokens[1000];
int token_count = 0;
while (lexer->source[lexer->position] != '\0') {
char current = lexer->source[lexer->position];
// Skip whitespace
while (isspace(current)) {
if (current == '\n') {
lexer->line++;
lexer->column = 1;
} else {
lexer->column++;
}
lexer->position++;
current = lexer->source[lexer->position];
}
// Identifiers and keywords
if (is_identifier_start(current)) {
int start = lexer->position;
while (is_identifier_char(lexer->source[lexer->position])) {
lexer->position++;
}
int length = lexer->position - start;
char* value = malloc(length + 1);
strncpy(value, &lexer->source[start], length);
value[length] = '\0';
// Check if it's a keyword
TokenType type = strcmp(value, "int") == 0 ||
strcmp(value, "return") == 0 ?
TOKEN_KEYWORD : TOKEN_IDENTIFIER;
tokens[token_count].type = type;
tokens[token_count].value = value;
tokens[token_count].line = lexer->line;
tokens[token_count].column = lexer->column;
token_count++;
continue;
}
// Integer literals
if (isdigit(current)) {
int start = lexer->position;
while (isdigit(lexer->source[lexer->position])) {
lexer->position++;
}
int length = lexer->position - start;
char* value = malloc(length + 1);
strncpy(value, &lexer->source[start], length);
value[length] = '\0';
tokens[token_count].type = TOKEN_INTEGER;
tokens[token_count].value = value;
tokens[token_count].line = lexer->line;
tokens[token_count].column = lexer->column;
token_count++;
continue;
}
// Punctuation
if (current == '(' || current == ')' || current == '{' ||
current == '}' || current == ';') {
char* value = malloc(2);
value[0] = current;
value[1] = '\0';
tokens[token_count].type = TOKEN_PUNCTUATION;
tokens[token_count].value = value;
tokens[token_count].line = lexer->line;
tokens[token_count].column = lexer->column;
token_count++;
lexer->position++;
lexer->column++;
continue;
}
// If no match, error
printf("Unexpected character: %c at line %d, column %d\n",
current, lexer->line, lexer->column);
lexer->position++;
lexer->column++;
}
// End of file token
tokens[token_count].type = TOKEN_EOF;
tokens[token_count].value = NULL;
tokens[token_count].line = lexer->line;
tokens[token_count].column = lexer->column;
return tokens;
}
// Simple recursive descent parser
ASTNode* parse(Token* tokens) {
ASTNode* program = malloc(sizeof(ASTNode));
program->type = AST_PROGRAM;
program->next = NULL;
int i = 0;
// Look for function declaration
while (tokens[i].type != TOKEN_EOF) {
// Check for 'int' keyword
if (tokens[i].type == TOKEN_KEYWORD &&
strcmp(tokens[i].value, "int") == 0) {
i++;
// Check for function name
if (tokens[i].type == TOKEN_IDENTIFIER) {
ASTNode* function = malloc(sizeof(ASTNode));
function->type = AST_FUNCTION_DECLARATION;
function->data.function_decl.name = strdup(tokens[i].value);
i++;
// Check for opening parenthesis
if (tokens[i].type == TOKEN_PUNCTUATION &&
strcmp(tokens[i].value, "(") == 0) {
i++;
// Close parenthesis
if (tokens[i].type == TOKEN_PUNCTUATION &&
strcmp(tokens[i].value, ")") == 0) {
i++;
// Open brace
if (tokens[i].type == TOKEN_PUNCTUATION &&
strcmp(tokens[i].value, "{") == 0) {
i++;
// Look for return statement
if (tokens[i].type == TOKEN_KEYWORD &&
strcmp(tokens[i].value, "return") == 0) {
i++;
// Return value must be an integer
if (tokens[i].type == TOKEN_INTEGER) {
ASTNode* return_stmt = malloc(sizeof(ASTNode));
return_stmt->type = AST_RETURN_STATEMENT;
ASTNode* literal = malloc(sizeof(ASTNode));
literal->type = AST_INTEGER_LITERAL;
literal->data.integer_value = atoi(tokens[i].value);
return_stmt->data.return_stmt.return_value = literal;
function->data.function_decl.body = return_stmt;
i++;
}
}
// Close brace
if (tokens[i].type == TOKEN_PUNCTUATION &&
strcmp(tokens[i].value, "}") == 0) {
// Add function to program
function->next = program->next;
program->next = function;
}
}
}
}
}
}
i++;
}
return program;
}
// Code generation - output x86 assembly
void generate_code(ASTNode* ast, FILE* output) {
fprintf(output, ".global main\n");
fprintf(output, "main:\n");
// Traverse AST and generate code
ASTNode* current = ast->next;
while (current != NULL) {
if (current->type == AST_FUNCTION_DECLARATION) {
ASTNode* body = current->data.function_decl.body;
if (body && body->type == AST_RETURN_STATEMENT) {
ASTNode* return_value = body->data.return_stmt.return_value;
if (return_value->type == AST_INTEGER_LITERAL) {
// x86 assembly for returning a value
fprintf(output, " movl $%d, %%eax\n", return_value->data.integer_value);
fprintf(output, " ret\n");
}
}
}
current = current->next;
}
}
// Free AST memory
void free_ast(ASTNode* node) {
while (node != NULL) {
ASTNode* next = node->next;
// Free based on node type
switch (node->type) {
case AST_FUNCTION_DECLARATION:
free(node->data.function_decl.name);
// Free body nodes recursively
if (node->data.function_decl.body) {
free_ast(node->data.function_decl.body);
}
break;
case AST_RETURN_STATEMENT:
if (node->data.return_stmt.return_value) {
free(node->data.return_stmt.return_value);
}
break;
default:
break;
}
free(node);
node = next;
}
}
int main(int argc, char* argv[]) {
if (argc != 3) {
printf("Usage: %s <input_file> <output_file>\n", argv[0]);
return 1;
}
// Read input file
FILE* input_file = fopen(argv[1], "r");
if (!input_file) {
printf("Could not open input file\n");
return 1;
}
// Get file size
fseek(input_file, 0, SEEK_END);
long file_size = ftell(input_file);
rewind(input_file);
// Read file content
char* source_code = malloc(file_size + 1);
fread(source_code, 1, file_size, input_file);
source_code[file_size] = '\0';
fclose(input_file);
// Create lexer
Lexer* lexer = create_lexer(source_code);
// Tokenize
Token* tokens = tokenize(lexer);
// Parse
ASTNode* ast = parse(tokens);
// Open output file for assembly
FILE* output_file = fopen(argv[2], "w");
if (!output_file) {
printf("Could not open output file\n");
return 1;
}
// Generate code
generate_code(ast, output_file);
// Clean up
fclose(output_file);
free_ast(ast);
free_lexer(lexer);
free(source_code);
printf("Compilation successful\n");
return 0;
}