diff options
author | Aryadev Chavali <aryadev@aryadevchavali.com> | 2023-10-29 16:58:58 +0000 |
---|---|---|
committer | Aryadev Chavali <aryadev@aryadevchavali.com> | 2023-10-29 16:59:31 +0000 |
commit | 157c79d53c4d04f6c6af1103a2db3a652b240edb (patch) | |
tree | ec6e535c94dcd65a2c5d39ae9361ca01b827722a /asm | |
parent | 597a45aa73d4d66fce4dd9135087e0e82f46899c (diff) | |
download | ovm-157c79d53c4d04f6c6af1103a2db3a652b240edb.tar.gz ovm-157c79d53c4d04f6c6af1103a2db3a652b240edb.tar.bz2 ovm-157c79d53c4d04f6c6af1103a2db3a652b240edb.zip |
Added a "usage" message and colours for assembler
Prints useful and pretty messages when verbose being at least 1.
Diffstat (limited to 'asm')
-rw-r--r-- | asm/main.c | 54 |
1 files changed, 41 insertions, 13 deletions
@@ -15,16 +15,37 @@ #include "./lexer.h" #include "./parser.h" -int main(void) +void usage(const char *program_name, FILE *fp) { - int ret = 0; - const char *filename = "main.asm"; - FILE *fp = fopen(filename, "rb"); - darr_t buffer = darr_read_file(fp); + fprintf(fp, + "Usage: %s FILE OUT-FILE\n" + "\tFILE: Source code to compile\n" + "\tOUT-FILE: Name of file to store bytecode\n", + program_name); +} + +int main(int argc, char *argv[]) +{ + int ret = 0; + char *source_file = ""; + char *out_file = ""; + if (argc < 3) + { + usage(argv[0], stderr); + return 1; + } + + source_file = argv[1]; + out_file = argv[2]; + FILE *fp = fopen(source_file, "rb"); + darr_t buffer = darr_read_file(fp); fclose(fp); token_stream_t tokens = tokenise_buffer(&buffer); - printf("%lu bytes -> %lu tokens\n", buffer.used, tokens.available); +#if VERBOSE >= 1 + printf("[%sTOKENISER%s]: %lu bytes -> %lu tokens\n", TERM_GREEN, TERM_RESET, + buffer.used, tokens.available); +#endif free(buffer.data); size_t number = 0; @@ -40,18 +61,25 @@ int main(void) column = t.column; line = t.line; } - fprintf(stderr, "%s:%lu:%lu: %s\n", filename, line, column, + fprintf(stderr, "%s:%lu:%lu: %s\n", source_file, line, column, perr_as_cstr(parse_error)); ret = 255 - parse_error; goto end; } - for (size_t i = 0; i < number; ++i) - { - inst_print(instructions[i], stdout); - puts(""); - } - // Free the tokens +#if VERBOSE >= 1 + printf("[%sPARSER%s]: %lu tokens -> %lu instructions\n", TERM_GREEN, + TERM_RESET, tokens.available, number); +#endif + + fp = fopen(out_file, "wb"); + insts_write_bytecode_file(instructions, number, fp); + fclose(fp); +#if VERBOSE >= 1 + printf("[%sCOMPILER%s]: Wrote bytecode to `%s`\n", TERM_GREEN, TERM_RESET, + out_file); +#endif end: + // Free the tokens and parsed data if (tokens.data) { for (size_t i = 0; i < tokens.available; ++i) |