From f3a5981fa2e6aff6b6cbae3f4117b9dec0666ccb Mon Sep 17 00:00:00 2001 From: Aryadev Chavali Date: Sat, 21 Oct 2023 23:24:41 +0100 Subject: Wrote generalised procedures for interpret and assembly --- src/main.c | 39 +++++++++++++++++++++++---------------- 1 file changed, 23 insertions(+), 16 deletions(-) (limited to 'src') diff --git a/src/main.c b/src/main.c index 6682fc5..5298876 100644 --- a/src/main.c +++ b/src/main.c @@ -17,28 +17,35 @@ #include "./inst.h" #include "./runtime.h" -void write_bytes_to_file(darr_t *bytes, const char *filepath) +int interpret_bytecode(const char *filepath) { - FILE *fp = fopen(filepath, "wb"); - size_t size = fwrite(bytes->data, bytes->used, 1, fp); + FILE *fp = fopen(filepath, "rb"); + size_t number = 0; + inst_t *instructions = insts_read_bytecode_file(fp, &number); fclose(fp); - assert(size == 1); + + byte stack[256]; + vm_t vm = {0}; + vm_load_stack(&vm, stack, ARR_SIZE(stack)); + vm_load_program(&vm, instructions, number); + for (size_t i = 0; i < number; ++i) + { + vm_execute(&vm); + printf("Cycle %lu\n", i); + vm_print_all(&vm, stdout); + printf("\n"); + } + free(instructions); + return 0; } -void read_bytes_from_file(const char *filepath, darr_t *darr) +int assemble_instructions(inst_t *instructions, size_t number, + const char *filepath) { - darr->data = NULL; - darr->used = 0; - darr->available = 0; - - FILE *fp = fopen(filepath, "rb"); - fseek(fp, 0, SEEK_END); - long size = ftell(fp); - darr_init(darr, size); - fseek(fp, 0, SEEK_SET); - size_t read = fread(darr->data, size, 1, fp); + FILE *fp = fopen(filepath, "wb"); + insts_write_bytecode_file(instructions, number, fp); fclose(fp); - assert(read == 1); + return 0; } int main(void) -- cgit v1.2.3-13-gbd6f