From 219498b453584dae9ffe7b71ffd97d3f5a0f9f60 Mon Sep 17 00:00:00 2001 From: David Walter Seikel Date: Fri, 3 Feb 2012 11:56:17 +1000 Subject: LSL "else if" -> Lua "elseif", and it was a bitch. lol --- LuaSL/src/LuaSL_compile.c | 137 ++++++++++++++++------------------------- LuaSL/src/LuaSL_lemon_yaccer.y | 9 ++- LuaSL/src/LuaSL_lexer.l | 1 + 3 files changed, 60 insertions(+), 87 deletions(-) (limited to 'LuaSL') diff --git a/LuaSL/src/LuaSL_compile.c b/LuaSL/src/LuaSL_compile.c index e13c7e7..3488545 100644 --- a/LuaSL/src/LuaSL_compile.c +++ b/LuaSL/src/LuaSL_compile.c @@ -127,6 +127,7 @@ LSL_Token LSL_Tokens[] = {LSL_DO, ST_NONE, "do", LSL_NONE, NULL, NULL}, {LSL_FOR, ST_NONE, "for", LSL_NONE, NULL, NULL}, {LSL_ELSE, ST_NONE, "else", LSL_NONE, NULL, NULL}, + {LSL_ELSEIF, ST_NONE, "elseif", LSL_NONE, NULL, NULL}, {LSL_IF, ST_NONE, "if", LSL_NONE, NULL, NULL}, {LSL_JUMP, ST_NONE, "jump", LSL_NONE, NULL, NULL}, {LSL_RETURN, ST_NONE, "return", LSL_NONE, NULL, NULL}, @@ -837,7 +838,17 @@ LSL_Leaf *addState(LuaSL_compiler *compiler, LSL_Leaf *state, LSL_Leaf *identifi LSL_Leaf *addIfElse(LuaSL_compiler *compiler, LSL_Leaf *ifBlock, LSL_Leaf *elseBlock) { - ifBlock->value.statementValue->elseBlock = elseBlock->value.statementValue; + if (ifBlock->value.statementValue->elseBlock) + { + LSL_Statement *oldElseIf = ifBlock->value.statementValue->elseBlock; + + while (oldElseIf->elseBlock) + oldElseIf = oldElseIf->elseBlock; + + oldElseIf->elseBlock = elseBlock->value.statementValue; + } + else + ifBlock->value.statementValue->elseBlock = elseBlock->value.statementValue; return ifBlock; } @@ -924,6 +935,11 @@ LSL_Leaf *addStatement(LuaSL_compiler *compiler, LSL_Leaf *lval, LSL_Leaf *flow, justOne = TRUE; break; } + case LSL_ELSEIF : + { + justOne = TRUE; + break; + } case LSL_JUMP : { justOne = TRUE; @@ -1432,6 +1448,10 @@ static LSL_Leaf *evaluateStatementToken(LSL_Leaf *content, LSL_Leaf *left, LSL_L { break; } + case LSL_ELSEIF : + { + break; + } case LSL_JUMP : { break; @@ -1519,7 +1539,7 @@ static void outputLeaf(FILE *file, outputMode mode, LSL_Leaf *leaf) // Circular references, so declare this one first. static void outputRawStatement(FILE *file, outputMode mode, LSL_Statement *statement); -static void outputRawBlock(FILE *file, outputMode mode, LSL_Block *block) +static void outputRawBlock(FILE *file, outputMode mode, LSL_Block *block, boolean doEnd) { if (block) { @@ -1533,7 +1553,7 @@ static void outputRawBlock(FILE *file, outputMode mode, LSL_Block *block) #else if (OM_LSL == mode) fprintf(file, "\n{\n"); - else if (OM_LUA == mode) + else if (doEnd && (OM_LUA == mode)) fprintf(file, "\n"); #endif EINA_CLIST_FOR_EACH_ENTRY(stat, &(block->statements), LSL_Statement, statement) @@ -1546,7 +1566,7 @@ static void outputRawBlock(FILE *file, outputMode mode, LSL_Block *block) #endif if (OM_LSL == mode) fprintf(file, "}"); - else if (OM_LUA == mode) + else if (doEnd && (OM_LUA == mode)) fprintf(file, "end "); } } @@ -1611,8 +1631,8 @@ static void outputRawStatement(FILE *file, outputMode mode, LSL_Statement *state case LSL_FOR : { #if LUASL_DIFF_CHECK - if ((statement->ignorable) && (statement->ignorable[1])) - fwrite(eina_strbuf_string_get(statement->ignorable[1]), 1, eina_strbuf_length_get(statement->ignorable[1]), file); + if ((statement->ignorable) && (statement->ignorable[1])) + fwrite(eina_strbuf_string_get(statement->ignorable[1]), 1, eina_strbuf_length_get(statement->ignorable[1]), file); #endif if (OM_LSL == mode) { @@ -1632,22 +1652,7 @@ static void outputRawStatement(FILE *file, outputMode mode, LSL_Statement *state fprintf(file, ") do\n"); #endif if (statement->block) - { - LSL_Statement *stat = NULL; - -#if LUASL_DIFF_CHECK - if (statement->block->openIgnorable) - fwrite(eina_strbuf_string_get(statement->block->openIgnorable), 1, eina_strbuf_length_get(statement->block->openIgnorable), file); -#endif - EINA_CLIST_FOR_EACH_ENTRY(stat, &(statement->block->statements), LSL_Statement, statement) - { - outputRawStatement(file, mode, stat); - } -#if LUASL_DIFF_CHECK - if (statement->block->closeIgnorable) - fwrite(eina_strbuf_string_get(statement->block->closeIgnorable), 1, eina_strbuf_length_get(statement->block->closeIgnorable), file); -#endif - } + outputRawBlock(file, mode, statement->block, FALSE); if (statement->single) outputRawStatement(file, mode, statement->single); fprintf(file, "\n"); @@ -1658,77 +1663,41 @@ static void outputRawStatement(FILE *file, outputMode mode, LSL_Statement *state break; } case LSL_IF : + case LSL_ELSE : + case LSL_ELSEIF : { isBlock = TRUE; #if LUASL_DIFF_CHECK - if ((statement->ignorable) && (statement->ignorable[1])) - fwrite(eina_strbuf_string_get(statement->ignorable[1]), 1, eina_strbuf_length_get(statement->ignorable[1]), file); + if ((statement->ignorable) && (statement->ignorable[1])) + fwrite(eina_strbuf_string_get(statement->ignorable[1]), 1, eina_strbuf_length_get(statement->ignorable[1]), file); #endif fprintf(file, "%s", tokens[statement->type - lowestToken]->toKen); if (OM_LUA == mode) { - if (statement->parenthesis) - outputRawParenthesisToken(file, mode, statement->parenthesis, ""); - else - outputLeaf(file, mode, statement->expressions); - fprintf(file, " then\n"); - if (statement->block) + if (LSL_ELSE != statement->type) { - LSL_Statement *stat = NULL; - -#if LUASL_DIFF_CHECK - if (statement->block->openIgnorable) - fwrite(eina_strbuf_string_get(statement->block->openIgnorable), 1, eina_strbuf_length_get(statement->block->openIgnorable), file); -#endif - EINA_CLIST_FOR_EACH_ENTRY(stat, &(statement->block->statements), LSL_Statement, statement) - { - outputRawStatement(file, mode, stat); - } -#if LUASL_DIFF_CHECK - if (statement->block->closeIgnorable) - fwrite(eina_strbuf_string_get(statement->block->closeIgnorable), 1, eina_strbuf_length_get(statement->block->closeIgnorable), file); -#endif + fprintf(file, " "); + if (statement->parenthesis) + outputRawParenthesisToken(file, mode, statement->parenthesis, ""); + else + outputLeaf(file, mode, statement->expressions); + fprintf(file, " then\n"); } + if (statement->block) + outputRawBlock(file, mode, statement->block, FALSE); if (statement->single) outputRawStatement(file, mode, statement->single); if (statement->elseBlock) outputRawStatement(file, mode, statement->elseBlock); - fprintf(file, "\nend\n"); - return; - } - break; - } - case LSL_ELSE : - { - isBlock = TRUE; -#if LUASL_DIFF_CHECK - if ((statement->ignorable) && (statement->ignorable[1])) - fwrite(eina_strbuf_string_get(statement->ignorable[1]), 1, eina_strbuf_length_get(statement->ignorable[1]), file); -#endif - fprintf(file, "%s", tokens[statement->type - lowestToken]->toKen); - if (OM_LUA == mode) - { - // TODO - look ahead to se if it's an elseif. - // Or not, seems to have happened by accident. lol - if (statement->block) + if (LSL_IF == statement->type) { - LSL_Statement *stat = NULL; - -#if LUASL_DIFF_CHECK - if (statement->block->openIgnorable) - fwrite(eina_strbuf_string_get(statement->block->openIgnorable), 1, eina_strbuf_length_get(statement->block->openIgnorable), file); -#endif - EINA_CLIST_FOR_EACH_ENTRY(stat, &(statement->block->statements), LSL_Statement, statement) - { - outputRawStatement(file, mode, stat); - } -#if LUASL_DIFF_CHECK - if (statement->block->closeIgnorable) - fwrite(eina_strbuf_string_get(statement->block->closeIgnorable), 1, eina_strbuf_length_get(statement->block->closeIgnorable), file); -#endif + fprintf(file, " end --[["); + if (statement->parenthesis) + outputRawParenthesisToken(file, mode, statement->parenthesis, ""); + else + outputLeaf(file, mode, statement->expressions); + fprintf(file, "]]\n"); } - if (statement->single) - outputRawStatement(file, mode, statement->single); return; } break; @@ -1783,7 +1752,7 @@ static void outputRawStatement(FILE *file, outputMode mode, LSL_Statement *state outputRawParenthesisToken(file, mode, statement->parenthesis, ""); fprintf(file, " do "); if (statement->block) - outputRawBlock(file, mode, statement->block); + outputRawBlock(file, mode, statement->block, TRUE); if (statement->single) outputRawStatement(file, mode, statement->single); fprintf(file, "\n"); @@ -1837,7 +1806,7 @@ static void outputRawStatement(FILE *file, outputMode mode, LSL_Statement *state outputLeaf(file, mode, statement->expressions); if (statement->block) - outputRawBlock(file, mode, statement->block); + outputRawBlock(file, mode, statement->block, TRUE); if (statement->single) outputRawStatement(file, mode, statement->single); @@ -1861,7 +1830,7 @@ static void outputRawStatement(FILE *file, outputMode mode, LSL_Statement *state static void outputBlockToken(FILE *file, outputMode mode, LSL_Leaf *content) { if (content) - outputRawBlock(file, mode, content->value.blockValue); + outputRawBlock(file, mode, content->value.blockValue, TRUE); } static void outputCrementsToken(FILE *file, outputMode mode, LSL_Leaf *content) @@ -1929,7 +1898,7 @@ static void outputFunctionToken(FILE *file, outputMode mode, LSL_Leaf *content) first = FALSE; } fprintf(file, ")"); - outputRawBlock(file, mode, func->block); + outputRawBlock(file, mode, func->block, TRUE); if (!LUASL_DIFF_CHECK) fprintf(file, "\n"); } @@ -1951,7 +1920,7 @@ static void outputFunctionToken(FILE *file, outputMode mode, LSL_Leaf *content) first = FALSE; } fprintf(file, ")"); - outputRawBlock(file, mode, func->block); + outputRawBlock(file, mode, func->block, TRUE); } } } @@ -2085,7 +2054,7 @@ static void outputStateToken(FILE *file, outputMode mode, LSL_Leaf *content) { outputText(file, &(state->state), !(LSL_NOIGNORE & content->toKen->flags)); outputText(file, &(state->name), !(LSL_NOIGNORE & content->toKen->flags)); - outputRawBlock(file, mode, state->block); + outputRawBlock(file, mode, state->block, TRUE); } } } diff --git a/LuaSL/src/LuaSL_lemon_yaccer.y b/LuaSL/src/LuaSL_lemon_yaccer.y index 9199699..716c65a 100644 --- a/LuaSL/src/LuaSL_lemon_yaccer.y +++ b/LuaSL/src/LuaSL_lemon_yaccer.y @@ -66,15 +66,18 @@ statementList(A) ::= statementList(B) statement(C). { A = collectSt statementList(A) ::= . { A = collectStatements(compiler, NULL, NULL); } %nonassoc LSL_DO LSL_FOR LSL_IF LSL_JUMP LSL_RETURN LSL_STATE_CHANGE LSL_WHILE. -%nonassoc LSL_ELSE. +%nonassoc LSL_ELSE LSL_ELSEIF. statement(A) ::= LSL_DO(F) block(B) LSL_WHILE(W) LSL_PARENTHESIS_OPEN(L) expr(E) LSL_PARENTHESIS_CLOSE(R) LSL_STATEMENT(S). { A = addStatement(compiler, S, F, L, E, R, B, W); } statement(A) ::= LSL_FOR(F) LSL_PARENTHESIS_OPEN(L) expr(E0) LSL_STATEMENT(S0) expr(E1) LSL_STATEMENT(S1) expr(E2) LSL_PARENTHESIS_CLOSE(R) block(B). { A = addFor(compiler, NULL, F, L, E0, S0, E1, S1, E2, R, B); } statement(A) ::= LSL_FOR(F) LSL_PARENTHESIS_OPEN(L) expr(E0) LSL_STATEMENT(S0) expr(E1) LSL_STATEMENT(S1) expr(E2) LSL_PARENTHESIS_CLOSE(R) statement(S). { A = addFor(compiler, NULL, F, L, E0, S0, E1, S1, E2, R, S); } statement(A) ::= ifBlock(B). { A = B; } +ifBlock(A) ::= ifBlock(B) elseIfBlock(E). { A = addIfElse(compiler, B, E); } ifBlock(A) ::= ifBlock(B) elseBlock(E). { A = addIfElse(compiler, B, E); } -ifBlock(A) ::= LSL_IF(F) LSL_PARENTHESIS_OPEN(L) expr(E) LSL_PARENTHESIS_CLOSE(R) block(B). [LSL_ELSE] { A = addStatement(compiler, NULL, F, L, E, R, B, NULL); } -ifBlock(A) ::= LSL_IF(F) LSL_PARENTHESIS_OPEN(L) expr(E) LSL_PARENTHESIS_CLOSE(R) statement(S). [LSL_ELSE] { A = addStatement(compiler, NULL, F, L, E, R, S, NULL); } +ifBlock(A) ::= LSL_IF(F) LSL_PARENTHESIS_OPEN(L) expr(E) LSL_PARENTHESIS_CLOSE(R) block(B). [LSL_ELSE] { A = addStatement(compiler, NULL, F, L, E, R, B, NULL); } +ifBlock(A) ::= LSL_IF(F) LSL_PARENTHESIS_OPEN(L) expr(E) LSL_PARENTHESIS_CLOSE(R) statement(S). [LSL_ELSE] { A = addStatement(compiler, NULL, F, L, E, R, S, NULL); } +elseIfBlock(A) ::= LSL_ELSEIF(F) LSL_PARENTHESIS_OPEN(L) expr(E) LSL_PARENTHESIS_CLOSE(R) block(B). [LSL_ELSEIF] { A = addStatement(compiler, NULL, F, L, E, R, B, NULL); } +elseIfBlock(A) ::= LSL_ELSEIF(F) LSL_PARENTHESIS_OPEN(L) expr(E) LSL_PARENTHESIS_CLOSE(R) statement(S). [LSL_ELSEIF] { A = addStatement(compiler, NULL, F, L, E, R, S, NULL); } elseBlock(A) ::= LSL_ELSE(F) block(B). { A = addStatement(compiler, NULL, F, NULL, NULL, NULL, B, NULL); } elseBlock(A) ::= LSL_ELSE(F) statement(S). { A = addStatement(compiler, NULL, F, NULL, NULL, NULL, S, NULL); } diff --git a/LuaSL/src/LuaSL_lexer.l b/LuaSL/src/LuaSL_lexer.l index adee1a7..76d1b5b 100644 --- a/LuaSL/src/LuaSL_lexer.l +++ b/LuaSL/src/LuaSL_lexer.l @@ -96,6 +96,7 @@ STRING \"(\\.|[^\\"\n])*\" "for" %{ return common(yylval, yytext, yyleng, yyextra, TRUE, LSL_FOR); %} "else" %{ return common(yylval, yytext, yyleng, yyextra, TRUE, LSL_ELSE); %} "if" %{ return common(yylval, yytext, yyleng, yyextra, TRUE, LSL_IF); %} +"else"[[:space:]]+"if" %{ return common(yylval, yytext, yyleng, yyextra, TRUE, LSL_ELSEIF); %} /* TODO - deal with people that slap a comment in between them. */ "jump" %{ return common(yylval, yytext, yyleng, yyextra, TRUE, LSL_JUMP); %} "return" %{ return common(yylval, yytext, yyleng, yyextra, TRUE, LSL_RETURN); %} "state" %{ return common(yylval, yytext, yyleng, yyextra, TRUE, LSL_STATE_CHANGE); %} -- cgit v1.1