From fee4290fa17758f37b9bb7d3906f21e4406a3326 Mon Sep 17 00:00:00 2001 From: James Roseborough Date: Mon, 3 Sep 2012 05:41:38 +0000 Subject: [PATCH] Compiler for lua 5.2 fixes. --- src/core/org/luaj/vm2/compiler/FuncState.java | 27 ++++++++++--------- src/core/org/luaj/vm2/compiler/LexState.java | 21 +++++++++------ 2 files changed, 28 insertions(+), 20 deletions(-) diff --git a/src/core/org/luaj/vm2/compiler/FuncState.java b/src/core/org/luaj/vm2/compiler/FuncState.java index b48deb91..f8bddcb9 100644 --- a/src/core/org/luaj/vm2/compiler/FuncState.java +++ b/src/core/org/luaj/vm2/compiler/FuncState.java @@ -28,6 +28,7 @@ import org.luaj.vm2.LocVars; import org.luaj.vm2.Lua; import org.luaj.vm2.LuaNil; import org.luaj.vm2.LuaTable; +import org.luaj.vm2.LuaUserdata; import org.luaj.vm2.Prototype; import org.luaj.vm2.Upvaldesc; import org.luaj.vm2.LuaString; @@ -511,8 +512,11 @@ public class FuncState extends LuaC { return this.addk((b ? LuaValue.TRUE : LuaValue.FALSE)); } + static final LuaUserdata NIL_PROXY = new LuaUserdata(LuaValue.NIL); + 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) { @@ -674,19 +678,22 @@ public class FuncState extends LuaC { int exp2RK(expdesc e) { this.exp2val(e); switch (e.k) { - case LexState.VKNUM: case LexState.VTRUE: case LexState.VFALSE: case LexState.VNIL: { if (this.nk <= MAXINDEXRK) { /* constant fit in RK operand? */ e.u.info = (e.k == LexState.VNIL) ? this.nilK() - : (e.k == LexState.VKNUM) ? this.numberK(e.u.nval()) : this.boolK((e.k == LexState.VTRUE)); e.k = LexState.VK; return RKASK(e.u.info); } else break; } + case LexState.VKNUM: { + e.u.info = this.numberK(e.u.nval()); + e.k = LexState.VK; + /* go through */ + } case LexState.VK: { if (e.u.info <= MAXINDEXRK) /* constant fit in argC? */ return RKASK(e.u.info); @@ -767,21 +774,17 @@ public class FuncState extends LuaC { int pc; /* pc of last jump */ this.dischargevars(e); switch (e.k) { + case LexState.VJMP: { + this.invertjump(e); + pc = e.u.info; + break; + } case LexState.VK: case LexState.VKNUM: case LexState.VTRUE: { pc = LexState.NO_JUMP; /* always true; do nothing */ break; } - case LexState.VFALSE: { - pc = this.jump(); /* always jump */ - break; - } - case LexState.VJMP: { - this.invertjump(e); - pc = e.u.info; - break; - } default: { pc = this.jumponcond(e, 0); break; diff --git a/src/core/org/luaj/vm2/compiler/LexState.java b/src/core/org/luaj/vm2/compiler/LexState.java index fd3ec7fb..9ec1381f 100644 --- a/src/core/org/luaj/vm2/compiler/LexState.java +++ b/src/core/org/luaj/vm2/compiler/LexState.java @@ -402,7 +402,7 @@ public class LexState { while (true) { if (check_next(expo)) check_next("+-"); - if(isxdigit(current)) + if(isxdigit(current) || current == '.') save_and_next(); else break; @@ -699,7 +699,6 @@ public class LexState { lookahead.token = TK_EOS; /* and discharge it */ } else 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() { @@ -1034,7 +1033,7 @@ public class LexState { */ void breaklabel () { 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]); } @@ -1787,10 +1786,15 @@ public class LexState { this.block(); fs.leaveblock(); /* end of scope for declared variables */ fs.patchtohere(prep); - endfor = (isnum) ? fs.codeAsBx(Lua.OP_FORLOOP, base, NO_JUMP) : fs - .codeABC(Lua.OP_TFORLOOP, base, 0, nvars); - fs.fixline(line); /* pretend that `Lua.OP_FOR' starts the loop */ - fs.patchlist((isnum ? endfor : fs.jump()), prep + 1); + if (isnum) /* numeric for? */ + endfor = fs.codeAsBx(Lua.OP_FORLOOP, base, NO_JUMP); + else { /* generic for */ + 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; expdesc e = new expdesc(); int first, nret; /* registers with returned values */ - this.next(); /* skip RETURN */ if (block_follow(true) || this.t.token == ';') first = nret = 0; /* return no values */ else { @@ -2014,10 +2017,12 @@ public class LexState { } } fs.ret(first, nret); + testnext(';'); /* skip optional semicolon */ } void statement() { int line = this.linenumber; /* may be needed for error messages */ + enterlevel(); switch (this.t.token) { case ';': { /* stat -> ';' (empty statement) */ next(); /* skip ';' */