commit 1c7c967f10ea047471e4c2812716e0ef539f541b parent bf52ab078dec3dcff68cca5e4117d64a3749f727 Author: Paul Longtine <paullongtine@gmail.com> Date: Fri Dec 18 08:36:32 2015 Outlined namespace implementation, made comments look different Diffstat: src/vm/Makefile | 14 +++++++++---- src/vm/inc/bc.h | 21 ++++++------------- src/vm/inc/fh.h | 15 ++++---------- src/vm/inc/is.h | 13 ++++-------- src/vm/inc/ns.h | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++- src/vm/inc/stk.h | 30 ++++++++++++---------------- src/vm/inc/types.h | 9 ++------ src/vm/src/ns.c | 40 +++++++++++++++++++++++++++++++++++++- src/vm/src/types.c | 3 +++- src/vm/tests/bc/test.c | 3 ++- 10 files changed, 143 insertions(+), 60 deletions(-)
diff --git a/src/vm/Makefile b/src/vm/Makefile @@ -5,14 +5,20 @@ CC = gcc CFLAGS = -std=c99 -Wall -I$(INC_DIR) DEPS = $(INC_DIR)/is_mdata.h \ - fh.h \ - is.h \ - bc.h \ + fh.h \ + is.h \ + bc.h \ + types.h \ + stk.h \ + ns.h OBJ = $(SRC_DIR)/main.o \ $(SRC_DIR)/fh.o \ $(SRC_DIR)/is.o \ - $(SRC_DIR)/bc.o + $(SRC_DIR)/bc.o \ + $(SRC_DIR)/types.o\ + $(SRC_DIR)/stk.o \ + $(SRC_DIR)/ns.o OUT = main diff --git a/src/vm/inc/bc.h b/src/vm/inc/bc.h @@ -1,5 +1,4 @@ -/* - * `bc` handles bytecode objects. +/* `bc` handles bytecode objects. */ #ifndef BC_H @@ -10,8 +9,7 @@ #include "fh.h" -/* - * 'Bytecode Container' +/* 'Bytecode Container' */ typedef struct bc_cont { byte_t op; @@ -22,23 +20,19 @@ typedef struct bc_cont { #include "is.h" -/* - * Handles allocation for new `bc_cont` instances +/* Handles allocation for new `bc_cont` instances */ bc_cont* bc_cont_new(void); -/* - * Pushes new bc_cont to the chain. +/* Pushes new bc_cont to the chain. */ bc_cont* bc_cont_push(bc_cont*); -/* - * Deallocates all the things, assuming the arguement is the root. +/* Deallocates all the things, assuming the arguement is the root. */ void bc_cont_del(bc_cont*); -/* - * Given a file object, and an instance of `bc_cont` with proper metadata, this +/* Given a file object, and an instance of `bc_cont` with proper metadata, this * function will read arguements into bc_cont. */ void get_args(FILE*, bc_cont*); @@ -46,8 +40,7 @@ byte_t* get_byte_arg(FILE*); byte_t* get_word_arg(FILE*); byte_t* get_dync_arg(FILE*); -/* - * Initiates the first pass to take a raw binary file and translate it into a +/* Initiates the first pass to take a raw binary file and translate it into a * basic datastructure */ bc_cont* bc_read(char* fname); diff --git a/src/vm/inc/fh.h b/src/vm/inc/fh.h @@ -1,5 +1,4 @@ -/* - * 'fh.h' is here to provide useful functions to mess about with files. +/* 'fh.h' is here to provide useful functions to mess about with files. */ #ifndef FH_H @@ -10,23 +9,19 @@ typedef unsigned char byte_t; -/* - * Reads passed file descriptor until NULL, returns array of byte_t +/* Reads passed file descriptor until NULL, returns array of byte_t */ byte_t* read_until_null(FILE*); -/* - * Reads n bytes, n passed as @param long, returns array of byte_t +/* Reads n bytes, n passed as @param long, returns array of byte_t */ byte_t* read_bytes(FILE*, long); -/* - * Reads a byte, returns byte_t +/* Reads a byte, returns byte_t */ byte_t read_byte(FILE*); -/* - * Determines filesize, returns size as long +/* Determines filesize, returns size as long */ long read_size(FILE*); diff --git a/src/vm/inc/is.h b/src/vm/inc/is.h @@ -10,8 +10,8 @@ #include <stdlib.h> -#include "bc.h" #include "fh.h" +#include "bc.h" #include "ins_mdata.h" @@ -26,14 +26,12 @@ // This array is populated by inc/is_mdata byte_t INS_MDATA[256]; -/* - * Takes an opcode, fills metadata about that opcode (given that it exists) in the - * `bc_cont` structure +/* Takes an opcode, fills metadata about that opcode (given that it exists) in + * the `bc_cont` structure */ void get_opcode(byte_t, bc_cont*); -/* - * Fills in metadata in @param byte_t. +/* Fills in metadata in @param byte_t. * @param[1] is number of params, * @param[2] is an array[3] detailing param types * @@ -43,8 +41,7 @@ void get_opcode(byte_t, bc_cont*); */ void get_mdata(byte_t, int*, int*); -/* - * Sets up the datastructure to quickly queue for data. +/* Sets up the datastructure to quickly queue for data. */ void init(void); diff --git a/src/vm/inc/ns.h b/src/vm/inc/ns.h @@ -0,0 +1,55 @@ +/* `ns` Namespace implementation + * + * Each variable that exists and accessed will be via a level on a namespace + * instance. For example, at the root of a program, a namespace is initialized + * and variables declared at this level are accessable everywhere. When a func + * is called, a new namespace level is initialized and variables declared on that + * level will be destroyed/inaccessable when the function returns and the new + * level is 'popped', going up a level. + * + * When a variable is queued, it will search through all the levels of the + * namespace to retreive the proper value. + */ + +#ifndef NS_H +#define NS_H + +#include <stdlib.h> + +#include "types.h" + +typedef struct ns_t { + void* placeholder; +} ns_t; + +/* Initializes namespace. + */ +ns_t* ns_init(void); + +/* Initializes new namspace level. + */ +void ns_new(ns_t*); + +/* Pushes new namespace level to a pre-initialized state + */ +void ns_push(ns_t*, ns_t*); + +/* Pops last namespace level. Destroys all data initialized at level. + */ +void ns_pop_del(ns_t*); + +/* Pops last namespace level. Keeps data, returns pointer. + */ +ns_t* ns_pop(ns_t*); + +/* Declares a variable of name char*. + * Return code denotes a success or failure. (bool) + */ +int ns_dec(ns_t*, char*); + +/* Sets variable to value. + * Return code denotes a success or failure. (bool) + */ +int ns_set(ns_t*, char*, var_cont*); + +#endif // NS_H diff --git a/src/vm/inc/stk.h b/src/vm/inc/stk.h @@ -1,6 +1,5 @@ -/* - stk.h -> Provide implementation of a stack -*/ +/* stk.h -> Provide implementation of a stack + */ #ifndef STK_H #define STK_H @@ -15,41 +14,38 @@ typedef struct stk_t { var_cont* data; } stk_t; -/* - * Create a new stack instance +/* Create a new stack instance */ stk_t* stk_new(void); -/* - * Delete a stack +/* Delete a stack */ void stk_del(stk_t*); -/* - * Pop the first element of the stack +/* Pop the first element of the stack * * @param stk_t* * stack instance pointer */ var_cont* stk_pop(stk_t**); -/* - * Pushes @param var_cont* to the stack +/* Pushes @param var_cont* to the stack */ void stk_push(stk_t**, var_cont*); -/* - * Returns the data at location in the stack +/* Returns the data at location in the stack */ var_cont* stk_at(stk_t*, int); -/* - * Rotates the top two elements of the stack +/* Rotates the top two elements of the stack + * i.e. [4, 3, 2, 1] -> [3, 4, 2, 1] + * ^ rot_top() */ void stk_rot_top(stk_t**); -/* - * Rotates the top three elements of the stack +/* Rotates the top three elements of the stack + * i.e. [4, 3, 2, 1] -> [2, 3, 4, 1] + * ^ rot_three() */ void stk_rot_three(stk_t**); diff --git a/src/vm/inc/types.h b/src/vm/inc/types.h @@ -1,5 +1,4 @@ -/* - * types.h -> Provide implemenation of types +/* types.h -> Provide implemenation of types */ #include <stdlib.h> @@ -26,13 +25,11 @@ typedef struct var_cont { void* data; } var_cont; -/* - * Initialze variable with type +/* Initialze variable with type */ var_cont* var_new(b_type); -/* - * Frees variable +/* Frees variable */ void var_del(var_cont*); diff --git a/src/vm/src/ns.c b/src/vm/src/ns.c @@ -0,0 +1,40 @@ +#include <stdlib.h> + +#include "ns.h" +#include "types.h" + +ns_t* ns_init(void) +{ + return; +} + +void ns_new(ns_t* ns) +{ + return; +} + +void ns_push(ns_t* ns, ns_t* ns_append) +{ + return; +} + +void ns_pop_del(ns_t* ns) +{ + return; +} + +ns_t* ns_pop(ns_t* ns) +{ + return; +} + +int ns_dec(ns_t* ns, char* name) +{ + return 0; +} + +int ns_set(ns_t* ns, char* name, var_cont* data) +{ + return 0; +} + diff --git a/src/vm/src/types.c b/src/vm/src/types.c @@ -20,6 +20,9 @@ void var_del(var_cont* var) { if (var == NULL) return; + + if (var->data != NULL) + free(var->data); free(var); } diff --git a/src/vm/tests/bc/test.c b/src/vm/tests/bc/test.c @@ -25,7 +25,8 @@ int main(int argc, char** argv) } if (ptr->op == 3) { - printf("%x: %x, %x %x\n", ptr->op, ptr->args[0][0], ptr->args[1][0], ptr->args[1][1]); + printf("%x: %x, %x %x\n", + ptr->op, ptr->args[0][0], ptr->args[1][0], ptr->args[1][1]); } if (ptr->op == 4) {