Added a "usage" message and colours for assembler

Prints useful and pretty messages when verbose being at least 1.
This commit is contained in:
2023-10-29 16:58:58 +00:00
parent 597a45aa73
commit 157c79d53c
3 changed files with 45 additions and 22 deletions

View File

@@ -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)

View File

@@ -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

View File

@@ -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,