diff options
-rw-r--r-- | main.c | 68 | ||||
-rw-r--r-- | parse.c | 40 | ||||
-rwxr-xr-x | rung | 2 | ||||
-rw-r--r-- | types.c | 4 |
4 files changed, 75 insertions, 39 deletions
@@ -8,7 +8,6 @@ /* @TODO: -compile to a temp file, then move it if compilation succeeds fix including something twice - just use the non-namespacey version if it exists or pick one namespace to use everywhere otherwise &void null @@ -109,6 +108,8 @@ int main(int argc, char **argv) { const char *in_filename = NULL; const char *out_filename = "out.c"; + + bool verbose = false; ErrCtx err_ctx = {0}; err_ctx.enabled = true; @@ -126,22 +127,28 @@ int main(int argc, char **argv) { err_ctx.color_enabled = default_color_enabled; for (int i = 1; i < argc; ++i) { - if ((i == 1 || argv[i-1][0] != '-') && argv[i][0] != '-') { - in_filename = argv[i]; - } else if (strs_equal(argv[i], "-no-color")) { + char *arg = argv[i]; + if (strs_equal(arg, "-no-color")) { err_ctx.color_enabled = false; - } else if (strs_equal(argv[i], "-color")) { + } else if (strs_equal(arg, "-color")) { err_ctx.color_enabled = true; - } else if (strs_equal(argv[i], "-o")) { + } else if (strs_equal(arg, "-o")) { if (i == argc-1) { fprintf(stderr, "-o cannot be the last argument to toc.\n"); return EXIT_FAILURE; } out_filename = argv[i+1]; ++i; + } else if (strs_equal(arg, "-v") || strs_equal(arg, "-verbose")) { + printf("Verbose mode enabled\n"); + verbose = true; } else { - fprintf(stderr, "Unrecognized option: %s.\n", argv[i]); - return EXIT_FAILURE; + if (arg[0] == '-') { + fprintf(stderr, "Unrecognized option: %s.\n", argv[i]); + return EXIT_FAILURE; + } else { + in_filename = arg; + } } } @@ -162,6 +169,7 @@ int main(int argc, char **argv) { Location file_where = {0}; file_where.file = &file; file.ctx = &err_ctx; + if (verbose) printf("Reading contents of %s...\n", in_filename); char *contents = read_file_contents(&main_allocr, in_filename, file_where); if (!contents) return EXIT_FAILURE; @@ -169,6 +177,7 @@ int main(int argc, char **argv) { idents_create(&globals, &main_allocr, NULL); Tokenizer t; file.contents = contents; + if (verbose) printf("Tokenizing...\n"); tokr_create(&t, &err_ctx, &main_allocr); if (!tokenize_file(&t, &file)) { err_text_important(&err_ctx, "Errors occured during preprocessing.\n"); @@ -176,15 +185,17 @@ int main(int argc, char **argv) { return EXIT_FAILURE; } -#if 0 - arr_foreach(t.tokens, Token, token) { - if (token != t.tokens) - printf(" "); - fprint_token(stdout, token); + if (verbose) { + printf("Tokens:\n"); + arr_foreach(t.tokens, Token, token) { + if (token != t.tokens) + printf(" "); + fprint_token(stdout, token); + } + printf("\n-----\n"); } - printf("\n"); -#endif - + + if (verbose) printf("Parsing...\n"); Parser p; parser_create(&p, &globals, &t, &main_allocr); ParsedFile f; @@ -193,9 +204,13 @@ int main(int argc, char **argv) { allocr_free_all(&main_allocr); return EXIT_FAILURE; } - /* fprint_parsed_file(stdout, &f); */ - /* printf("\n\n-----\n\n"); */ + if (verbose) { + printf("Parsed file:\n"); + fprint_parsed_file(stdout, &f); + printf("\n\n-----\n\n"); + } + if (verbose) printf("Determining types...\n"); Typer tr; Evaluator ev; @@ -207,10 +222,14 @@ int main(int argc, char **argv) { allocr_free_all(&main_allocr); return EXIT_FAILURE; } -#ifdef TOC_DEBUG - /* printf("\n\n"); */ - /* fprint_parsed_file(stdout, &f); */ -#endif + + if (verbose) { + printf("Typed file:\n"); + fprint_parsed_file(stdout, &f); + printf("\n\n-----\n\n"); + } + + if (verbose) printf("Opening output file...\n"); FILE *out = fopen(out_filename, "w"); if (!out) { err_text_important(&err_ctx, "Could not open output file: "); @@ -218,11 +237,14 @@ int main(int argc, char **argv) { allocr_free_all(&main_allocr); return EXIT_FAILURE; } + + if (verbose) printf("Generating C code...\n"); CGenerator g; cgen_create(&g, out, &globals, &main_allocr); cgen_file(&g, &f); - allocr_free_all(&main_allocr); + if (verbose) printf("Cleaning up...\n"); fclose(out); + allocr_free_all(&main_allocr); return 0; } @@ -302,7 +302,6 @@ static size_t type_to_str_(Type *t, char *buffer, size_t bufsize) { /* @TODO: improve this... we're gonna need expr_to_str ): */ return str_copy(buffer, bufsize, "<type expression>"); } - assert(0); return 0; } @@ -2607,16 +2606,20 @@ static void print_block_location(Block *b) { } static void fprint_fn_expr(FILE *out, FnExpr *f) { - fprintf(out, "fn ("); - arr_foreach(f->params, Declaration, decl) { - if (decl != f->params) - fprintf(out, ", "); - fprint_decl(out, decl); - } - fprintf(out, ") "); - fprint_type(out, &f->ret_type); - fprintf(out, " "); - fprint_block(out, &f->body); + if (f->flags & FN_EXPR_FOREIGN) { + fprintf(out, "#foreign fn;"); + } else { + fprintf(out, "fn ("); + arr_foreach(f->params, Declaration, decl) { + if (decl != f->params) + fprintf(out, ", "); + fprint_decl(out, decl); + } + fprintf(out, ") "); + fprint_type(out, &f->ret_type); + fprintf(out, " "); + fprint_block(out, &f->body); + } } static void fprint_args(FILE *out, Argument *args) { @@ -2678,8 +2681,12 @@ static void fprint_expr(FILE *out, Expression *e) { fprintf(out, "("); fprint_expr(out, e->binary.lhs); fprintf(out, ")%s(", binary_op_to_str(e->binary.op)); - if (e->binary.op == BINARY_DOT && found_type && e->binary.lhs->type.kind == TYPE_STRUCT) { - fprint_ident(out, e->binary.field->name); + Type *lhs_type = &e->binary.lhs->type; + if (lhs_type->kind == TYPE_PTR) { + lhs_type = lhs_type->ptr; + } + if (e->binary.op == BINARY_DOT && found_type && lhs_type->kind == TYPE_STRUCT) { + fprint_ident(out, e->binary.field->name); } else { fprint_expr(out, e->binary.rhs); } @@ -2884,12 +2891,14 @@ static void fprint_stmt(FILE *out, Statement *s) { fprintf(out, "#info "); break; } + fprint_expr(out, &m->text); + fprintf(out, ";\n"); } break; case STMT_BREAK: - fprintf(out, "break;"); + fprintf(out, "break;\n"); break; case STMT_CONT: - fprintf(out, "continue;"); + fprintf(out, "continue;\n"); break; case STMT_DEFER: fprintf(out, "defer "); @@ -2898,6 +2907,7 @@ static void fprint_stmt(FILE *out, Statement *s) { case STMT_USE: fprintf(out, "use "); fprint_expr(out, &s->use->expr); + fprintf(out, ";\n"); break; } } @@ -1,2 +1,2 @@ #!/bin/bash -gdb -tui toc +gdb toc @@ -2108,6 +2108,7 @@ static Status types_expr(Typer *tr, Expression *e) { e->binary.lhs = used; e->binary.rhs = typer_calloc(tr, 1, sizeof *e->binary.rhs); e->binary.rhs->kind = EXPR_IDENT; + e->binary.rhs->flags = 0; e->binary.rhs->ident_str.str = i_str; e->binary.rhs->ident_str.len = i_len; /* re-type */ @@ -3131,6 +3132,7 @@ static Status types_expr(Typer *tr, Expression *e) { return false; } rhs->kind = EXPR_IDENT; + rhs->flags = 0; rhs->ident_str.str = val.slice.data; rhs->ident_str.len = (size_t)val.slice.len; /* re-type with new expression */ @@ -3314,6 +3316,8 @@ static Status types_expr(Typer *tr, Expression *e) { free(s); return false; } + if (rhs->kind == EXPR_IDENT) + assert(!(rhs->flags & EXPR_FOUND_TYPE)); } break; } break; } break; |