-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodegen.c
More file actions
126 lines (114 loc) · 3.61 KB
/
codegen.c
File metadata and controls
126 lines (114 loc) · 3.61 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
#include <stdio.h>
#include <string.h>
#include "codegen.h"
static void emit_expr(Expr* expr) {
if (!expr) return;
switch (expr->type) {
case EXPR_NUMBER:
printf("%d", expr->number);
break;
case EXPR_VAR:
printf("%s", expr->var);
break;
case EXPR_BINARY:
printf("(");
emit_expr(expr->binary.left);
if (expr->binary.op == 'F') {
printf(" / "); // Treat floor division same as integer division in C
} else {
printf(" %c ", expr->binary.op);
}
emit_expr(expr->binary.right);
printf(")");
break;
case EXPR_CALL:
if (strcmp(expr->call.name, "print") == 0) {
printf("printf(\"%%d\\n\", ");
for (int i = 0; i < expr->call.argc; i++) {
emit_expr(expr->call.args[i]);
if (i < expr->call.argc - 1) printf(", ");
}
printf(")");
} else {
printf("%s(", expr->call.name);
for (int i = 0; i < expr->call.argc; i++) {
emit_expr(expr->call.args[i]);
if (i < expr->call.argc - 1) printf(", ");
}
printf(")");
}
break;
}
}
static void emit_stmt(Statement* stmt) {
switch (stmt->type) {
case STMT_ASSIGN:
printf(" int %s = ", stmt->name);
emit_expr(stmt->expr);
printf(";\n");
break;
case STMT_RETURN:
printf(" return ");
emit_expr(stmt->expr);
printf(";\n");
break;
case STMT_EXPR:
printf(" ");
emit_expr(stmt->expr);
printf(";\n");
break;
}
}
static void emit_function(Function* func) {
int has_return = 0;
for (int i = 0; i < func->body_len; i++) {
if (func->body[i]->type == STMT_RETURN) {
has_return = 1;
break;
}
}
// Function declaration
printf("%s %s() {\n", has_return ? "int" : "void", func->name);
for (int i = 0; i < func->body_len; i++) {
emit_stmt(func->body[i]);
}
printf("}\n\n");
}
void generate_c_code(Program* prog) {
printf("#include <stdio.h>\n\n");
// Generate all functions except main (handle main specially)
Function* main_func = NULL;
for (int i = 0; i < prog->func_count; i++) {
if (strcmp(prog->functions[i]->name, "main") == 0) {
main_func = prog->functions[i];
} else {
emit_function(prog->functions[i]);
}
}
// Always generate a C main() function
printf("int main() {\n");
if (main_func) {
// If there's a SynC main() function, call it
for (int i = 0; i < main_func->body_len; i++) {
emit_stmt(main_func->body[i]);
}
} else if (prog->func_count > 0) {
// If no SynC main(), call the first function
Function* first_func = prog->functions[0];
// Check if first function returns something
int has_return = 0;
for (int i = 0; i < first_func->body_len; i++) {
if (first_func->body[i]->type == STMT_RETURN) {
has_return = 1;
break;
}
}
if (has_return) {
printf(" printf(\"%%d\\n\", %s());\n", first_func->name);
} else {
printf(" %s();\n", first_func->name);
}
}
printf(" return 0;\n");
printf("}\n");
}