-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcodegen.c
More file actions
270 lines (230 loc) · 8.84 KB
/
Copy pathcodegen.c
File metadata and controls
270 lines (230 loc) · 8.84 KB
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
#include "codegen.h"
#include <stdlib.h>
#include <string.h>
#include <time.h>
// ================== CODE GENERATOR CREATION ==================
CodeGenerator* codegen_create(const char* output_filename, OutputFormat format) {
CodeGenerator* codegen = malloc(sizeof(CodeGenerator));
if (!codegen) return NULL;
codegen->output_file = fopen(output_filename, "w");
if (!codegen->output_file) {
free(codegen);
return NULL;
}
codegen->format = format;
codegen->indent_level = 0;
codegen->had_error = false;
codegen->error_message[0] = '\0';
codegen->lines_generated = 0;
codegen->variables_declared = 0;
codegen->functions_generated = 0;
codegen->gen_start_time = (double)clock() / CLOCKS_PER_SEC;
return codegen;
}
void codegen_destroy(CodeGenerator* codegen) {
if (codegen) {
if (codegen->output_file) {
fclose(codegen->output_file);
}
free(codegen);
}
}
// ================== UTILITY FUNCTIONS ==================
// Forward declarations
static void generate_c_expression(CodeGenerator* codegen, const ASTNode* node);
static void emit_indent(CodeGenerator* codegen) {
for (int i = 0; i < codegen->indent_level; i++) {
fprintf(codegen->output_file, " ");
}
}
static void emit_line(CodeGenerator* codegen, const char* line) {
emit_indent(codegen);
fprintf(codegen->output_file, "%s\n", line);
codegen->lines_generated++;
}
static void codegen_error(CodeGenerator* codegen, const char* message) {
codegen->had_error = true;
strncpy(codegen->error_message, message, sizeof(codegen->error_message) - 1);
}
// ================== C CODE GENERATION ==================
static void generate_c_literal(CodeGenerator* codegen, const ASTNode* node) {
switch (node->data.literal.token_type) {
case TOKEN_INTEGER:
fprintf(codegen->output_file, "%lld", node->data.literal.value.int_value);
break;
case TOKEN_FLOAT:
fprintf(codegen->output_file, "%g", node->data.literal.value.float_value);
break;
case TOKEN_STRING:
fprintf(codegen->output_file, "\"%s\"", node->data.literal.value.string_value);
break;
case TOKEN_TRUE:
fprintf(codegen->output_file, "true");
break;
case TOKEN_FALSE:
fprintf(codegen->output_file, "false");
break;
case TOKEN_NULL:
fprintf(codegen->output_file, "NULL");
break;
default:
codegen_error(codegen, "Unknown literal type");
break;
}
}
static void generate_c_identifier(CodeGenerator* codegen, const ASTNode* node) {
fprintf(codegen->output_file, "%s", node->data.identifier.name);
}
static void generate_c_binary(CodeGenerator* codegen, const ASTNode* node) {
fprintf(codegen->output_file, "(");
generate_c_expression(codegen, node->data.binary.left);
switch (node->data.binary.operator) {
case TOKEN_PLUS: fprintf(codegen->output_file, " + "); break;
case TOKEN_MINUS: fprintf(codegen->output_file, " - "); break;
case TOKEN_MULTIPLY: fprintf(codegen->output_file, " * "); break;
case TOKEN_DIVIDE: fprintf(codegen->output_file, " / "); break;
case TOKEN_MODULO: fprintf(codegen->output_file, " %% "); break;
case TOKEN_EQUAL: fprintf(codegen->output_file, " == "); break;
case TOKEN_NOT_EQUAL: fprintf(codegen->output_file, " != "); break;
case TOKEN_LESS: fprintf(codegen->output_file, " < "); break;
case TOKEN_LESS_EQUAL: fprintf(codegen->output_file, " <= "); break;
case TOKEN_GREATER: fprintf(codegen->output_file, " > "); break;
case TOKEN_GREATER_EQUAL: fprintf(codegen->output_file, " >= "); break;
case TOKEN_AND: fprintf(codegen->output_file, " && "); break;
case TOKEN_OR: fprintf(codegen->output_file, " || "); break;
case TOKEN_ASSIGN: fprintf(codegen->output_file, " = "); break;
default:
codegen_error(codegen, "Unknown binary operator");
break;
}
generate_c_expression(codegen, node->data.binary.right);
fprintf(codegen->output_file, ")");
}
static void generate_c_expression(CodeGenerator* codegen, const ASTNode* node) {
if (!node) return;
switch (node->type) {
case AST_LITERAL:
generate_c_literal(codegen, node);
break;
case AST_IDENTIFIER:
generate_c_identifier(codegen, node);
break;
case AST_BINARY:
case AST_ASSIGNMENT:
generate_c_binary(codegen, node);
break;
default:
codegen_error(codegen, "Unknown expression type");
break;
}
}
static void generate_c_var_declaration(CodeGenerator* codegen, const ASTNode* node) {
emit_indent(codegen);
// Convert ShayLang types to C types
switch (node->data.var_decl.type) {
case TOKEN_INT:
fprintf(codegen->output_file, "int ");
break;
case TOKEN_FLOAT_KW:
fprintf(codegen->output_file, "double ");
break;
case TOKEN_STRING_KW:
fprintf(codegen->output_file, "char* ");
break;
case TOKEN_BOOL_KW:
fprintf(codegen->output_file, "bool ");
break;
default:
fprintf(codegen->output_file, "int ");
break;
}
fprintf(codegen->output_file, "%s", node->data.var_decl.name);
if (node->data.var_decl.initializer) {
fprintf(codegen->output_file, " = ");
generate_c_expression(codegen, node->data.var_decl.initializer);
}
fprintf(codegen->output_file, ";\n");
codegen->lines_generated++;
codegen->variables_declared++;
}
static void generate_c_statement(CodeGenerator* codegen, const ASTNode* node) {
if (!node) return;
switch (node->type) {
case AST_VAR_DECLARATION:
generate_c_var_declaration(codegen, node);
break;
case AST_EXPRESSION_STMT:
emit_indent(codegen);
generate_c_expression(codegen, node->data.binary.left);
fprintf(codegen->output_file, ";\n");
codegen->lines_generated++;
break;
case AST_RETURN_STMT:
emit_indent(codegen);
fprintf(codegen->output_file, "return");
if (node->data.return_stmt.value) {
fprintf(codegen->output_file, " ");
generate_c_expression(codegen, node->data.return_stmt.value);
}
fprintf(codegen->output_file, ";\n");
codegen->lines_generated++;
break;
default:
codegen_error(codegen, "Unknown statement type");
break;
}
}
static void generate_c_program(CodeGenerator* codegen, const ASTNode* node) {
// Generate C headers
emit_line(codegen, "#include <stdio.h>");
emit_line(codegen, "#include <stdlib.h>");
emit_line(codegen, "#include <stdbool.h>");
emit_line(codegen, "#include <string.h>");
emit_line(codegen, "");
emit_line(codegen, "int main() {");
codegen->indent_level++;
// Generate all statements
for (int i = 0; i < node->data.program.statement_count; i++) {
generate_c_statement(codegen, node->data.program.statements[i]);
}
// Add return 0 if no explicit return
emit_line(codegen, "return 0;");
codegen->indent_level--;
emit_line(codegen, "}");
}
// ================== MAIN CODE GENERATION FUNCTION ==================
bool codegen_generate(CodeGenerator* codegen, const ASTNode* ast) {
if (!codegen || !ast) return false;
switch (codegen->format) {
case OUTPUT_C:
generate_c_program(codegen, ast);
break;
case OUTPUT_JAVASCRIPT:
// Could implement JavaScript generation here
codegen_error(codegen, "JavaScript output not implemented yet");
return false;
case OUTPUT_PYTHON:
// Could implement Python generation here
codegen_error(codegen, "Python output not implemented yet");
return false;
case OUTPUT_BYTECODE:
// Could implement bytecode generation here
codegen_error(codegen, "Bytecode output not implemented yet");
return false;
}
return !codegen->had_error;
}
// ================== UTILITY FUNCTIONS ==================
bool codegen_has_error(const CodeGenerator* codegen) {
return codegen->had_error;
}
const char* codegen_get_error(const CodeGenerator* codegen) {
return codegen->error_message;
}
int codegen_get_lines_generated(const CodeGenerator* codegen) {
return codegen->lines_generated;
}
double codegen_get_generation_time(const CodeGenerator* codegen) {
double current_time = (double)clock() / CLOCKS_PER_SEC;
return current_time - codegen->gen_start_time;
}