Compiler for lua 5.2 fixes.

This commit is contained in:
James Roseborough
2012-09-03 05:41:38 +00:00
parent f396d8b39e
commit fee4290fa1
2 changed files with 28 additions and 20 deletions

View File

@@ -28,6 +28,7 @@ import org.luaj.vm2.LocVars;
import org.luaj.vm2.Lua; import org.luaj.vm2.Lua;
import org.luaj.vm2.LuaNil; import org.luaj.vm2.LuaNil;
import org.luaj.vm2.LuaTable; import org.luaj.vm2.LuaTable;
import org.luaj.vm2.LuaUserdata;
import org.luaj.vm2.Prototype; import org.luaj.vm2.Prototype;
import org.luaj.vm2.Upvaldesc; import org.luaj.vm2.Upvaldesc;
import org.luaj.vm2.LuaString; import org.luaj.vm2.LuaString;
@@ -511,8 +512,11 @@ public class FuncState extends LuaC {
return this.addk((b ? LuaValue.TRUE : LuaValue.FALSE)); return this.addk((b ? LuaValue.TRUE : LuaValue.FALSE));
} }
static final LuaUserdata NIL_PROXY = new LuaUserdata(LuaValue.NIL);
int nilK() { int nilK() {
return this.addk(LuaValue.NIL); /* cannot use nil as key; instead use table itself to represent nil */
return this.addk(NIL_PROXY);
} }
void setreturns(expdesc e, int nresults) { void setreturns(expdesc e, int nresults) {
@@ -674,19 +678,22 @@ public class FuncState extends LuaC {
int exp2RK(expdesc e) { int exp2RK(expdesc e) {
this.exp2val(e); this.exp2val(e);
switch (e.k) { switch (e.k) {
case LexState.VKNUM:
case LexState.VTRUE: case LexState.VTRUE:
case LexState.VFALSE: case LexState.VFALSE:
case LexState.VNIL: { case LexState.VNIL: {
if (this.nk <= MAXINDEXRK) { /* constant fit in RK operand? */ if (this.nk <= MAXINDEXRK) { /* constant fit in RK operand? */
e.u.info = (e.k == LexState.VNIL) ? this.nilK() e.u.info = (e.k == LexState.VNIL) ? this.nilK()
: (e.k == LexState.VKNUM) ? this.numberK(e.u.nval())
: this.boolK((e.k == LexState.VTRUE)); : this.boolK((e.k == LexState.VTRUE));
e.k = LexState.VK; e.k = LexState.VK;
return RKASK(e.u.info); return RKASK(e.u.info);
} else } else
break; break;
} }
case LexState.VKNUM: {
e.u.info = this.numberK(e.u.nval());
e.k = LexState.VK;
/* go through */
}
case LexState.VK: { case LexState.VK: {
if (e.u.info <= MAXINDEXRK) /* constant fit in argC? */ if (e.u.info <= MAXINDEXRK) /* constant fit in argC? */
return RKASK(e.u.info); return RKASK(e.u.info);
@@ -767,21 +774,17 @@ public class FuncState extends LuaC {
int pc; /* pc of last jump */ int pc; /* pc of last jump */
this.dischargevars(e); this.dischargevars(e);
switch (e.k) { switch (e.k) {
case LexState.VJMP: {
this.invertjump(e);
pc = e.u.info;
break;
}
case LexState.VK: case LexState.VK:
case LexState.VKNUM: case LexState.VKNUM:
case LexState.VTRUE: { case LexState.VTRUE: {
pc = LexState.NO_JUMP; /* always true; do nothing */ pc = LexState.NO_JUMP; /* always true; do nothing */
break; break;
} }
case LexState.VFALSE: {
pc = this.jump(); /* always jump */
break;
}
case LexState.VJMP: {
this.invertjump(e);
pc = e.u.info;
break;
}
default: { default: {
pc = this.jumponcond(e, 0); pc = this.jumponcond(e, 0);
break; break;

View File

@@ -402,7 +402,7 @@ public class LexState {
while (true) { while (true) {
if (check_next(expo)) if (check_next(expo))
check_next("+-"); check_next("+-");
if(isxdigit(current)) if(isxdigit(current) || current == '.')
save_and_next(); save_and_next();
else else
break; break;
@@ -699,7 +699,6 @@ public class LexState {
lookahead.token = TK_EOS; /* and discharge it */ lookahead.token = TK_EOS; /* and discharge it */
} else } else
t.token = llex(t.seminfo); /* read next token */ t.token = llex(t.seminfo); /* read next token */
System.out.println("----- next t.token " + t.token + " (" + (t.token!=0? txtToken(t.token): "-")+") "+ linenumber );
} }
void lookahead() { void lookahead() {
@@ -1034,7 +1033,7 @@ public class LexState {
*/ */
void breaklabel () { void breaklabel () {
LuaString n = LuaString.valueOf("break"); LuaString n = LuaString.valueOf("break");
int l = newlabelentry(LuaC.grow(dyd.label, dyd.n_label+1), dyd.n_label++, n, 0, fs.pc); int l = newlabelentry(dyd.label=LuaC.grow(dyd.label, dyd.n_label+1), dyd.n_label++, n, 0, fs.pc);
findgotos(dyd.label[l]); findgotos(dyd.label[l]);
} }
@@ -1787,10 +1786,15 @@ public class LexState {
this.block(); this.block();
fs.leaveblock(); /* end of scope for declared variables */ fs.leaveblock(); /* end of scope for declared variables */
fs.patchtohere(prep); fs.patchtohere(prep);
endfor = (isnum) ? fs.codeAsBx(Lua.OP_FORLOOP, base, NO_JUMP) : fs if (isnum) /* numeric for? */
.codeABC(Lua.OP_TFORLOOP, base, 0, nvars); endfor = fs.codeAsBx(Lua.OP_FORLOOP, base, NO_JUMP);
fs.fixline(line); /* pretend that `Lua.OP_FOR' starts the loop */ else { /* generic for */
fs.patchlist((isnum ? endfor : fs.jump()), prep + 1); fs.codeABC(Lua.OP_TFORCALL, base, 0, nvars);
fs.fixline(line);
endfor = fs.codeAsBx(Lua.OP_TFORLOOP, base + 2, NO_JUMP);
}
fs.patchlist(endfor, prep + 1);
fs.fixline(line);
} }
@@ -1990,7 +1994,6 @@ public class LexState {
FuncState fs = this.fs; FuncState fs = this.fs;
expdesc e = new expdesc(); expdesc e = new expdesc();
int first, nret; /* registers with returned values */ int first, nret; /* registers with returned values */
this.next(); /* skip RETURN */
if (block_follow(true) || this.t.token == ';') if (block_follow(true) || this.t.token == ';')
first = nret = 0; /* return no values */ first = nret = 0; /* return no values */
else { else {
@@ -2014,10 +2017,12 @@ public class LexState {
} }
} }
fs.ret(first, nret); fs.ret(first, nret);
testnext(';'); /* skip optional semicolon */
} }
void statement() { void statement() {
int line = this.linenumber; /* may be needed for error messages */ int line = this.linenumber; /* may be needed for error messages */
enterlevel();
switch (this.t.token) { switch (this.t.token) {
case ';': { /* stat -> ';' (empty statement) */ case ';': { /* stat -> ';' (empty statement) */
next(); /* skip ';' */ next(); /* skip ';' */