diff options
author | Leo Tenenbaum <pommicket@gmail.com> | 2019-10-09 21:59:10 -0400 |
---|---|---|
committer | Leo Tenenbaum <pommicket@gmail.com> | 2019-10-09 21:59:10 -0400 |
commit | 655f7a4976f42d419bf1111a177a0204eaaf1f8d (patch) | |
tree | fbefcdabc1dfb41fa38faeccd7de08009e1c2d64 | |
parent | 076ea231f9d33d20066916ac8aa8506e91d3f9ea (diff) |
a bit more cgen (basic fn headers)
-rw-r--r-- | allocator.c | 2 | ||||
-rwxr-xr-x | build.sh | 4 | ||||
-rw-r--r-- | cgen.c | 45 | ||||
-rw-r--r-- | decls_cgen.c | 9 | ||||
-rw-r--r-- | main.c | 2 | ||||
-rw-r--r-- | out.c | 23 |
6 files changed, 72 insertions, 13 deletions
diff --git a/allocator.c b/allocator.c index 69e18fb..14b1a40 100644 --- a/allocator.c +++ b/allocator.c @@ -1,4 +1,4 @@ -#define NO_ALLOCATOR 1 +#define NO_ALLOCATOR 1 /* useful for debugging; valgrind checks writing past the end of a malloc, but that won't work with an allocator */ /* number of bytes a page hold, not including the header */ #define PAGE_BYTES (16384 - sizeof(Page)) #define PAGE_MAX_ALIGNS (PAGE_BYTES / sizeof(max_align_t)) @@ -13,9 +13,9 @@ fi ADDITIONAL_FLAGS='-Wno-unused-function' if [[ $CC == "clang" ]]; then - WARNINGS='-Wall -Wextra -Wpedantic -Wshadow -Wimplicit-fallthrough' + WARNINGS='-Wall -Wextra -Wpedantic -Wshadow -Wimplicit-fallthrough -Wno-unused-parameter' else - WARNINGS='-Wall -Wextra -Wpedantic -Wshadow -Wno-pointer-to-int-cast' + WARNINGS='-Wall -Wextra -Wpedantic -Wshadow -Wno-pointer-to-int-cast -Wno-unused-parameter' fi DEBUG_FLAGS="-O0 -g3 $WARNINGS -std=c11 -DTOC_DEBUG" @@ -3,11 +3,13 @@ typedef struct { unsigned long ident_counter; ParsedFile *file; Block *block; + Identifier main_ident; } CGenerator; -static void cgen_create(CGenerator *g, FILE *out) { +static void cgen_create(CGenerator *g, FILE *out, Identifiers *ids) { g->outc = out; g->ident_counter = 0; + g->main_ident = ident_get(ids, "main"); } static void cgen_block_enter(CGenerator *g, Block *b) { @@ -44,6 +46,14 @@ static void cgen_write(CGenerator *g, const char *fmt, ...) { va_end(args); } +static void cgen_ident(CGenerator *g, Identifier i) { + if (i == g->main_ident) { + /* don't conflict with C's main! */ + cgen_write(g, "main__"); + } else { + fprint_ident(cgen_writing_to(g), i); + } +} static bool cgen_type_post(CGenerator *g, Type *t, Location where); static bool cgen_type_pre(CGenerator *g, Type *t, Location where) { @@ -62,18 +72,40 @@ static bool cgen_type_pre(CGenerator *g, Type *t, Location where) { case BUILTIN_BOOL: cgen_write(g, "bool"); break; case BUILTIN_F32: cgen_write(g, "f32"); break; case BUILTIN_F64: cgen_write(g, "f64"); break; - } + } break; case TYPE_VOID: cgen_write(g, "void"); break; case TYPE_UNKNOWN: err_print(t->where, "Can't determine type."); - break; + return false; } + return true; } static bool cgen_type_post(CGenerator *g, Type *t, Location where) { + return true; } -static bool cgen_fn_header(CGenerator *g, FnExpr *f, Identifier name, long id) { +static bool cgen_fn_header(CGenerator *g, FnExpr *f, Location where) { + if (!cgen_type_pre(g, &f->ret_type, where)) return false; + cgen_write(g, " "); + if (f->c.name) { + cgen_ident(g, f->c.name); + } else { + /* TODO */ + } + if (!cgen_type_post(g, &f->ret_type, where)) return false; + cgen_write(g, "("); + arr_foreach(f->params, Declaration, d) { + arr_foreach(d->idents, Identifier, i) { + if (!cgen_type_pre(g, &d->type, where)) + return false; + cgen_ident(g, *i); + if (!cgen_type_post(g, &d->type, where)) + return false; + } + } + cgen_write(g, ")"); + return true; } static bool cgen_decls_file(CGenerator *g, ParsedFile *f); @@ -95,7 +127,10 @@ static bool cgen_file(CGenerator *g, ParsedFile *f) { "#define false ((bool)0)\n" "#define true ((bool)1)\n\n\n"); cgen_block_enter(g, NULL); - cgen_decls_file(g, f); + if (!cgen_decls_file(g, f)) + return false; cgen_write(g, "/* code */\n"); cgen_block_exit(g, NULL); + cgen_write(g, "int main() {\n\tmain__();\n\treturn 0;\n}\n"); + return true; } diff --git a/decls_cgen.c b/decls_cgen.c index 881240c..5310981 100644 --- a/decls_cgen.c +++ b/decls_cgen.c @@ -1,21 +1,24 @@ static bool cgen_decls_expr(CGenerator *g, Expression *e) { + return true; } static bool cgen_decls_block(CGenerator *g, Block *b) { + return true; } static bool cgen_decls_decl(CGenerator *g, Declaration *d) { if ((d->flags & DECL_FLAG_HAS_EXPR) && d->expr.kind == EXPR_FN && arr_len(d->idents) == 1) { - d->expr.fn.name = d->idents[0]; - if (!cgen_fn_header(g, &d->expr.fn)) + d->expr.fn.c.name = d->idents[0]; + if (!cgen_fn_header(g, &d->expr.fn, d->where)) return false; - cgen_write(g, ";"); + cgen_write(g, ";\n"); if (!cgen_decls_block(g, &d->expr.fn.body)) return false; } else if (d->flags & DECL_FLAG_HAS_EXPR) { if (!cgen_decls_expr(g, &d->expr)) return false; } + return true; } static bool cgen_decls_stmt(CGenerator *g, Statement *s) { @@ -82,7 +82,7 @@ int main(int argc, char **argv) { return EXIT_FAILURE; } CGenerator g; - cgen_create(&g, out); + cgen_create(&g, out, &file_idents); cgen_file(&g, &f); tokr_free(&t); @@ -1,2 +1,23 @@ +#include <stdint.h> +typedef int8_t i8; +typedef int16_t i16; +typedef int32_t i32; +typedef int64_t i64; +typedef uint8_t u8; +typedef uint16_t u16; +typedef uint32_t u32; +typedef uint64_t u64; +typedef float f32; +typedef double f64; +typedef unsigned char bool; +#define false ((bool)0) +#define true ((bool)1) + + /* declarations */ -;/* code */ +void main__(); +/* code */ +int main() { + main__(); + return 0; +} |