language

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit d8057530fc93e4f82f2c14438d8650fe1464d251
parent 2c48260242a07ebd4c89c82218ce0ce8f8e9a27d
Author: Paul Longtine <paullongtine@gmail.com>
Date:   Sat Dec 19 14:58:11 2015

Started to implement hashtable, added helper.h, made macro ASSERT(condition, message)

Diffstat:
 src/vm/Makefile           |  7 ++++--
 src/vm/inc/bc.h           |  1 +-
 src/vm/inc/fh.h           |  2 ++-
 src/vm/inc/helper.h       | 14 ++++++++++++-
 src/vm/inc/ht.h           | 23 ++++++++++++++++----
 src/vm/inc/ns.h           |  1 +-
 src/vm/inc/stk.h          |  1 +-
 src/vm/src/bc.c           |  7 ++----
 src/vm/src/fh.c           |  5 +---
 src/vm/src/ht.c           | 56 ++++++++++++++++++++++++++++++++++++++++++++++++-
 src/vm/src/ns.c           | 20 ++++++++++-------
 src/vm/src/stk.c          |  7 +-----
 src/vm/src/types.c        |  8 ++-----
 src/vm/tests/bc/Makefile  | 13 +++++------
 src/vm/tests/ht/Makefile  | 26 ++++++++++++++++++++++-
 src/vm/tests/ht/test.c    | 10 +++++++++-
 src/vm/tests/stk/Makefile |  5 ++--
 17 files changed, 167 insertions(+), 39 deletions(-)

diff --git a/src/vm/Makefile b/src/vm/Makefile @@ -5,19 +5,22 @@ CC = gcc CFLAGS = -std=c99 -Wall -I$(INC_DIR) DEPS = $(INC_DIR)/is_mdata.h \ + helper.h \ fh.h \ - is.h \ bc.h \ + is.h \ types.h \ stk.h \ + ht.h \ ns.h OBJ = $(SRC_DIR)/main.o \ $(SRC_DIR)/fh.o \ - $(SRC_DIR)/is.o \ $(SRC_DIR)/bc.o \ + $(SRC_DIR)/is.o \ $(SRC_DIR)/types.o\ $(SRC_DIR)/stk.o \ + $(SRC_DIR)/ht.o \ $(SRC_DIR)/ns.o OUT = main diff --git a/src/vm/inc/bc.h b/src/vm/inc/bc.h @@ -8,6 +8,7 @@ #include <stdio.h> #include "fh.h" +#include "helper.h" /* 'Bytecode Container' */ diff --git a/src/vm/inc/fh.h b/src/vm/inc/fh.h @@ -7,6 +7,8 @@ #include <stdio.h> #include <stdlib.h> +#include "helper.h" + typedef unsigned char byte_t; /* Reads passed file descriptor until NULL, returns array of byte_t diff --git a/src/vm/inc/helper.h b/src/vm/inc/helper.h @@ -0,0 +1,14 @@ +/* `helper.h` -> Helper macros/functions + */ + +#ifndef HELPER_H +#define HELPER_H + +#define ASSERT(condition, message)\ + if (!(condition)) \ + { \ + fprintf(stderr, message); \ + exit(1); \ + } + +#endif // HELPER_H diff --git a/src/vm/inc/ht.h b/src/vm/inc/ht.h @@ -7,6 +7,7 @@ #include <stdlib.h> #include "types.h" +#include "helper.h" typedef struct ht_entry { char* key; @@ -19,9 +20,25 @@ typedef struct ht_t { ht_entry** table; } ht_t; -/* Creates hashtable of size +/* Creates hashtable of @param int size */ -ht_t* ht_create(int); +ht_t* ht_init(int); + +/* Creates the table of buckets for the hashtable + */ +ht_entry** ht_init_table(int); + +/* Destroys hashtable + */ +void ht_destroy(ht_t*); + +/* Destroys the table of buckets for the hashtable + */ +void ht_destroy_table(ht_entry**, int); + +/* Destroys an entry. + */ +void ht_destroy_entry(ht_entry*); /* Set a key-value pair */ @@ -39,6 +56,4 @@ ht_entry* ht_newpair(char*, var_cont*); */ int ht_hash(ht_t*, char*); - - #endif // HT_H diff --git a/src/vm/inc/ns.h b/src/vm/inc/ns.h @@ -17,6 +17,7 @@ #include <stdlib.h> #include "types.h" +#include "helper.h" typedef struct ns_t { void* placeholder; diff --git a/src/vm/inc/stk.h b/src/vm/inc/stk.h @@ -8,6 +8,7 @@ #include <stdio.h> #include "types.h" +#include "helper.h" typedef struct stk_t { struct stk_t* next; diff --git a/src/vm/src/bc.c b/src/vm/src/bc.c @@ -4,14 +4,13 @@ #include "bc.h" #include "is.h" #include "fh.h" +#include "helper.h" bc_cont* bc_cont_new(void) { bc_cont *new = (bc_cont*)malloc(sizeof(bc_cont)); - if (new == NULL) { - fprintf(stderr, "Cannot allocate memory\n"); - exit(1); - } + ASSERT(new != NULL, "Could not allocate memory\n"); + new->args[0] = NULL; new->args[1] = NULL; new->args[2] = NULL; diff --git a/src/vm/src/fh.c b/src/vm/src/fh.c @@ -2,6 +2,7 @@ #include <stdlib.h> #include "fh.h" +#include "helper.h" byte_t* read_until_null(FILE* f) { @@ -25,9 +26,7 @@ byte_t* read_until_null(FILE* f) byte_t* read_bytes(FILE* f, long bytes) { byte_t* buffer = (byte_t*)malloc(bytes*sizeof(byte_t)); - if (buffer == NULL) - return buffer; - + ASSERT(buffer != NULL, "Could not allocate memory\n"); fread(buffer, bytes, 1, f); return buffer; diff --git a/src/vm/src/ht.c b/src/vm/src/ht.c @@ -1,3 +1,59 @@ #include <stdlib.h> #include "ht.h" +#include "types.h" +#include "helper.h" + +ht_t* ht_init(int size) +{ + ht_t* hashtable = (ht_t*)malloc(sizeof(ht_t)); + ASSERT(hashtable != NULL, "Could not allocate memory\n"); + + hashtable->size = size; + + hashtable->table = ht_init_table(hashtable->size); + + return hashtable; +} + +ht_entry** ht_init_table(int size) +{ + ht_entry** table = (ht_entry**)malloc(sizeof(ht_entry)*size); + ASSERT(table != NULL, "Could not allocate memory\n"); + + for (int i = 0; i < size; i++) + { + table[i]->key = NULL; + table[i]->value = var_new(0); + table[i]->next = NULL; + } + + return table; +} + +void ht_destroy(ht_t* hashtable) +{ + ht_destroy_table(hashtable->table, hashtable->size); +} + +void ht_destroy_table(ht_entry** table, int size) +{ + for (int i = 0; i < size; i++) + { + ht_destroy_entry(table[i]); + } +} + +void ht_destroy_entry(ht_entry* entry) +{ + var_del(entry->value); + + if (entry->key != NULL) + free(entry->key); + + if (entry->next != NULL) + ht_destroy_entry(entry->next); + + free(entry); +} + diff --git a/src/vm/src/ns.c b/src/vm/src/ns.c @@ -2,38 +2,42 @@ #include "ns.h" #include "types.h" +#include "helper.h" ns_t* ns_init(void) { - return; + ns_t* namespace = (ns_t*)malloc(sizeof(ns_t)); + ASSERT(namespace != NULL, "Could not allocate memory\n"); + + return namespace; } -void ns_new(ns_t* ns) +void ns_new(ns_t* namespace) { return; } -void ns_push(ns_t* ns, ns_t* ns_append) +void ns_push(ns_t* namespace, ns_t* namespace_append) { return; } -void ns_pop_del(ns_t* ns) +void ns_pop_del(ns_t* namespace) { return; } -ns_t* ns_pop(ns_t* ns) +ns_t* ns_pop(ns_t* namespace) { - return; + return namespace; } -int ns_dec(ns_t* ns, char* name) +int ns_dec(ns_t* namespace, char* name) { return 0; } -int ns_set(ns_t* ns, char* name, var_cont* data) +int ns_set(ns_t* namespace, char* name, var_cont* data) { return 0; } diff --git a/src/vm/src/stk.c b/src/vm/src/stk.c @@ -3,15 +3,12 @@ #include "stk.h" #include "types.h" +#include "helper.h" stk_t* stk_new( void ) { stk_t* new = (stk_t*)malloc(sizeof(stk_t)); - if (new == NULL) - { - fprintf(stderr, "Could not allocate memory\n"); - exit(1); - } + ASSERT(new != NULL, "Could not allocate memory\n"); return new; } diff --git a/src/vm/src/types.c b/src/vm/src/types.c @@ -2,15 +2,13 @@ #include <stdio.h> #include "types.h" +#include "helper.h" var_cont* var_new(b_type type) { var_cont* new = (var_cont*)malloc(sizeof(var_cont)); - if (new == NULL) - { - fprintf(stderr, "Could not allocate memory"); - exit(1); - } + ASSERT(new != NULL, "Could not allocate memory\n"); + new->type = type; new->data = NULL; return new; diff --git a/src/vm/tests/bc/Makefile b/src/vm/tests/bc/Makefile @@ -5,13 +5,14 @@ CC = gcc CFLAGS = -std=c99 -Wall -I$(INC_DIR) DEPS = i$(INC_DIR)/is_mdata.h \ - fh.h \ - is.h \ - bc.h \ + helper.h \ + fh.h \ + is.h \ + bc.h \ -OBJ = test.o \ - $(SRC_DIR)/fh.o \ - $(SRC_DIR)/is.o \ +OBJ = test.o \ + $(SRC_DIR)/fh.o \ + $(SRC_DIR)/is.o \ $(SRC_DIR)/bc.o OUT = test diff --git a/src/vm/tests/ht/Makefile b/src/vm/tests/ht/Makefile @@ -0,0 +1,26 @@ +SRC_DIR = ../../src +INC_DIR = ../../inc + +CC = gcc +CFLAGS = -std=c99 -Wall -I$(INC_DIR) + +DEPS = i$(INC_DIR)/helper.h \ + types.h \ + ht.h + +OBJ = test.o \ + $(SRC_DIR)/types.o \ + $(SRC_DIR)/ht.o + +OUT = test + +%.o: %.c $(DEPS) + $(CC) $(CFLAGS) -c -o $@ $< + +$(OUT): $(OBJ) + $(CC) $(CFLAGS) -o $@ $^ + +clean: + rm *.o + rm $(SRC_DIR)/*.o + rm $(OUT) diff --git a/src/vm/tests/ht/test.c b/src/vm/tests/ht/test.c @@ -0,0 +1,10 @@ +#include <stdio.h> + +#include "ht.h" + +int main(int argc, char* argv[]) +{ + ht_t* hashtable = ht_init(65536); + ht_destroy(hashtable); + return 0; +} diff --git a/src/vm/tests/stk/Makefile b/src/vm/tests/stk/Makefile @@ -4,8 +4,9 @@ INC_DIR = ../../inc CC = gcc CFLAGS = -std=c99 -Wall -I$(INC_DIR) -DEPS = i$(INC_DIR)/stk.h \ - types.h \ +DEPS = i$(INC_DIR)/helper.h \ + stk.h \ + types.h \ OBJ = test.o \ $(SRC_DIR)/stk.o \