From 8b71bc2d2905ca2ded0789a5534efc914ea84858 Mon Sep 17 00:00:00 2001 From: David Walter Seikel Date: Mon, 30 Jan 2012 13:33:11 +1000 Subject: Change the way blocks start and end, the look ahead was screwing things. --- LuaSL/src/LuaSL_LSL_tree.h | 5 +++-- LuaSL/src/LuaSL_compile.c | 25 ++++++++++++++++--------- LuaSL/src/LuaSL_lemon_yaccer.y | 6 +++++- LuaSL/src/LuaSL_lexer.l | 4 ++-- 4 files changed, 26 insertions(+), 14 deletions(-) (limited to 'LuaSL') diff --git a/LuaSL/src/LuaSL_LSL_tree.h b/LuaSL/src/LuaSL_LSL_tree.h index 24f838a..e5a415a 100644 --- a/LuaSL/src/LuaSL_LSL_tree.h +++ b/LuaSL/src/LuaSL_LSL_tree.h @@ -360,6 +360,7 @@ typedef struct #endif LSL_Leaf *lval; LSL_Block *currentBlock; + LSL_Function *currentFunction; Eina_Clist danglingCalls; // HEAD for function calls used before the function is defined. int column, line; int undeclared; @@ -372,6 +373,7 @@ typedef struct void burnLeaf(void *data); +LSL_Leaf *addBlock(LuaSL_compiler *compiler, LSL_Leaf *left, LSL_Leaf *lval, LSL_Leaf *right); LSL_Leaf *addCrement(LuaSL_compiler *compiler, LSL_Leaf *variable, LSL_Leaf *crement); LSL_Leaf *addFunction(LuaSL_compiler *compiler, LSL_Leaf *type, LSL_Leaf *identifier, LSL_Leaf *open, LSL_Leaf *params, LSL_Leaf *close); LSL_Leaf *addFunctionBody(LuaSL_compiler *compiler, LSL_Leaf *function, LSL_Leaf *block); @@ -384,11 +386,10 @@ LSL_Leaf *addStatement(LuaSL_compiler *compiler, LSL_Leaf *lval, LSL_Type type, LSL_Leaf *addTypecast(LSL_Leaf *lval, LSL_Leaf *type, LSL_Leaf *rval, LSL_Leaf *expr); LSL_Leaf *addVariable(LuaSL_compiler *compiler, LSL_Leaf *type, LSL_Leaf *identifier, LSL_Leaf *assignment, LSL_Leaf *expr); -void beginBlock(LuaSL_compiler *compiler, LSL_Leaf *block); +LSL_Leaf *beginBlock(LuaSL_compiler *compiler, LSL_Leaf *block); LSL_Leaf *checkVariable(LuaSL_compiler *compiler, LSL_Leaf *identifier); LSL_Leaf *collectParameters(LuaSL_compiler *compiler, LSL_Leaf *list, LSL_Leaf *comma, LSL_Leaf *newParam); LSL_Leaf *collectStatements(LuaSL_compiler *compiler, LSL_Leaf *list, LSL_Leaf *newStatement); -void endBlock(LuaSL_compiler *compiler, LSL_Leaf *block); void *ParseAlloc(void *(*mallocProc)(size_t)); void ParseTrace(FILE *TraceFILE, char *zTracePrompt); diff --git a/LuaSL/src/LuaSL_compile.c b/LuaSL/src/LuaSL_compile.c index 48f2910..e4a526a 100644 --- a/LuaSL/src/LuaSL_compile.c +++ b/LuaSL/src/LuaSL_compile.c @@ -446,6 +446,13 @@ LSL_Leaf *addOperation(LuaSL_compiler *compiler, LSL_Leaf *left, LSL_Leaf *lval, return lval; } +LSL_Leaf *addBlock(LuaSL_compiler *compiler, LSL_Leaf *left, LSL_Leaf *lval, LSL_Leaf *right) +{ + // Damn, look ahead. The } symbol is getting read (and thus endBlock called) before the last statement in the block is reduced (which actually calls the add*() functions). + compiler->currentBlock = compiler->currentBlock->outerBlock; + return lval; +} + LSL_Leaf *addCrement(LuaSL_compiler *compiler, LSL_Leaf *variable, LSL_Leaf *crement) { if ((variable) && (crement)) @@ -541,8 +548,7 @@ LSL_Leaf *addFunction(LuaSL_compiler *compiler, LSL_Leaf *type, LSL_Leaf *identi func->params = addParenthesis(open, params, LSL_PARAMETER_LIST, close); #endif } - if (compiler->currentBlock) - compiler->currentBlock->function = func; + compiler->currentFunction = func; } } @@ -725,12 +731,13 @@ LSL_Leaf *addStatement(LuaSL_compiler *compiler, LSL_Leaf *lval, LSL_Type type, LSL_Leaf *collectStatements(LuaSL_compiler *compiler, LSL_Leaf *list, LSL_Leaf *statement) { + boolean wasNull = FALSE; if (NULL == list) { list = newLeaf(LSL_BLOCK_OPEN, NULL, NULL); if (list) { - list->value.blockValue = compiler->currentBlock; // Maybe NULL. + wasNull = TRUE; } } @@ -738,6 +745,8 @@ LSL_Leaf *collectStatements(LuaSL_compiler *compiler, LSL_Leaf *list, LSL_Leaf * { if (statement) { + if (!wasNull) + list->value.blockValue = compiler->currentBlock; // Maybe NULL. if (list->value.blockValue) { eina_clist_add_tail(&(list->value.blockValue->statements), &(statement->value.statementValue->statement)); @@ -792,7 +801,7 @@ LSL_Leaf *addVariable(LuaSL_compiler *compiler, LSL_Leaf *type, LSL_Leaf *identi return identifier; } -void beginBlock(LuaSL_compiler *compiler, LSL_Leaf *block) +LSL_Leaf *beginBlock(LuaSL_compiler *compiler, LSL_Leaf *block) { LSL_Block *blok = calloc(1, sizeof(LSL_Block)); @@ -803,12 +812,10 @@ void beginBlock(LuaSL_compiler *compiler, LSL_Leaf *block) block->value.blockValue = blok; blok->outerBlock = compiler->currentBlock; compiler->currentBlock = blok; + blok->function = compiler->currentFunction; + compiler->currentFunction = NULL; } -} - -void endBlock(LuaSL_compiler *compiler, LSL_Leaf *block) -{ - compiler->currentBlock = compiler->currentBlock->outerBlock; + return block; } static void secondPass(LuaSL_compiler *compiler, LSL_Leaf *leaf) diff --git a/LuaSL/src/LuaSL_lemon_yaccer.y b/LuaSL/src/LuaSL_lemon_yaccer.y index 125b384..ed53fd4 100644 --- a/LuaSL/src/LuaSL_lemon_yaccer.y +++ b/LuaSL/src/LuaSL_lemon_yaccer.y @@ -31,7 +31,7 @@ script ::= . // State definitions. %nonassoc LSL_BLOCK_OPEN LSL_BLOCK_CLOSE LSL_STATE. -stateBlock(A) ::= LSL_BLOCK_OPEN functionList(B) LSL_BLOCK_CLOSE. { A = B; } +stateBlock(A) ::= beginBlock(L) functionList(B) LSL_BLOCK_CLOSE(R). { A = addBlock(compiler, L, B, R); } state(S) ::= LSL_DEFAULT(I) stateBlock(B). { S = addState(compiler, I, B); } state(S) ::= LSL_STATE_CHANGE LSL_IDENTIFIER(I) stateBlock(B). { S = addState(compiler, I, B); } @@ -57,6 +57,8 @@ function(A) ::= type(B) LSL_IDENTIFIER(C) LSL_PARENTHESIS_OPEN(D) parameterList( block(A) ::= funcBlock(B). { A = B; } block(A) ::= statement(B). { A = B; } funcBlock(A) ::= LSL_BLOCK_OPEN statementList(B) LSL_BLOCK_CLOSE. { A = B; } +// Perhaps change this to block? No ,this is what differentiates it from a single statement, which functions can't handle. +funcBlock(A) ::= beginBlock(L) statementList(B) LSL_BLOCK_CLOSE(R). { A = addBlock(compiler, L, B, R); } // Various forms of statement. @@ -85,6 +87,8 @@ statement(A) ::= LSL_WHILE(F) LSL_PARENTHESIS_OPEN(L) expr(E) LSL_PARENTHESIS_CL %nonassoc LSL_LABEL. statement(A) ::= LSL_LABEL(F) LSL_IDENTIFIER(I) LSL_STATEMENT(S). { A = addStatement(compiler, S, F->toKen->type, NULL, NULL, NULL, NULL, I); } +beginBlock(A) ::= LSL_BLOCK_OPEN(B). { A = beginBlock(compiler, B); } + // This might be bogus, or might be valid LSL, but it lets us test the expression parser by evaluating them. statement(A) ::= expr(E) LSL_STATEMENT(S). { A = addStatement(compiler, S, LSL_EXPRESSION, NULL, E, NULL, NULL, NULL); } diff --git a/LuaSL/src/LuaSL_lexer.l b/LuaSL/src/LuaSL_lexer.l index c66a3a8..0c7d627 100644 --- a/LuaSL/src/LuaSL_lexer.l +++ b/LuaSL/src/LuaSL_lexer.l @@ -76,8 +76,8 @@ STRING \"(\\.|[^\\"\n])*\" /* Other symbols. */ "@" %{ return common(yylval, yytext, yyleng, yyextra, TRUE, LSL_LABEL); %} -"{" %{ beginBlock(yyextra, yylval); return common(yylval, yytext, yyleng, yyextra, TRUE, LSL_BLOCK_OPEN); %} -"}" %{ endBlock(yyextra, yylval); return common(yylval, yytext, yyleng, yyextra, TRUE, LSL_BLOCK_CLOSE); %} +"{" %{ return common(yylval, yytext, yyleng, yyextra, TRUE, LSL_BLOCK_OPEN); %} +"}" %{ return common(yylval, yytext, yyleng, yyextra, TRUE, LSL_BLOCK_CLOSE); %} ";" %{ return common(yylval, yytext, yyleng, yyextra, TRUE, LSL_STATEMENT); %} /* Type keywords. */ -- cgit v1.1