From c300df8ea35ff75530b6cfd187c92c41658cb52d Mon Sep 17 00:00:00 2001 From: David Walter Seikel Date: Tue, 17 Jan 2012 15:45:40 +1000 Subject: Use eina hash for functions, states, and variables. --- LuaSL/src/LuaSL_LSL_tree.h | 19 +++++++++---------- LuaSL/src/LuaSL_compile.c | 30 ++++++++++++------------------ LuaSL/src/LuaSL_lemon_yaccer.y | 4 ++-- 3 files changed, 23 insertions(+), 30 deletions(-) diff --git a/LuaSL/src/LuaSL_LSL_tree.h b/LuaSL/src/LuaSL_LSL_tree.h index fd9c65a..2674542 100644 --- a/LuaSL/src/LuaSL_LSL_tree.h +++ b/LuaSL/src/LuaSL_LSL_tree.h @@ -223,9 +223,9 @@ struct _LSL_Statement struct _LSL_Block { LSL_Block *outerBlock; - LSL_Statement **statements; - LSL_Identifier **variables; // Those variables in this scope. - int scount, vcount; +// Eina_Hash *statements; // Probably should be some sort of eina list. + Eina_Hash *variables; // Those variables in this scope. + int scount; }; struct _LSL_Function @@ -240,16 +240,15 @@ struct _LSL_State { const char *name; LSL_Leaf *block; - LSL_Function **handlers; + Eina_Hash *handlers; }; struct _LSL_Script { const char *name; - LSL_Function **functions; - LSL_State **states; - LSL_Identifier **variables; - int fcount, scount, vcount; + Eina_Hash *functions; + Eina_Hash *states; + Eina_Hash *variables; }; // Define the type for flex and lemon. @@ -277,8 +276,8 @@ typedef struct #endif -void burnLeaf(LSL_Leaf *leaf); -LSL_Leaf *addFunction(LSL_Leaf *type, LSL_Leaf *identifier, LSL_Leaf *open, LSL_Leaf *params, LSL_Leaf *close, LSL_Leaf *block); +void burnLeaf(void *data); +LSL_Leaf *addFunction(LuaSL_compiler *compiler, LSL_Leaf *type, LSL_Leaf *identifier, LSL_Leaf *open, LSL_Leaf *params, LSL_Leaf *close, LSL_Leaf *block); LSL_Leaf *addOperation(LuaSL_compiler *compiler, LSL_Leaf *left, LSL_Leaf *lval, LSL_Leaf *right); LSL_Leaf *addParameter(LSL_Leaf *type, LSL_Leaf *newParam); LSL_Leaf *addParenthesis(LSL_Leaf *lval, LSL_Leaf *expr, LSL_Type type, LSL_Leaf *rval); diff --git a/LuaSL/src/LuaSL_compile.c b/LuaSL/src/LuaSL_compile.c index 277e278..445014e 100644 --- a/LuaSL/src/LuaSL_compile.c +++ b/LuaSL/src/LuaSL_compile.c @@ -196,8 +196,9 @@ static LSL_Leaf *newLeaf(LSL_Type type, LSL_Leaf *left, LSL_Leaf *right) return leaf; } -void burnLeaf(LSL_Leaf *leaf) +void burnLeaf(void *data) { + LSL_Leaf *leaf = data; if (leaf) { burnLeaf(leaf->left); @@ -326,7 +327,7 @@ LSL_Leaf *collectParameters(LSL_Leaf *list, LSL_Leaf *comma, LSL_Leaf *newParam) return newList; } -LSL_Leaf *addFunction(LSL_Leaf *type, LSL_Leaf *identifier, LSL_Leaf *open, LSL_Leaf *params, LSL_Leaf *close, LSL_Leaf *block) +LSL_Leaf *addFunction(LuaSL_compiler *compiler, LSL_Leaf *type, LSL_Leaf *identifier, LSL_Leaf *open, LSL_Leaf *params, LSL_Leaf *close, LSL_Leaf *block) { LSL_Function *func = calloc(1, sizeof(LSL_Function)); @@ -334,11 +335,9 @@ LSL_Leaf *addFunction(LSL_Leaf *type, LSL_Leaf *identifier, LSL_Leaf *open, LSL_ { if (identifier) { - const char *temp = identifier->value.stringValue; - + func->name = identifier->value.stringValue; identifier->token = tokens[LSL_FUNCTION - lowestToken]; identifier->value.functionValue = func; - identifier->value.functionValue->name = temp; identifier->value.functionValue->block = block; func->type = type; if (type) @@ -346,6 +345,7 @@ LSL_Leaf *addFunction(LSL_Leaf *type, LSL_Leaf *identifier, LSL_Leaf *open, LSL_ else identifier->basicType = OT_nothing; func->params = addParenthesis(open, params, LSL_PARAMETER_LIST, close); + eina_hash_add(compiler->script.functions, func->name, identifier); } } return identifier; @@ -380,9 +380,7 @@ LSL_Leaf *addState(LuaSL_compiler *compiler, LSL_Leaf *identifier, LSL_Leaf *blo result->name = identifier->value.stringValue; result->block = block; identifier->value.stateValue = result; - compiler->script.scount++; - compiler->script.states = realloc(compiler->script.states, compiler->script.scount * sizeof(LSL_State *)); - compiler->script.states[compiler->script.scount - 1] = result; + eina_hash_add(compiler->script.states, result->name, identifier); } return identifier; @@ -446,17 +444,9 @@ LSL_Leaf *addVariable(LuaSL_compiler *compiler, LSL_Leaf *type, LSL_Leaf *identi result->value.basicType = type->basicType; } if (compiler->currentBlock) - { -// compiler->currentBlock->vcount++; -// compiler->currentBlock->variables = realloc(compiler->currentBlock->variables, compiler->currentBlock->vcount * sizeof(LSL_Identifier *)); -// compiler->currentBlock->variables[compiler->currentBlock->vcount - 1] = result; - } + eina_hash_add(compiler->currentBlock->variables, result->name, identifier); else - { - compiler->script.vcount++; - compiler->script.variables = realloc(compiler->script.variables, compiler->script.vcount * sizeof(LSL_Identifier *)); - compiler->script.variables[compiler->script.vcount - 1] = result; - } + eina_hash_add(compiler->script.variables, result->name, identifier); } return identifier; @@ -468,6 +458,7 @@ void beginBlock(LuaSL_compiler *compiler, LSL_Leaf *block) if (blok) { + blok->variables = eina_hash_string_superfast_new(burnLeaf); block->value.blockValue = blok; blok->outerBlock = compiler->currentBlock; compiler->currentBlock = blok; @@ -947,6 +938,9 @@ Eina_Bool compileLSL(gameGlobals *game, char *script) memset(&compiler, 0, sizeof(LuaSL_compiler)); compiler.game = game; + compiler.script.functions = eina_hash_string_superfast_new(burnLeaf); + compiler.script.states = eina_hash_string_superfast_new(burnLeaf); + compiler.script.variables = eina_hash_string_superfast_new(burnLeaf); compiler.ignorableText = eina_strbuf_new(); strncpy(compiler.fileName, script, PATH_MAX - 1); diff --git a/LuaSL/src/LuaSL_lemon_yaccer.y b/LuaSL/src/LuaSL_lemon_yaccer.y index e62776b..e163991 100644 --- a/LuaSL/src/LuaSL_lemon_yaccer.y +++ b/LuaSL/src/LuaSL_lemon_yaccer.y @@ -45,8 +45,8 @@ parameterList(A) ::= parameter(D). { A = collectParameters(NULL, NULL, D); parameterList(A) ::= . { A = collectParameters(NULL, NULL, NULL); } parameter(A) ::= type(B) LSL_IDENTIFIER(C). { A = addParameter(B, C); } // Causes a conflict when it's an empty parameterList with calling the same type of function. -function(A) ::= LSL_IDENTIFIER(C) LSL_PARENTHESIS_OPEN(D) parameterList(E) LSL_PARENTHESIS_CLOSE(F) funcBlock(G). { A = addFunction(NULL, C, D, E, F, G); } -function(A) ::= type(B) LSL_IDENTIFIER(C) LSL_PARENTHESIS_OPEN(D) parameterList(E) LSL_PARENTHESIS_CLOSE(F) funcBlock(G). { A = addFunction(B, C, D, E, F, G); } +function(A) ::= LSL_IDENTIFIER(C) LSL_PARENTHESIS_OPEN(D) parameterList(E) LSL_PARENTHESIS_CLOSE(F) funcBlock(G). { A = addFunction(compiler, NULL, C, D, E, F, G); } +function(A) ::= type(B) LSL_IDENTIFIER(C) LSL_PARENTHESIS_OPEN(D) parameterList(E) LSL_PARENTHESIS_CLOSE(F) funcBlock(G). { A = addFunction(compiler, B, C, D, E, F, G); } // Blocks. -- cgit v1.1