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
|
typedef struct {
FILE *outc;
unsigned long ident_counter;
ParsedFile *file;
Block *block;
} CGenerator;
static void cgen_create(CGenerator *g, FILE *out) {
g->outc = out;
g->ident_counter = 0;
}
static void cgen_block_enter(CGenerator *g, Block *b) {
g->block = b;
Statement *stmts;
if (b == NULL) {
stmts = g->file->stmts;
} else {
stmts = b->stmts;
}
block_enter(b, stmts);
}
static void cgen_block_exit(CGenerator *g, Block *into) {
Block *b = g->block;
Statement *stmts;
if (b == NULL) {
stmts = g->file->stmts;
} else {
stmts = b->stmts;
}
block_exit(b, stmts);
g->block = into;
}
static inline FILE *cgen_writing_to(CGenerator *g) {
return g->outc; /* for now */
}
static void cgen_write(CGenerator *g, const char *fmt, ...) {
va_list args;
va_start(args, fmt);
vfprintf(cgen_writing_to(g), fmt, args);
va_end(args);
}
static bool cgen_type_post(CGenerator *g, Type *t, Location where);
static bool cgen_type_pre(CGenerator *g, Type *t, Location where) {
switch (t->kind) {
case TYPE_BUILTIN:
switch (t->builtin) {
case BUILTIN_I8: cgen_write(g, "i8"); break;
case BUILTIN_I16: cgen_write(g, "i16"); break;
case BUILTIN_I32: cgen_write(g, "i32"); break;
case BUILTIN_I64: cgen_write(g, "i64"); break;
case BUILTIN_U8: cgen_write(g, "u8"); break;
case BUILTIN_U16: cgen_write(g, "u16"); break;
case BUILTIN_U32: cgen_write(g, "u32"); break;
case BUILTIN_U64: cgen_write(g, "u64"); break;
case BUILTIN_CHAR: cgen_write(g, "char"); break;
case BUILTIN_BOOL: cgen_write(g, "bool"); break;
case BUILTIN_F32: cgen_write(g, "f32"); break;
case BUILTIN_F64: cgen_write(g, "f64"); break;
}
case TYPE_VOID: cgen_write(g, "void"); break;
case TYPE_UNKNOWN:
err_print(t->where, "Can't determine type.");
break;
}
}
static bool cgen_type_post(CGenerator *g, Type *t, Location where) {
}
static bool cgen_fn_header(CGenerator *g, FnExpr *f, Identifier name, long id) {
}
static bool cgen_decls_file(CGenerator *g, ParsedFile *f);
static bool cgen_file(CGenerator *g, ParsedFile *f) {
g->file = f;
cgen_write(g, "#include <stdint.h>\n"
"typedef int8_t i8;\n"
"typedef int16_t i16;\n"
"typedef int32_t i32;\n"
"typedef int64_t i64;\n"
"typedef uint8_t u8;\n"
"typedef uint16_t u16;\n"
"typedef uint32_t u32;\n"
"typedef uint64_t u64;\n"
"typedef float f32;\n"
"typedef double f64;\n"
"typedef unsigned char bool;\n"
"#define false ((bool)0)\n"
"#define true ((bool)1)\n\n\n");
cgen_block_enter(g, NULL);
cgen_decls_file(g, f);
cgen_write(g, "/* code */\n");
cgen_block_exit(g, NULL);
}
|