Commit 0162716a authored by Jack's avatar Jack
Browse files

added hack to handle return in single line if statement, still to test it thoroughly

parent 93f377ce
......@@ -44,7 +44,7 @@ static const char *const luaX_tokens [] = {
"return", "then", "true", "until", "while",
"//", "..", "...", "==", ">=", "<=", "~=", "!=",
"+=", "-=", "*=", "/=",
"<<", ">>", "::", "<eof>",
"<<", ">>", "::", "<eof>", "<eol>"
"<number>", "<integer>", "<name>", "<string>"
};
......@@ -172,6 +172,7 @@ void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, TString *source,
ls->lastline = 1;
ls->source = source;
ls->envn = luaS_newliteral(L, LUA_ENV); /* get env name */
ls->ignorenewline = 1;
luaZ_resizebuffer(ls->L, ls->buff, LUA_MINBUFFER); /* initialize buffer */
}
......@@ -432,7 +433,9 @@ static int llex (LexState *ls, SemInfo *seminfo) {
switch (ls->current) {
case '\n': case '\r': { /* line breaks */
inclinenumber(ls);
break;
if (ls->ignorenewline)
break;
return TK_EOL;
}
case ' ': case '\f': case '\t': case '\v': { /* spaces */
next(ls);
......
......@@ -33,7 +33,7 @@ enum RESERVED {
TK_IDIV, TK_CONCAT, TK_DOTS, TK_EQ, TK_GE, TK_LE, TK_NE, TK_NE2,
TK_ASSADD, TK_ASSSUB, TK_ASSMUL, TK_ASSDIV, TK_ASSMOD,
TK_SHL, TK_SHR,
TK_DBCOLON, TK_EOS,
TK_DBCOLON, TK_EOS, TK_EOL,
TK_FLT, TK_INT, TK_NAME, TK_STRING
};
......@@ -57,6 +57,7 @@ typedef struct Token {
/* state of the lexer plus state of the parser when shared by all
functions */
typedef struct LexState {
int ignorenewline;
int current; /* current character (charint) */
int linenumber; /* input line counter */
int lastline; /* line of last token 'consumed' */
......
......@@ -589,6 +589,8 @@ static int block_follow (LexState *ls, int withuntil) {
case TK_ELSE: case TK_ELSEIF:
case TK_END: case TK_EOS:
return 1;
case TK_EOL:
return ls->ignorenewline ? 0 : 1;
case TK_UNTIL: return withuntil;
default: return 0;
}
......@@ -1431,19 +1433,22 @@ static void inline_if(LexState* ls, expdesc* v)
enterblock(fs, &bl, 0);
jf = v->f;
/* handling return manually because otherwise any valid LHS
would be parsed as return value for return statement */
if (ls->t.token == TK_RETURN)
retstat(ls);
else
statlist(ls); /* parse true block */
ls->ignorenewline = 0;
statlist(ls); /* parse true block */
ls->ignorenewline = 1;
while (testnext(ls, TK_EOL));
leaveblock(fs);
luaK_patchtohere(fs, jf);
if (testnext(ls, TK_ELSE))
{
ls->ignorenewline = 0;
block(ls); /* 'else' part */
ls->ignorenewline = 1;
while (testnext(ls, TK_EOL));
}
}
static int test_then_block (LexState *ls, int *escapelist) {
......@@ -1590,7 +1595,7 @@ static void retstat (LexState *ls) {
FuncState *fs = ls->fs;
expdesc e;
int first, nret; /* registers with returned values */
if (block_follow(ls, 1) || ls->t.token == ';')
if (block_follow(ls, 1) || ls->t.token == ';' || (ls->t.token == TK_EOL && !ls->ignorenewline))
first = nret = 0; /* return no values */
else {
nret = explist(ls, &e); /* optional return values */
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment