aboutsummaryrefslogtreecommitdiff
path: root/asm/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'asm/main.c')
-rw-r--r--asm/main.c54
1 files changed, 41 insertions, 13 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)