better line numbers for function calls and unary/binary operators +

null statement + no more "ambiguous syntax" restriction
This commit is contained in:
Roberto Ierusalimschy 2010-04-17 09:46:32 -03:00
parent 1e6940f291
commit c066ac10a9
3 changed files with 27 additions and 20 deletions

19
lcode.c
View File

@ -1,5 +1,5 @@
/* /*
** $Id: lcode.c,v 2.44 2010/02/26 20:40:29 roberto Exp roberto $ ** $Id: lcode.c,v 2.45 2010/03/12 19:14:06 roberto Exp roberto $
** Code generator for Lua ** Code generator for Lua
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -719,7 +719,8 @@ static int constfolding (OpCode op, expdesc *e1, expdesc *e2) {
} }
static void codearith (FuncState *fs, OpCode op, expdesc *e1, expdesc *e2) { static void codearith (FuncState *fs, OpCode op,
expdesc *e1, expdesc *e2, int line) {
if (constfolding(op, e1, e2)) if (constfolding(op, e1, e2))
return; return;
else { else {
@ -735,6 +736,7 @@ static void codearith (FuncState *fs, OpCode op, expdesc *e1, expdesc *e2) {
} }
e1->u.s.info = luaK_codeABC(fs, op, 0, o1, o2); e1->u.s.info = luaK_codeABC(fs, op, 0, o1, o2);
e1->k = VRELOCABLE; e1->k = VRELOCABLE;
luaK_fixline(fs, line);
} }
} }
@ -755,7 +757,7 @@ static void codecomp (FuncState *fs, OpCode op, int cond, expdesc *e1,
} }
void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e) { void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e, int line) {
expdesc e2; expdesc e2;
e2.t = e2.f = NO_JUMP; e2.k = VKNUM; e2.u.nval = 0; e2.t = e2.f = NO_JUMP; e2.k = VKNUM; e2.u.nval = 0;
switch (op) { switch (op) {
@ -764,14 +766,14 @@ void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e) {
e->u.nval = luai_numunm(NULL, e->u.nval); /* fold it */ e->u.nval = luai_numunm(NULL, e->u.nval); /* fold it */
else { else {
luaK_exp2anyreg(fs, e); luaK_exp2anyreg(fs, e);
codearith(fs, OP_UNM, e, &e2); codearith(fs, OP_UNM, e, &e2, line);
} }
break; break;
} }
case OPR_NOT: codenot(fs, e); break; case OPR_NOT: codenot(fs, e); break;
case OPR_LEN: { case OPR_LEN: {
luaK_exp2anyreg(fs, e); /* cannot operate on constants */ luaK_exp2anyreg(fs, e); /* cannot operate on constants */
codearith(fs, OP_LEN, e, &e2); codearith(fs, OP_LEN, e, &e2, line);
break; break;
} }
default: lua_assert(0); default: lua_assert(0);
@ -806,7 +808,8 @@ void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) {
} }
void luaK_posfix (FuncState *fs, BinOpr op, expdesc *e1, expdesc *e2) { void luaK_posfix (FuncState *fs, BinOpr op,
expdesc *e1, expdesc *e2, int line) {
switch (op) { switch (op) {
case OPR_AND: { case OPR_AND: {
lua_assert(e1->t == NO_JUMP); /* list must be closed */ lua_assert(e1->t == NO_JUMP); /* list must be closed */
@ -832,13 +835,13 @@ void luaK_posfix (FuncState *fs, BinOpr op, expdesc *e1, expdesc *e2) {
} }
else { else {
luaK_exp2nextreg(fs, e2); /* operand must be on the 'stack' */ luaK_exp2nextreg(fs, e2); /* operand must be on the 'stack' */
codearith(fs, OP_CONCAT, e1, e2); codearith(fs, OP_CONCAT, e1, e2, line);
} }
break; break;
} }
case OPR_ADD: case OPR_SUB: case OPR_MUL: case OPR_DIV: case OPR_ADD: case OPR_SUB: case OPR_MUL: case OPR_DIV:
case OPR_MOD: case OPR_POW: { case OPR_MOD: case OPR_POW: {
codearith(fs, cast(OpCode, op - OPR_ADD + OP_ADD), e1, e2); codearith(fs, cast(OpCode, op - OPR_ADD + OP_ADD), e1, e2, line);
break; break;
} }
case OPR_EQ: case OPR_LT: case OPR_LE: { case OPR_EQ: case OPR_LT: case OPR_LE: {

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lcode.h,v 1.52 2009/09/23 20:33:05 roberto Exp roberto $ ** $Id: lcode.h,v 1.53 2010/02/26 20:40:29 roberto Exp roberto $
** Code generator for Lua ** Code generator for Lua
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -73,9 +73,10 @@ LUAI_FUNC void luaK_patchlist (FuncState *fs, int list, int target);
LUAI_FUNC void luaK_patchtohere (FuncState *fs, int list); LUAI_FUNC void luaK_patchtohere (FuncState *fs, int list);
LUAI_FUNC void luaK_concat (FuncState *fs, int *l1, int l2); LUAI_FUNC void luaK_concat (FuncState *fs, int *l1, int l2);
LUAI_FUNC int luaK_getlabel (FuncState *fs); LUAI_FUNC int luaK_getlabel (FuncState *fs);
LUAI_FUNC void luaK_prefix (FuncState *fs, UnOpr op, expdesc *v); LUAI_FUNC void luaK_prefix (FuncState *fs, UnOpr op, expdesc *v, int line);
LUAI_FUNC void luaK_infix (FuncState *fs, BinOpr op, expdesc *v); LUAI_FUNC void luaK_infix (FuncState *fs, BinOpr op, expdesc *v);
LUAI_FUNC void luaK_posfix (FuncState *fs, BinOpr op, expdesc *v1, expdesc *v2); LUAI_FUNC void luaK_posfix (FuncState *fs, BinOpr op, expdesc *v1,
expdesc *v2, int line);
LUAI_FUNC void luaK_setlist (FuncState *fs, int base, int nelems, int tostore); LUAI_FUNC void luaK_setlist (FuncState *fs, int base, int nelems, int tostore);

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lparser.c,v 2.81 2010/04/05 16:26:37 roberto Exp roberto $ ** $Id: lparser.c,v 2.82 2010/04/05 16:35:37 roberto Exp roberto $
** Lua Parser ** Lua Parser
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -670,15 +670,12 @@ static int explist1 (LexState *ls, expdesc *v) {
} }
static void funcargs (LexState *ls, expdesc *f) { static void funcargs (LexState *ls, expdesc *f, int line) {
FuncState *fs = ls->fs; FuncState *fs = ls->fs;
expdesc args; expdesc args;
int base, nparams; int base, nparams;
int line = ls->linenumber;
switch (ls->t.token) { switch (ls->t.token) {
case '(': { /* funcargs -> `(' [ explist1 ] `)' */ case '(': { /* funcargs -> `(' [ explist1 ] `)' */
if (line != ls->lastline)
luaX_syntaxerror(ls,"ambiguous syntax (function call x new statement)");
luaX_next(ls); luaX_next(ls);
if (ls->t.token == ')') /* arg list is empty? */ if (ls->t.token == ')') /* arg list is empty? */
args.k = VVOID; args.k = VVOID;
@ -755,6 +752,7 @@ static void primaryexp (LexState *ls, expdesc *v) {
/* primaryexp -> /* primaryexp ->
prefixexp { `.' NAME | `[' exp `]' | `:' NAME funcargs | funcargs } */ prefixexp { `.' NAME | `[' exp `]' | `:' NAME funcargs | funcargs } */
FuncState *fs = ls->fs; FuncState *fs = ls->fs;
int line = ls->linenumber;
prefixexp(ls, v); prefixexp(ls, v);
for (;;) { for (;;) {
switch (ls->t.token) { switch (ls->t.token) {
@ -774,12 +772,12 @@ static void primaryexp (LexState *ls, expdesc *v) {
luaX_next(ls); luaX_next(ls);
checkname(ls, &key); checkname(ls, &key);
luaK_self(fs, v, &key); luaK_self(fs, v, &key);
funcargs(ls, v); funcargs(ls, v, line);
break; break;
} }
case '(': case TK_STRING: case '{': { /* funcargs */ case '(': case TK_STRING: case '{': { /* funcargs */
luaK_exp2nextreg(fs, v); luaK_exp2nextreg(fs, v);
funcargs(ls, v); funcargs(ls, v, line);
break; break;
} }
default: return; default: return;
@ -894,9 +892,10 @@ static BinOpr subexpr (LexState *ls, expdesc *v, unsigned int limit) {
enterlevel(ls); enterlevel(ls);
uop = getunopr(ls->t.token); uop = getunopr(ls->t.token);
if (uop != OPR_NOUNOPR) { if (uop != OPR_NOUNOPR) {
int line = ls->linenumber;
luaX_next(ls); luaX_next(ls);
subexpr(ls, v, UNARY_PRIORITY); subexpr(ls, v, UNARY_PRIORITY);
luaK_prefix(ls->fs, uop, v); luaK_prefix(ls->fs, uop, v, line);
} }
else simpleexp(ls, v); else simpleexp(ls, v);
/* expand while operators have priorities higher than `limit' */ /* expand while operators have priorities higher than `limit' */
@ -904,11 +903,12 @@ static BinOpr subexpr (LexState *ls, expdesc *v, unsigned int limit) {
while (op != OPR_NOBINOPR && priority[op].left > limit) { while (op != OPR_NOBINOPR && priority[op].left > limit) {
expdesc v2; expdesc v2;
BinOpr nextop; BinOpr nextop;
int line = ls->linenumber;
luaX_next(ls); luaX_next(ls);
luaK_infix(ls->fs, op, v); luaK_infix(ls->fs, op, v);
/* read sub-expression with higher priority */ /* read sub-expression with higher priority */
nextop = subexpr(ls, &v2, priority[op].right); nextop = subexpr(ls, &v2, priority[op].right);
luaK_posfix(ls->fs, op, v, &v2); luaK_posfix(ls->fs, op, v, &v2, line);
op = nextop; op = nextop;
} }
leavelevel(ls); leavelevel(ls);
@ -1346,6 +1346,9 @@ static void retstat (LexState *ls) {
static int statement (LexState *ls) { static int statement (LexState *ls) {
int line = ls->linenumber; /* may be needed for error messages */ int line = ls->linenumber; /* may be needed for error messages */
switch (ls->t.token) { switch (ls->t.token) {
case ';': { /* stat -> <empty statement> */
return 0;
}
case TK_IF: { /* stat -> ifstat */ case TK_IF: { /* stat -> ifstat */
ifstat(ls, line); ifstat(ls, line);
return 0; return 0;