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 | |
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.
-rw-r--r-- | asm/main.c | 54 | ||||
-rw-r--r-- | lib/base.h | 4 | ||||
-rw-r--r-- | vm/main.c | 9 |
3 files changed, 45 insertions, 22 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) @@ -18,6 +18,10 @@ #define ARR_SIZE(xs) (sizeof(xs) / sizeof(xs[0])) #define MAX(a, b) ((a) > (b) ? (a) : (b)) #define MIN(a, b) ((a) > (b) ? (b) : (a)) +#define TERM_GREEN "\e[0;32m" +#define TERM_YELLOW "\e[0;33m" +#define TERM_RED "\e[0;31m" +#define TERM_RESET "\e[0;0m" // Flags #ifndef VERBOSE @@ -42,15 +42,6 @@ int interpret_bytecode(const char *filepath) return ret; } -int assemble_instructions(inst_t *instructions, size_t number, - const char *filepath) -{ - FILE *fp = fopen(filepath, "wb"); - insts_write_bytecode_file(instructions, number, fp); - fclose(fp); - return 0; -} - void usage(const char *program_name, FILE *out) { fprintf(out, |