aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAryadev Chavali <aryadev@aryadevchavali.com>2023-10-21 23:24:41 +0100
committerAryadev Chavali <aryadev@aryadevchavali.com>2023-10-21 23:24:41 +0100
commitf3a5981fa2e6aff6b6cbae3f4117b9dec0666ccb (patch)
treef3af4497529c48a8052655bcc64848913d1e838c
parent44a9a3ca464f0bf24b0da2c0a6f106678dc67d52 (diff)
downloadovm-f3a5981fa2e6aff6b6cbae3f4117b9dec0666ccb.tar.gz
ovm-f3a5981fa2e6aff6b6cbae3f4117b9dec0666ccb.tar.bz2
ovm-f3a5981fa2e6aff6b6cbae3f4117b9dec0666ccb.zip
Wrote generalised procedures for interpret and assembly
-rw-r--r--src/main.c39
1 files changed, 23 insertions, 16 deletions
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)