commit 87e0b0aa84b2f4043bbb8a1839f47fc573389d08 parent 96353f2df124558ab919e495af73436f3ef2ce3c Author: Paul Longtine <paullongtine@gmail.com> Date: Sun Jan 24 13:50:54 2016 Tweaked tests, updated lots of things, reworked a broken casting system Diffstat: src/vm/inc/ns.h | 26 +++++++------ src/vm/inc/var.h | 34 +++++++++--------- src/vm/src/ht.c | 16 +++++--- src/vm/src/var.c | 79 ++++++++++++++++++++--------------------- src/vm/tests/bc/test | Bin 11264 -> 0 bytes src/vm/tests/ht/Makefile | 4 +- src/vm/tests/ht/expected_output | 7 ++-- src/vm/tests/ht/test.c | 19 +++++++--- src/vm/tests/stk/test | Bin 11520 -> 0 bytes src/vm/tests/var/expected_output | 1 +- src/vm/tests/var/test.c | 14 +++++-- 11 files changed, 115 insertions(+), 85 deletions(-)
diff --git a/src/vm/inc/ns.h b/src/vm/inc/ns.h @@ -22,42 +22,46 @@ typedef struct ns_t { ns_cont* last; } ns_t; -ns_cont* ns_cont_init(ns_addr); - /* Initializes namespace of size */ ns_t* ns_init(ns_addr); - -void ns_cont_del(ns_cont*); +ns_cont* ns_cont_init(ns_addr); /* Cleans up memory */ void ns_del(ns_t*); +void ns_cont_del(ns_cont*); /* Pushes namespace of size */ -void ns_push(ns_t*, int); +void ns_push(ns_t*, ns_addr); /* Pops last namespace level */ void ns_pop(ns_t*); -void ns_cont_dec(ns_cont*, b_type, ns_addr); - /* Declares a variable, at root or last namespace + * int = namespace level + * * 0 -> current namespace + * * 1 -> global namespace */ void ns_dec(ns_t*, b_type, int, ns_addr); - -void ns_cont_set(ns_cont*, var_cont*, ns_addr); +void ns_cont_dec(ns_cont*, b_type, ns_addr); /* Sets variable to value, at root or last namespace + * int = namespace level + * * 0 -> current namespace + * * 1 -> global namespace */ void ns_set(ns_t*, int, ns_addr, var_cont*); - -var_cont* ns_cont_get(ns_cont*, ns_addr); +void ns_cont_set(ns_cont*, var_cont*, ns_addr); /* Gets variable from address + * int = namespace level + * * 0 -> current namespace + * * 1 -> global namespace */ var_cont* ns_get(ns_t*, int, ns_addr); +var_cont* ns_cont_get(ns_cont*, ns_addr); #endif // NS_H diff --git a/src/vm/inc/var.h b/src/vm/inc/var.h @@ -47,32 +47,34 @@ typedef struct var_data_u_str { } var_data_u_str; */ -void** var_data_alloc_G_INT(var_data_int); -void** var_data_alloc_G_FLOAT(var_data_float); -void** var_data_alloc_G_CHAR(var_data_char); -void** var_data_alloc_G_STR(size_t); - -void** var_data_alloc(b_type); - /* Initialze variable with type */ var_cont* var_new(b_type); -void var_data_free(void**, b_type); +void** var_data_alloc(b_type); +void** var_data_alloc_G_INT(var_data_int); +void** var_data_alloc_G_FLOAT(var_data_float); +void** var_data_alloc_G_CHAR(var_data_char); +void** var_data_alloc_G_STR(size_t); /* Frees variable */ void var_del(var_cont*); +void var_data_free(void**, b_type); +/* Sets variable + * void** -> pointer to allocated space of memory that agrees with b_type + */ void var_set(var_cont*, void**, b_type); -var_cont* var_cast_data(var_cont*, b_type); - -void var_cast(var_cont**, b_type); - -var_cont* var_cast_data_G_INT(void**, b_type); -var_cont* var_cast_data_G_FLOAT(void**, b_type); -var_cont* var_cast_data_G_CHAR(void**, b_type); -var_cont* var_cast_data_G_STR(void**, b_type); +/* Casts type to variable + * This function will try to change the passed variable to a different type + */ +void var_cast(var_cont*, b_type); +void** var_cast_data(var_cont*, b_type); +void** var_cast_data_G_INT(void**, b_type); +void** var_cast_data_G_FLOAT(void**, b_type); +void** var_cast_data_G_CHAR(void**, b_type); +void** var_cast_data_G_STR(void**, b_type); #endif // TYPES_H diff --git a/src/vm/src/ht.c b/src/vm/src/ht.c @@ -1,4 +1,4 @@ -#define _XOPEN_SOUCE 500 +#define _XOPEN_SOURCE 500 #include <stdio.h> #include <stdlib.h> @@ -11,8 +11,10 @@ ht_t* ht_init(int size) { + ASSERT((size % 2) == 0, "Hashtable size must be powers of 2\n"); + ht_t* hashtable = (ht_t*)malloc(sizeof(ht_t)); - ASSERT(hashtable != NULL, "Could not allocate memory\n"); + M_ASSERT(hashtable); hashtable->size = size; hashtable->table = ht_init_table(hashtable->size); @@ -22,8 +24,10 @@ ht_t* ht_init(int size) ht_entry** ht_init_table(int size) { + ASSERT((size % 2 ) == 0, "hashtable size must be powers of 2\n"); + ht_entry** table = (ht_entry**)malloc(sizeof(ht_entry*)*size); - ASSERT(table != NULL, "Could not allocate memory\n"); + M_ASSERT(table); for (int i = 0; i < size; i++) { @@ -130,10 +134,10 @@ ht_entry* ht_newpair(char* key, var_cont* value) ht_entry* newpair; newpair = malloc(sizeof(ht_entry)); - ASSERT(newpair != NULL, "Couldn't allocate memory!"); + M_ASSERT(newpair); - newpair->key = strdup(key); - ASSERT(newpair->key != NULL, "Null keys are bad mhmkay"); + newpair->key = (char*)strdup(key); + M_ASSERT(newpair->key); newpair->value = value; diff --git a/src/vm/src/var.c b/src/vm/src/var.c @@ -104,7 +104,7 @@ void var_del(var_cont* var) { if (var == NULL) return; - + if (var->data != NULL) var_data_free(var->data, var->type); @@ -125,72 +125,71 @@ void var_set(var_cont* var, void** data, b_type type) var->data = data; } -var_cont* var_cast_data(var_cont* var, b_type type) +void** var_cast_data_G_INT(void** data, b_type type) { - if (var->type == VOID) - return var_new(type); + N_ASSERT(data); - if (var->type == G_INT) - return var_cast_data_G_INT(var->data, type); + free(data); - if (var->type == G_FLOAT) - return var_cast_data_G_FLOAT(var->data, type); + return var_data_alloc(type); +} - if (var->type == G_CHAR) - return var_cast_data_G_CHAR(var->data, type); +void** var_cast_data_G_FLOAT(void** data, b_type type) +{ + N_ASSERT(data); - if (var->type == G_STR) - return var_cast_data_G_STR(var->data, type); + free(data); - return var_new(type); + return var_data_alloc(type); } -void var_cast(var_cont** var, b_type type) +void** var_cast_data_G_CHAR(void** data, b_type type) { - if (var == NULL) - return; + N_ASSERT(data); - *var = var_cast_data(*var, type); + free(data); - (*var)->type = type; + return var_data_alloc(type); } -var_cont* var_cast_data_G_INT(void** data, b_type type) +void** var_cast_data_G_STR(void** data, b_type type) { - var_cont* new = var_new(type); + N_ASSERT(data); - if (data != NULL) - free(data); + free(data); - return new; + return var_data_alloc(type); } -var_cont* var_cast_data_G_FLOAT(void** data, b_type type) +void** var_cast_data(var_cont* var, b_type type) { - var_cont* new = var_new(type); + void** rv = NULL; - if (data != NULL) - free(data); + if (var->type == G_INT) + rv = var_cast_data_G_INT(var->data, type); - return new; -} + if (var->type == G_FLOAT) + rv = var_cast_data_G_FLOAT(var->data, type); -var_cont* var_cast_data_G_CHAR(void** data, b_type type) -{ - var_cont* new = var_new(type); + if (var->type == G_CHAR) + rv = var_cast_data_G_CHAR(var->data, type); - if (data != NULL) - free(data); + if (var->type == G_STR) + rv = var_cast_data_G_STR(var->data, type); - return new; + return rv; } -var_cont* var_cast_data_G_STR(void** data, b_type type) +void var_cast(var_cont* var, b_type type) { - var_cont* new = var_new(type); + if (var == NULL) + return; + + if (var->data == NULL) + return; - if (data != NULL) - free(data); + var->data = var_cast_data(var, type); - return new; + var->type = type; } + diff --git a/src/vm/tests/bc/test b/src/vm/tests/bc/test Binary files differ diff --git a/src/vm/tests/ht/Makefile b/src/vm/tests/ht/Makefile @@ -5,11 +5,11 @@ CC = gcc CFLAGS = -std=c99 -Wall -I$(INC_DIR) DEPS = i$(INC_DIR)/helper.h \ - types.h \ + var.h \ ht.h OBJ = test.o \ - $(SRC_DIR)/types.o \ + $(SRC_DIR)/var.o \ $(SRC_DIR)/ht.o OUT = test diff --git a/src/vm/tests/ht/expected_output b/src/vm/tests/ht/expected_output @@ -1,3 +1,6 @@ 0 -1 -2 +3 +4 +key2 value: 0 +key2 value: 32 +key2 value: 32 diff --git a/src/vm/tests/ht/test.c b/src/vm/tests/ht/test.c @@ -1,18 +1,29 @@ #include <stdio.h> +#include "var.h" #include "ht.h" int main(int argc, char* argv[]) { - ht_t* hashtable = ht_init(65536); - ht_set(hashtable, "key1", var_new(0)); - ht_set(hashtable, "key2", var_new(1)); - ht_set(hashtable, "key3", var_new(2)); + ht_t* hashtable = ht_init(2); + ht_set(hashtable, "key1", var_new(VOID)); + ht_set(hashtable, "key2", var_new(G_INT)); + ht_set(hashtable, "key3", var_new(G_FLOAT)); printf("%i\n", ht_get(hashtable, "key1")->type); printf("%i\n", ht_get(hashtable, "key2")->type); printf("%i\n", ht_get(hashtable, "key3")->type); + var_cont* var = ht_get(hashtable, "key2"); + + printf("key2 value: %i\n", *(var->data)); + + var_set(var, var_data_alloc_G_INT(32), G_INT); + + printf("key2 value: %i\n", *(var->data)); + + printf("key2 value: %i\n", *(ht_get(hashtable, "key2")->data)); + ht_destroy(hashtable); return 0; } diff --git a/src/vm/tests/stk/test b/src/vm/tests/stk/test Binary files differ diff --git a/src/vm/tests/var/expected_output b/src/vm/tests/var/expected_output @@ -1,2 +1,3 @@ 1 1 +3 -> 4 diff --git a/src/vm/tests/var/test.c b/src/vm/tests/var/test.c @@ -1,6 +1,7 @@ #include <stdio.h> #include "var.h" +#include "helper.h" int main( void ) { @@ -9,16 +10,21 @@ int main( void ) printf("1\n"); else printf("0\n"); - + var_cont* test1 = var_new(G_INT); var_set(test1, var_data_alloc_G_INT(32), G_INT); - int testing = *(test1->data); - if (testing == 32) + if (*(test1->data) == 32) printf("1\n"); else - printf("0: %d\n", testing); + printf("0: %d\n", *(test1->data)); + printf("%i -> ", test1->type); + var_cast(test1, G_FLOAT); + printf("%i\n", test1->type); + + N_ASSERT(test1->data); + var_del(test0); var_del(test1); }