diff --git a/frontend/cmd/assemble.c b/frontend/cmd/assemble.c index 4283510..b3cb34a 100644 --- a/frontend/cmd/assemble.c +++ b/frontend/cmd/assemble.c @@ -2,13 +2,13 @@ #include "cmd.h" #include -#include #include +#include #include -#include +#include #include #include -#include +#include #include enum { @@ -19,10 +19,13 @@ enum { static int assemble_file(const char *in_path, const char *out_path) { - FILE *in = fopen(in_path, "r"); - if (!in) { + enum ivy_status status = IVY_OK; + + struct ivy_file *src = NULL; + status = ivy_file_open(in_path, &src); + if (!src) { b_err("cannot open source file '%s'", in_path); - b_i("reason: %s", strerror(errno)); + b_i("reason: %s", ivy_status_to_string(status)); return -1; } @@ -34,24 +37,23 @@ static int assemble_file(const char *in_path, const char *out_path) } struct ivy_assembler *as = NULL; - enum ivy_status status = ivy_assembler_create(out, &as); + status = ivy_assembler_create(out, &as); if (status != IVY_OK) { b_err("failed to initialise Ivy assembler"); b_i("reason: %s", ivy_status_to_string(status)); - fclose(in); + ivy_file_close(src); fclose(out); return -1; } - struct ivy_file *src = ivy_file_from_fp(in); struct ivy_asm_lexer *lex; status = ivy_asm_lexer_create(&lex); if (status != IVY_OK) { b_err("failed to initialise Ivy assembly lexer"); b_i("reason: %s", ivy_status_to_string(status)); - + ivy_file_close(src); fclose(out); ivy_assembler_finish(as); @@ -101,7 +103,7 @@ static int assemble_file(const char *in_path, const char *out_path) } int r = (status == IVY_OK || status == IVY_ERR_EOF) ? 0 : -1; - + ivy_asm_parser_destroy(parser); ivy_assembler_finish(as); ivy_file_close(src); @@ -120,7 +122,7 @@ static b_string *get_source_filename(const char *src_path) continue; } - char s[] = { src_path[i], 0 }; + char s[] = {src_path[i], 0}; b_string_append_cstr(name, s); } @@ -140,15 +142,15 @@ static b_string *generate_object_filename(const char *src_filename) if (src_filename[i] == '.') { break; } - - char s[] = { src_filename[i], 0 }; + + char s[] = {src_filename[i], 0}; b_string_append_cstr(name, s); } if (b_string_get_size(name, B_STRLEN_NORMAL) == 0) { b_string_append_cstr(name, "out"); } - + b_string_append_cstr(name, ".io"); return name; } @@ -161,12 +163,13 @@ static int assemble(const b_command *cmd, const b_arglist *args, const b_array * b_string *in_name = NULL; b_string *out_name = NULL; - b_arglist_get_string(args, B_COMMAND_INVALID_ID, ARG_SOURCE_FILE, 0, &in_path); + b_arglist_get_string( + args, B_COMMAND_INVALID_ID, ARG_SOURCE_FILE, 0, &in_path); if (!in_path) { b_err("no source file specified."); return -1; } - + in_name = get_source_filename(in_path); if (!in_path) { b_err("source filepath is not a file."); @@ -231,7 +234,8 @@ B_COMMAND(CMD_ASSEMBLE, CMD_ROOT) B_COMMAND_NAME("assemble"); B_COMMAND_SHORT_NAME('A'); B_COMMAND_DESC( - "assemble one or more Ivy assembly source files into Ivy object files."); + "assemble one or more Ivy assembly source files into Ivy " + "object files."); B_COMMAND_FLAGS(B_COMMAND_SHOW_HELP_BY_DEFAULT); B_COMMAND_FUNCTION(assemble); @@ -247,7 +251,8 @@ B_COMMAND(CMD_ASSEMBLE, CMD_ROOT) B_OPTION_SHORT_NAME('o'); B_OPTION_LONG_NAME("out"); B_OPTION_DESC("the path to write the output binary file to."); - B_OPTION_ARG(ARG_OUT_FILE) { + B_OPTION_ARG(ARG_OUT_FILE) + { B_ARG_NAME("path"); B_ARG_NR_VALUES(1); } diff --git a/frontend/cmd/compile.c b/frontend/cmd/compile.c index ea161c8..dc2db36 100644 --- a/frontend/cmd/compile.c +++ b/frontend/cmd/compile.c @@ -4,9 +4,11 @@ #include #include #include +#include #include #include #include +#include #include #include #include @@ -52,15 +54,30 @@ static int compile_file(const char *path, const b_arglist *args) > 0; bool show_ir = b_arglist_get_count(args, OPT_SHOW_IR, B_COMMAND_INVALID_ID) > 0; + enum ivy_status status = IVY_OK; - FILE *fp = fopen(path, "r"); - if (!fp) { + struct ivy_file *src = NULL; + status = ivy_file_open(path, &src); + if (!src) { b_err("cannot open source file '%s'", path); - b_i("reason: %s", strerror(errno)); + b_i("reason: %s", ivy_status_to_string(status)); return -1; } - struct ivy_file *src = ivy_file_from_fp(fp); + struct ivy_diag_stream diag_stream; + ivy_diag_stream_init_tty(&diag_stream, b_stdtty); + + struct ivy_diag_ctx *diag = NULL; + status = ivy_diag_ctx_create(&diag); + if (status != IVY_OK) { + b_err("failed to initialise Ivy diagnostic context"); + ivy_file_close(src); + return -1; + } + + ivy_lang_diag_ctx_init(diag); + ivy_diag_ctx_set_line_source(diag, &src->f_base); + struct ivy_lexer *lex = NULL; if (ivy_lexer_create(&lex) != IVY_OK) { b_err("failed to initialise Ivy lexer"); @@ -69,7 +86,7 @@ static int compile_file(const char *path, const b_arglist *args) } ivy_lexer_set_source(lex, &src->f_base); - enum ivy_status status = IVY_OK; + ivy_lexer_set_diag_ctx(lex, diag); struct ivy_parser *parser = NULL; status = ivy_parser_create(&parser); @@ -102,9 +119,13 @@ static int compile_file(const char *path, const b_arglist *args) } if (status != IVY_OK) { + ivy_diag_ctx_write( + diag, IVY_DIAG_FORMAT_PRETTY, &diag_stream); +#if 0 b_err("failed to parse '%s'", path); b_i("reason: lex error (%s)", ivy_status_to_string(ivy_lexer_get_status(lex))); +#endif break; }