From ed12ce05dba6456d428dfd5b6d864fca3622079a Mon Sep 17 00:00:00 2001 From: Payton Turnage Date: Mon, 13 Jan 2014 09:45:27 -0500 Subject: [PATCH] Remove all traces of the tvm_tree structure. It is to be replaced with a htab hash table. --- include/tvm/tvm_lexer.h | 4 +- include/tvm/tvm_preprocessor.h | 4 +- include/tvm/tvm_program.h | 3 - include/tvm/tvm_tree.h | 17 ------ libtvm/tvm_lexer.c | 8 +-- libtvm/tvm_preprocessor.c | 14 +---- libtvm/tvm_program.c | 4 +- libtvm/tvm_tree.c | 101 --------------------------------- 8 files changed, 7 insertions(+), 148 deletions(-) delete mode 100644 include/tvm/tvm_tree.h delete mode 100644 libtvm/tvm_tree.c diff --git a/include/tvm/tvm_lexer.h b/include/tvm/tvm_lexer.h index d10cfbc..6b28a6b 100644 --- a/include/tvm/tvm_lexer.h +++ b/include/tvm/tvm_lexer.h @@ -4,8 +4,6 @@ #define MAX_ARGS 2 #define MAX_TOKENS 4 -#include "tvm_tree.h" - typedef struct tvm_lexer_s { char **source_lines; @@ -16,6 +14,6 @@ tvm_lexer_t *lexer_create(); void lexer_destroy(tvm_lexer_t *l); /* Tokenize the character array "source" into lines and tokens */ -void lex(tvm_lexer_t *lexer, char *source, tvm_tree_t **node); +void lex(tvm_lexer_t *lexer, char *source); #endif diff --git a/include/tvm/tvm_preprocessor.h b/include/tvm/tvm_preprocessor.h index 2b5c41a..e999950 100644 --- a/include/tvm/tvm_preprocessor.h +++ b/include/tvm/tvm_preprocessor.h @@ -1,8 +1,6 @@ #ifndef TVM_PREPROCESSOR_H_ #define TVM_PREPROCESSOR_H_ -#include "tvm_tree.h" - -int tvm_preprocess(char *src, int *src_len, tvm_tree_t **node); +int tvm_preprocess(char *src, int *src_len); #endif diff --git a/include/tvm/tvm_program.h b/include/tvm/tvm_program.h index 3201e51..e5722f1 100644 --- a/include/tvm/tvm_program.h +++ b/include/tvm/tvm_program.h @@ -7,7 +7,6 @@ #include "tvm_htab.h" #include "tvm_memory.h" -#include "tvm_tree.h" typedef struct tvm_program_s { @@ -21,8 +20,6 @@ typedef struct tvm_program_s int **values; int num_values; - tvm_tree_t *defines; - tvm_htab_t *label_htab; } tvm_program_t; diff --git a/include/tvm/tvm_tree.h b/include/tvm/tvm_tree.h deleted file mode 100644 index d450893..0000000 --- a/include/tvm/tvm_tree.h +++ /dev/null @@ -1,17 +0,0 @@ -#ifndef TVM_TREE_H_ -#define TVM_TREE_H_ - -typedef struct tvm_tree_t -{ - char *keystr; - char *val; - struct tvm_tree_t *left; - struct tvm_tree_t *right; -} tvm_tree_t; - -tvm_tree_t *tvm_tree_create(tvm_tree_t *node, const char *str, const void *val, int len); -int tvm_tree_add(tvm_tree_t *node, const char *str, const void *val, int len); -void *tvm_tree_find(tvm_tree_t *node, const char *str); -void tvm_tree_destroy(tvm_tree_t *node); - -#endif \ No newline at end of file diff --git a/libtvm/tvm_lexer.c b/libtvm/tvm_lexer.c index a5e5e70..0db1d1d 100644 --- a/libtvm/tvm_lexer.c +++ b/libtvm/tvm_lexer.c @@ -26,7 +26,7 @@ void lexer_destroy(tvm_lexer_t *lexer) free(lexer); } -void lex(tvm_lexer_t *lexer, char *source, tvm_tree_t **node) +void lex(tvm_lexer_t *lexer, char *source) { int i, j; char *pToken, *pLine = strtok(source, "\n"); @@ -60,9 +60,7 @@ void lex(tvm_lexer_t *lexer, char *source, tvm_tree_t **node) for(j = 0; (pToken && j < MAX_TOKENS); j++) { - /* Check if this token is a define. */ - char *token = (char *)tvm_tree_find(*node, pToken); - if(!token) token = pToken; + char *token = pToken; lexer->tokens[i][j] = (char *)calloc(1, (strlen(token) + 1)); strcpy(lexer->tokens[i][j], token); @@ -72,6 +70,4 @@ void lex(tvm_lexer_t *lexer, char *source, tvm_tree_t **node) } lexer->tokens[i] = NULL; - tvm_tree_destroy(*node); - *node = NULL; } diff --git a/libtvm/tvm_preprocessor.c b/libtvm/tvm_preprocessor.c index 82286ee..1fce20a 100644 --- a/libtvm/tvm_preprocessor.c +++ b/libtvm/tvm_preprocessor.c @@ -3,7 +3,7 @@ #include -int tvm_preprocess(char *src, int *src_len, tvm_tree_t **node) +int tvm_preprocess(char *src, int *src_len) { char* pp_directive_delimiter = NULL; if((pp_directive_delimiter = strstr(src, "%include"))) @@ -85,18 +85,6 @@ int tvm_preprocess(char *src, int *src_len, tvm_tree_t **node) return -1; } - int err = 0; - if(!*node) - *node = tvm_tree_create(*node, keystr, valstr, strlen(valstr)); - else - err = tvm_tree_add(*node, keystr, valstr, strlen(valstr)); - - if(err == 2) - { - printf("Multiple definitions for %s.\n", keystr); - return -1; - } - /* Remove the define line so it is not processed again. */ size_t new_length = *src_len - (end - begin); size_t first_block_len = begin - src; diff --git a/libtvm/tvm_program.c b/libtvm/tvm_program.c index ca74421..c31ab7a 100644 --- a/libtvm/tvm_program.c +++ b/libtvm/tvm_program.c @@ -55,14 +55,14 @@ int program_interpret(tvm_program_t *p, char *filename, tvm_memory_t *pMemory) fclose(pFile); int err = 0; - while((err = tvm_preprocess(source, &source_length, &p->defines)) > 0); + while((err = tvm_preprocess(source, &source_length)) > 0); /* The preprocessor encountered a problem. */ if (err < 0) return 1; tvm_lexer_t *lexer_ctx = lexer_create(); - lex(lexer_ctx, source, &p->defines); + lex(lexer_ctx, source); free(source); if(parse_labels(p, (const char ***)lexer_ctx->tokens) != 0) return 1; diff --git a/libtvm/tvm_tree.c b/libtvm/tvm_tree.c deleted file mode 100644 index 77cb96f..0000000 --- a/libtvm/tvm_tree.c +++ /dev/null @@ -1,101 +0,0 @@ -#include -#include - -#include - -tvm_tree_t *tvm_tree_create(tvm_tree_t *node, const char *str, const void *val, int len) -{ - int keylen = strlen(str); - node = calloc(1, sizeof(tvm_tree_t) + len + keylen + 1); - - if(val && len) - { - node->val = (char *)node + sizeof(tvm_tree_t); - memcpy(node->val, val, len); - } - - node->keystr = (char *)node + sizeof(tvm_tree_t) + len; - memcpy(node->keystr, str, keylen); - - return node; -} - -int tvm_tree_add(tvm_tree_t *node, const char *str, const void *val, int len) -{ - if (!str || !node) - return 1; - - int done = 0, diff; - tvm_tree_t *current = node; - while(!done) - { - diff = strcmp(current->keystr, str); - if(!diff) - done = 1; - else if(diff < 0) - { - if(current->left) - { - current = current->left; - } - else - { - current->left = tvm_tree_create(NULL, str, val, len); - done = 1; - } - } - else if(diff > 0) - { - if(current->right) - { - current = current->right; - } - else - { - current->right = tvm_tree_create(NULL, str, val, len); - done = 1; - } - } - } - - /* String already exists in the tree. */ - if(!diff) - return 2; - - return 0; -} - -void *tvm_tree_find(tvm_tree_t *node, const char *str) -{ - if(!node || !str) - return 0; - - int diff; - void *value = NULL; - tvm_tree_t *current = node; - while(current && !value) - { - diff = strcmp(current->keystr, str); - if(!diff) - value = current->val; - else if(diff < 0) - current = current->left; - else if(diff > 0) - current = current->right; - } - - return value; -} - -void tvm_tree_destroy(tvm_tree_t *node) -{ - if(node) - { - if (node->right) - tvm_tree_destroy(node->right); - if (node->left) - tvm_tree_destroy(node->left); - - free(node); - } -} \ No newline at end of file