| author | |
| committer | |
| log | 29f24e3c5066e7cb28876d40a811a4a64f9d4b33 |
| tree | 01d27aa3253da35af6fe8a5d3f69cffab08773bd |
| parent | 257cf09472ce5f4a51bf39808e119717fa0e4280 |
9 files changed, 43 insertions(+), 11 deletions(-)
src/codegen.cpp+7-3| ... | @@ -42,6 +42,10 @@ void codegen_set_verbose(CodeGen *g, bool verbose) { | ... | @@ -42,6 +42,10 @@ void codegen_set_verbose(CodeGen *g, bool verbose) { |
| 42 | g->verbose = verbose; | 42 | g->verbose = verbose; |
| 43 | } | 43 | } |
| 44 | 44 | ||
| 45 | void codegen_set_errmsg_color(CodeGen *g, ErrColor err_color) { | ||
| 46 | g->err_color = err_color; | ||
| 47 | } | ||
| 48 | |||
| 45 | void codegen_set_strip(CodeGen *g, bool strip) { | 49 | void codegen_set_strip(CodeGen *g, bool strip) { |
| 46 | g->strip_debug_symbols = strip; | 50 | g->strip_debug_symbols = strip; |
| 47 | } | 51 | } |
| ... | @@ -693,7 +697,7 @@ static ImportTableEntry *codegen_add_code(CodeGen *g, Buf *source_path, Buf *sou | ... | @@ -693,7 +697,7 @@ static ImportTableEntry *codegen_add_code(CodeGen *g, Buf *source_path, Buf *sou |
| 693 | err->source = source_code; | 697 | err->source = source_code; |
| 694 | err->line_offsets = tokenization.line_offsets; | 698 | err->line_offsets = tokenization.line_offsets; |
| 695 | 699 | ||
| 696 | print_err_msg(err); | 700 | print_err_msg(err, g->err_color); |
| 697 | exit(1); | 701 | exit(1); |
| 698 | } | 702 | } |
| 699 | 703 | ||
| ... | @@ -709,7 +713,7 @@ static ImportTableEntry *codegen_add_code(CodeGen *g, Buf *source_path, Buf *sou | ... | @@ -709,7 +713,7 @@ static ImportTableEntry *codegen_add_code(CodeGen *g, Buf *source_path, Buf *sou |
| 709 | import_entry->line_offsets = tokenization.line_offsets; | 713 | import_entry->line_offsets = tokenization.line_offsets; |
| 710 | import_entry->path = source_path; | 714 | import_entry->path = source_path; |
| 711 | import_entry->fn_table.init(32); | 715 | import_entry->fn_table.init(32); |
| 712 | import_entry->root = ast_parse(source_code, tokenization.tokens, import_entry); | 716 | import_entry->root = ast_parse(source_code, tokenization.tokens, import_entry, g->err_color); |
| 713 | assert(import_entry->root); | 717 | assert(import_entry->root); |
| 714 | if (g->verbose) { | 718 | if (g->verbose) { |
| 715 | ast_print(import_entry->root, 0); | 719 | ast_print(import_entry->root, 0); |
| ... | @@ -756,7 +760,7 @@ void codegen_add_root_code(CodeGen *g, Buf *source_path, Buf *source_code) { | ... | @@ -756,7 +760,7 @@ void codegen_add_root_code(CodeGen *g, Buf *source_path, Buf *source_code) { |
| 756 | } else { | 760 | } else { |
| 757 | for (int i = 0; i < g->errors.length; i += 1) { | 761 | for (int i = 0; i < g->errors.length; i += 1) { |
| 758 | ErrorMsg *err = g->errors.at(i); | 762 | ErrorMsg *err = g->errors.at(i); |
| 759 | print_err_msg(err); | 763 | print_err_msg(err, g->err_color); |
| 760 | } | 764 | } |
| 761 | exit(1); | 765 | exit(1); |
| 762 | } | 766 | } |
src/codegen.hpp+2-1| ... | @@ -9,6 +9,7 @@ | ... | @@ -9,6 +9,7 @@ |
| 9 | #define ZIG_CODEGEN_HPP | 9 | #define ZIG_CODEGEN_HPP |
| 10 | 10 | ||
| 11 | #include "parser.hpp" | 11 | #include "parser.hpp" |
| 12 | #include "errmsg.hpp" | ||
| 12 | 13 | ||
| 13 | struct CodeGen; | 14 | struct CodeGen; |
| 14 | 15 | ||
| ... | @@ -19,7 +20,6 @@ enum OutType { | ... | @@ -19,7 +20,6 @@ enum OutType { |
| 19 | OutTypeObj, | 20 | OutTypeObj, |
| 20 | }; | 21 | }; |
| 21 | 22 | ||
| 22 | |||
| 23 | CodeGen *codegen_create(Buf *root_source_dir); | 23 | CodeGen *codegen_create(Buf *root_source_dir); |
| 24 | 24 | ||
| 25 | enum CodeGenBuildType { | 25 | enum CodeGenBuildType { |
| ... | @@ -30,6 +30,7 @@ void codegen_set_build_type(CodeGen *codegen, CodeGenBuildType build_type); | ... | @@ -30,6 +30,7 @@ void codegen_set_build_type(CodeGen *codegen, CodeGenBuildType build_type); |
| 30 | void codegen_set_is_static(CodeGen *codegen, bool is_static); | 30 | void codegen_set_is_static(CodeGen *codegen, bool is_static); |
| 31 | void codegen_set_strip(CodeGen *codegen, bool strip); | 31 | void codegen_set_strip(CodeGen *codegen, bool strip); |
| 32 | void codegen_set_verbose(CodeGen *codegen, bool verbose); | 32 | void codegen_set_verbose(CodeGen *codegen, bool verbose); |
| 33 | void codegen_set_errmsg_color(CodeGen *codegen, ErrColor err_color); | ||
| 33 | void codegen_set_out_type(CodeGen *codegen, OutType out_type); | 34 | void codegen_set_out_type(CodeGen *codegen, OutType out_type); |
| 34 | void codegen_set_out_name(CodeGen *codegen, Buf *out_name); | 35 | void codegen_set_out_name(CodeGen *codegen, Buf *out_name); |
| 35 | 36 |
src/errmsg.cpp+2-2| ... | @@ -8,8 +8,8 @@ | ... | @@ -8,8 +8,8 @@ |
| 8 | #define GREEN "\x1b[32;1m" | 8 | #define GREEN "\x1b[32;1m" |
| 9 | #define RESET "\x1b[0m" | 9 | #define RESET "\x1b[0m" |
| 10 | 10 | ||
| 11 | void print_err_msg(ErrorMsg *err) { | 11 | void print_err_msg(ErrorMsg *err, ErrColor color) { |
| 12 | if (os_stderr_tty()) { | 12 | if (color == ErrColorOn || (color == ErrColorAuto && os_stderr_tty())) { |
| 13 | fprintf(stderr, WHITE "%s:%d:%d: " RED "error:" WHITE " %s" RESET "\n", | 13 | fprintf(stderr, WHITE "%s:%d:%d: " RED "error:" WHITE " %s" RESET "\n", |
| 14 | buf_ptr(err->path), | 14 | buf_ptr(err->path), |
| 15 | err->line_start + 1, err->column_start + 1, | 15 | err->line_start + 1, err->column_start + 1, |
src/errmsg.hpp+7-1| ... | @@ -11,6 +11,12 @@ | ... | @@ -11,6 +11,12 @@ |
| 11 | #include "buffer.hpp" | 11 | #include "buffer.hpp" |
| 12 | #include "list.hpp" | 12 | #include "list.hpp" |
| 13 | 13 | ||
| 14 | enum ErrColor { | ||
| 15 | ErrColorAuto, | ||
| 16 | ErrColorOff, | ||
| 17 | ErrColorOn, | ||
| 18 | }; | ||
| 19 | |||
| 14 | struct ErrorMsg { | 20 | struct ErrorMsg { |
| 15 | int line_start; | 21 | int line_start; |
| 16 | int column_start; | 22 | int column_start; |
| ... | @@ -22,6 +28,6 @@ struct ErrorMsg { | ... | @@ -22,6 +28,6 @@ struct ErrorMsg { |
| 22 | ZigList<int> *line_offsets; | 28 | ZigList<int> *line_offsets; |
| 23 | }; | 29 | }; |
| 24 | 30 | ||
| 25 | void print_err_msg(ErrorMsg *msg); | 31 | void print_err_msg(ErrorMsg *msg, ErrColor color); |
| 26 | 32 | ||
| 27 | #endif | 33 | #endif |
src/main.cpp+16-1| ... | @@ -25,6 +25,7 @@ static int usage(const char *arg0) { | ... | @@ -25,6 +25,7 @@ static int usage(const char *arg0) { |
| 25 | " --name [name] override output name\n" | 25 | " --name [name] override output name\n" |
| 26 | " --output [file] override destination path\n" | 26 | " --output [file] override destination path\n" |
| 27 | " --verbose turn on compiler debug output\n" | 27 | " --verbose turn on compiler debug output\n" |
| 28 | " --color [auto|off|on] enable or disable colored error messages\n" | ||
| 28 | , arg0); | 29 | , arg0); |
| 29 | return EXIT_FAILURE; | 30 | return EXIT_FAILURE; |
| 30 | } | 31 | } |
| ... | @@ -43,6 +44,7 @@ struct Build { | ... | @@ -43,6 +44,7 @@ struct Build { |
| 43 | OutType out_type; | 44 | OutType out_type; |
| 44 | const char *out_name; | 45 | const char *out_name; |
| 45 | bool verbose; | 46 | bool verbose; |
| 47 | ErrColor color; | ||
| 46 | }; | 48 | }; |
| 47 | 49 | ||
| 48 | static int build(const char *arg0, Build *b) { | 50 | static int build(const char *arg0, Build *b) { |
| ... | @@ -73,6 +75,7 @@ static int build(const char *arg0, Build *b) { | ... | @@ -73,6 +75,7 @@ static int build(const char *arg0, Build *b) { |
| 73 | if (b->out_name) | 75 | if (b->out_name) |
| 74 | codegen_set_out_name(g, buf_create_from_str(b->out_name)); | 76 | codegen_set_out_name(g, buf_create_from_str(b->out_name)); |
| 75 | codegen_set_verbose(g, b->verbose); | 77 | codegen_set_verbose(g, b->verbose); |
| 78 | codegen_set_errmsg_color(g, b->color); | ||
| 76 | codegen_add_root_code(g, &root_source_name, &root_source_code); | 79 | codegen_add_root_code(g, &root_source_name, &root_source_code); |
| 77 | codegen_link(g, b->out_file); | 80 | codegen_link(g, b->out_file); |
| 78 | 81 | ||
| ... | @@ -106,7 +109,9 @@ int main(int argc, char **argv) { | ... | @@ -106,7 +109,9 @@ int main(int argc, char **argv) { |
| 106 | return usage(arg0); | 109 | return usage(arg0); |
| 107 | } else { | 110 | } else { |
| 108 | i += 1; | 111 | i += 1; |
| 109 | if (strcmp(arg, "--output") == 0) { | 112 | if (i >= argc) { |
| 113 | return usage(arg0); | ||
| 114 | } else if (strcmp(arg, "--output") == 0) { | ||
| 110 | b.out_file = argv[i]; | 115 | b.out_file = argv[i]; |
| 111 | } else if (strcmp(arg, "--export") == 0) { | 116 | } else if (strcmp(arg, "--export") == 0) { |
| 112 | if (strcmp(argv[i], "exe") == 0) { | 117 | if (strcmp(argv[i], "exe") == 0) { |
| ... | @@ -118,6 +123,16 @@ int main(int argc, char **argv) { | ... | @@ -118,6 +123,16 @@ int main(int argc, char **argv) { |
| 118 | } else { | 123 | } else { |
| 119 | return usage(arg0); | 124 | return usage(arg0); |
| 120 | } | 125 | } |
| 126 | } else if (strcmp(arg, "--color") == 0) { | ||
| 127 | if (strcmp(argv[i], "auto") == 0) { | ||
| 128 | b.color = ErrColorAuto; | ||
| 129 | } else if (strcmp(argv[i], "on") == 0) { | ||
| 130 | b.color = ErrColorOn; | ||
| 131 | } else if (strcmp(argv[i], "off") == 0) { | ||
| 132 | b.color = ErrColorOff; | ||
| 133 | } else { | ||
| 134 | return usage(arg0); | ||
| 135 | } | ||
| 121 | } else if (strcmp(arg, "--name") == 0) { | 136 | } else if (strcmp(arg, "--name") == 0) { |
| 122 | b.out_name = argv[i]; | 137 | b.out_name = argv[i]; |
| 123 | } else { | 138 | } else { |
src/parser.cpp+4-2| ... | @@ -242,6 +242,7 @@ struct ParseContext { | ... | @@ -242,6 +242,7 @@ struct ParseContext { |
| 242 | ZigList<Token> *tokens; | 242 | ZigList<Token> *tokens; |
| 243 | ZigList<AstNode *> *directive_list; | 243 | ZigList<AstNode *> *directive_list; |
| 244 | ImportTableEntry *owner; | 244 | ImportTableEntry *owner; |
| 245 | ErrColor err_color; | ||
| 245 | }; | 246 | }; |
| 246 | 247 | ||
| 247 | __attribute__ ((format (printf, 3, 4))) | 248 | __attribute__ ((format (printf, 3, 4))) |
| ... | @@ -262,7 +263,7 @@ static void ast_error(ParseContext *pc, Token *token, const char *format, ...) { | ... | @@ -262,7 +263,7 @@ static void ast_error(ParseContext *pc, Token *token, const char *format, ...) { |
| 262 | err->source = pc->owner->source_code; | 263 | err->source = pc->owner->source_code; |
| 263 | err->line_offsets = pc->owner->line_offsets; | 264 | err->line_offsets = pc->owner->line_offsets; |
| 264 | 265 | ||
| 265 | print_err_msg(err); | 266 | print_err_msg(err, pc->err_color); |
| 266 | exit(EXIT_FAILURE); | 267 | exit(EXIT_FAILURE); |
| 267 | } | 268 | } |
| 268 | 269 | ||
| ... | @@ -1334,8 +1335,9 @@ static AstNode *ast_parse_root(ParseContext *pc, int *token_index) { | ... | @@ -1334,8 +1335,9 @@ static AstNode *ast_parse_root(ParseContext *pc, int *token_index) { |
| 1334 | return node; | 1335 | return node; |
| 1335 | } | 1336 | } |
| 1336 | 1337 | ||
| 1337 | AstNode *ast_parse(Buf *buf, ZigList<Token> *tokens, ImportTableEntry *owner) { | 1338 | AstNode *ast_parse(Buf *buf, ZigList<Token> *tokens, ImportTableEntry *owner, ErrColor err_color) { |
| 1338 | ParseContext pc = {0}; | 1339 | ParseContext pc = {0}; |
| 1340 | pc.err_color = err_color; | ||
| 1339 | pc.owner = owner; | 1341 | pc.owner = owner; |
| 1340 | pc.buf = buf; | 1342 | pc.buf = buf; |
| 1341 | pc.tokens = tokens; | 1343 | pc.tokens = tokens; |
src/parser.hpp+2-1| ... | @@ -11,6 +11,7 @@ | ... | @@ -11,6 +11,7 @@ |
| 11 | #include "list.hpp" | 11 | #include "list.hpp" |
| 12 | #include "buffer.hpp" | 12 | #include "buffer.hpp" |
| 13 | #include "tokenizer.hpp" | 13 | #include "tokenizer.hpp" |
| 14 | #include "errmsg.hpp" | ||
| 14 | 15 | ||
| 15 | struct AstNode; | 16 | struct AstNode; |
| 16 | struct CodeGenNode; | 17 | struct CodeGenNode; |
| ... | @@ -199,7 +200,7 @@ void ast_token_error(Token *token, const char *format, ...); | ... | @@ -199,7 +200,7 @@ void ast_token_error(Token *token, const char *format, ...); |
| 199 | 200 | ||
| 200 | 201 | ||
| 201 | // This function is provided by generated code, generated by parsergen.cpp | 202 | // This function is provided by generated code, generated by parsergen.cpp |
| 202 | AstNode * ast_parse(Buf *buf, ZigList<Token> *tokens, ImportTableEntry *owner); | 203 | AstNode * ast_parse(Buf *buf, ZigList<Token> *tokens, ImportTableEntry *owner, ErrColor err_color); |
| 203 | 204 | ||
| 204 | const char *node_type_str(NodeType node_type); | 205 | const char *node_type_str(NodeType node_type); |
| 205 | 206 |
src/semantic_info.hpp+1| ... | @@ -89,6 +89,7 @@ struct CodeGen { | ... | @@ -89,6 +89,7 @@ struct CodeGen { |
| 89 | int version_minor; | 89 | int version_minor; |
| 90 | int version_patch; | 90 | int version_patch; |
| 91 | bool verbose; | 91 | bool verbose; |
| 92 | ErrColor err_color; | ||
| 92 | ImportTableEntry *root_import; | 93 | ImportTableEntry *root_import; |
| 93 | }; | 94 | }; |
| 94 | 95 |
test/run_tests.cpp+2| ... | @@ -48,6 +48,8 @@ static void add_simple_case(const char *case_name, const char *source, const cha | ... | @@ -48,6 +48,8 @@ static void add_simple_case(const char *case_name, const char *source, const cha |
| 48 | test_case->compiler_args.append("--release"); | 48 | test_case->compiler_args.append("--release"); |
| 49 | test_case->compiler_args.append("--strip"); | 49 | test_case->compiler_args.append("--strip"); |
| 50 | test_case->compiler_args.append("--verbose"); | 50 | test_case->compiler_args.append("--verbose"); |
| 51 | test_case->compiler_args.append("--color"); | ||
| 52 | test_case->compiler_args.append("on"); | ||
| 51 | 53 | ||
| 52 | test_cases.append(test_case); | 54 | test_cases.append(test_case); |
| 53 | } | 55 | } |