Clearer VERBOSE messages

This commit is contained in:
2023-11-01 15:22:47 +00:00
parent 6a270eda1e
commit 6d35283ef0
3 changed files with 66 additions and 28 deletions

View File

@@ -71,7 +71,19 @@ int main(int argc, char *argv[])
printf("\t[%sTOKENISER%s]: %lu bytes -> %lu tokens\n", TERM_GREEN, TERM_RESET, printf("\t[%sTOKENISER%s]: %lu bytes -> %lu tokens\n", TERM_GREEN, TERM_RESET,
buffer.used, tokens.available); buffer.used, tokens.available);
#endif #endif
#if VERBOSE >= 2
printf("\t[%sTOKENISER%s]: Tokens parsed:\n", TERM_GREEN, TERM_RESET);
for (size_t i = 0; i < tokens.available; ++i)
{
token_t token = TOKEN_STREAM_AT(tokens.data, i);
printf("\t[%lu]: %s(`%s`)@%lu,%lu\n", i, token_type_as_cstr(token.type),
token.str, token.line, token.column);
}
#endif
free(buffer.data); free(buffer.data);
buffer.data = NULL;
size_t number = 0; size_t number = 0;
inst_t *instructions = NULL; inst_t *instructions = NULL;
@@ -96,6 +108,16 @@ int main(int argc, char *argv[])
TERM_RESET, tokens.available, number); TERM_RESET, tokens.available, number);
#endif #endif
#if VERBOSE >= 2
printf("\t[%sPARSER%s]: Instructions parsed:\n", TERM_GREEN, TERM_RESET);
for (size_t i = 0; i < number; ++i)
{
printf("\t[%lu]: ", i);
inst_print(instructions[i], stdout);
printf("\n");
}
#endif
fp = fopen(out_file, "wb"); fp = fopen(out_file, "wb");
insts_write_bytecode_file(instructions, number, fp); insts_write_bytecode_file(instructions, number, fp);
fclose(fp); fclose(fp);
@@ -104,7 +126,8 @@ int main(int argc, char *argv[])
out_file); out_file);
#endif #endif
end: end:
// Free the tokens and parsed data if (buffer.data)
free(buffer.data);
if (tokens.data) if (tokens.data)
{ {
for (size_t i = 0; i < tokens.available; ++i) for (size_t i = 0; i < tokens.available; ++i)

View File

@@ -17,31 +17,6 @@
#include "./runtime.h" #include "./runtime.h"
#include <lib/inst.h> #include <lib/inst.h>
int interpret_bytecode(const char *filepath)
{
FILE *fp = fopen(filepath, "rb");
size_t number = 0;
inst_t *instructions = insts_read_bytecode_file(fp, &number);
fclose(fp);
byte stack[256];
vm_t vm = {0};
vm_load_stack(&vm, stack, ARR_SIZE(stack));
vm_load_program(&vm, instructions, number);
err_t err = vm_execute_all(&vm);
int ret = 0;
if (err)
{
const char *error_str = err_as_cstr(err);
fprintf(stderr, "[ERROR]: %s\n", error_str);
vm_print_all(&vm, stderr);
ret = 255 - err;
}
free(instructions);
return ret;
}
void usage(const char *program_name, FILE *out) void usage(const char *program_name, FILE *out)
{ {
fprintf(out, fprintf(out,
@@ -61,5 +36,44 @@ int main(int argc, char *argv[])
} }
const char *filename = argv[1]; const char *filename = argv[1];
return interpret_bytecode(filename); #if VERBOSE >= 1
printf("[%sINTERPRETER%s]: Interpreting `%s`\n", TERM_YELLOW, TERM_RESET,
filename);
#endif
FILE *fp = fopen(filename, "rb");
size_t number = 0;
inst_t *instructions = insts_read_bytecode_file(fp, &number);
fclose(fp);
#if VERBOSE >= 1
printf("\t[%sBYTECODE-READER%s]: Read %lu instructions\n", TERM_GREEN,
TERM_RESET, number);
#endif
byte stack[256];
vm_t vm = {0};
vm_load_stack(&vm, stack, ARR_SIZE(stack));
vm_load_program(&vm, instructions, number);
#if VERBOSE >= 1
printf("\t[%sVM-SETUP%s]: Loaded stack and program into VM\n", TERM_GREEN,
TERM_RESET);
#endif
err_t err = vm_execute_all(&vm);
int ret = 0;
if (err)
{
const char *error_str = err_as_cstr(err);
fprintf(stderr, "[ERROR]: %s\n", error_str);
vm_print_all(&vm, stderr);
ret = 255 - err;
}
free(instructions);
#if VERBOSE >= 1
printf("[%sINTERPRETER%s]: Finished execution\n", TERM_GREEN, TERM_RESET);
#endif
return ret;
} }

View File

@@ -299,7 +299,8 @@ err_t vm_execute_all(vm_t *vm)
} }
#if VERBOSE >= 1 #if VERBOSE >= 1
fprintf(stdout, "[vm_execute_all]: Final VM state(Cycle %lu)\n", cycles); fprintf(stdout, "[%svm_execute_all%s]: Final VM state(Cycle %lu)\n",
TERM_YELLOW, TERM_RESET, cycles);
vm_print_all(vm, stdout); vm_print_all(vm, stdout);
#endif #endif
return err; return err;