aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAryadev Chavali <aryadev@aryadevchavali.com>2023-10-29 16:58:58 +0000
committerAryadev Chavali <aryadev@aryadevchavali.com>2023-10-29 16:59:31 +0000
commit157c79d53c4d04f6c6af1103a2db3a652b240edb (patch)
treeec6e535c94dcd65a2c5d39ae9361ca01b827722a
parent597a45aa73d4d66fce4dd9135087e0e82f46899c (diff)
downloadovm-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.c54
-rw-r--r--lib/base.h4
-rw-r--r--vm/main.c9
3 files changed, 45 insertions, 22 deletions
diff --git a/asm/main.c b/asm/main.c
index aa6dcec..2f1102b 100644
--- a/asm/main.c
+++ b/asm/main.c
@@ -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)
diff --git a/lib/base.h b/lib/base.h
index 172abff..537792f 100644
--- a/lib/base.h
+++ b/lib/base.h
@@ -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
diff --git a/vm/main.c b/vm/main.c
index 2b82cd3..2594edc 100644
--- a/vm/main.c
+++ b/vm/main.c
@@ -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,