commit f51869be325abe62fa3ecccde0df0f3eabc3f516 parent 6f6e5d3c031528cc0ceb4089662e7970cacaaf14 Author: Paul Longtine <paullongtine@gmail.com> Date: Sun Dec 6 23:15:13 2015 added read_byte and bc_cont_push Diffstat: src/vm/inc/bc.h | 2 ++ src/vm/inc/fh.h | 2 ++ src/vm/src/bc.c | 14 +++++++++----- src/vm/src/fh.c | 7 +++++++ 4 files changed, 20 insertions(+), 5 deletions(-)
diff --git a/src/vm/inc/bc.h b/src/vm/inc/bc.h @@ -27,6 +27,8 @@ typedef struct bc_cont { */ bc_cont* bc_cont_new(void); +bc_cont** bc_cont_push(bc_cont**); + /* Deallocates all the things, assuming the arguement is the root. */ diff --git a/src/vm/inc/fh.h b/src/vm/inc/fh.h @@ -14,6 +14,8 @@ void read_until_null(FILE**, long*, byte_t**); void read_bytes(FILE**, long*, int, byte_t**); +byte_t read_byte(FILE**, long*); + long read_size(FILE**, char*); #endif // FH_H diff --git a/src/vm/src/bc.c b/src/vm/src/bc.c @@ -15,6 +15,12 @@ bc_cont* bc_cont_new(void) return new; } +bc_cont** bc_cont_push(bc_cont** head) +{ + (*head)->next = bc_cont_new(); + return &((*head)->next); +} + void bc_cont_del(bc_cont* root) { if (root->next != NULL) @@ -71,7 +77,7 @@ bc_cont* bc_read(char* fname) /* initialize datastructures for instructionstuffs, begin to read file byte-by-byte */ FILE* f; - char byte; + byte_t byte; long f_pos = 0; long fsize = read_size(&f, fname); @@ -81,14 +87,12 @@ bc_cont* bc_read(char* fname) /* Loop through file byte-by-byte */ while (f_pos<fsize) { - byte = fgetc(f); - f_pos++; + byte = read_byte(&f, &f_pos); get_opcode(byte, ptr); get_args(&f, &f_pos, ptr); - (*ptr)->next = bc_cont_new(); - ptr = &((*ptr)->next); + ptr = bc_cont_push(ptr); } fclose(f); diff --git a/src/vm/src/fh.c b/src/vm/src/fh.c @@ -24,6 +24,13 @@ void read_bytes(FILE** f, long* f_pos, int bytes, byte_t** buffer) *f_pos = *f_pos + bytes; } +byte_t read_byte(FILE** f, long* f_pos) +{ + byte_t byte = fgetc(*f); + (*f_pos)++; + return byte; +} + long read_size(FILE** f, char* fname) { *f = fopen(fname, "rb");