internal static ObjLuaCode Unserialize(System.IO.BinaryReader reader, ObjLuaFunction parent) { uint val = reader.ReadUInt32(); ObjLuaCode ret = ObjLuaCode.CreateOperator(val, parent); return(ret); }
public static ObjLuaCode CreateOperator(uint val, ObjLuaFunction parent) { byte oc = GetOpCode(val, parent); Type t = GetOpcodeType(oc); ObjLuaCode ret = (ObjLuaCode)System.Activator.CreateInstance(t, new object[] { val, parent }); return(ret); }
internal bool IsLocalRegister(ushort regnr, Lua.Context cx) { ObjLuaCode line = null; for (int i = cx.PC + 1; i < codes.Count; i++) { line = codes[i] as ObjLuaCode; if (line != null) { if (line is Lua.ILoadOperator) { Lua.ILoadOperator lop = line as Lua.ILoadOperator; if (lop.LoadsRegister(regnr)) { return(false); } } } } return(true); }
void PrintLine(ArrayList sw, Lua.Context cx, ObjLuaCode line, string plusindent) { if (line != null) { string content = ""; if (line is Lua.IOperator) { Lua.IOperator lop = line as Lua.IOperator; if (ObjLuaFunction.DEBUG) { content = lop.ToString(cx) + " #" + line.GetType().Name; } else { content = lop.ToString(cx); } lop.Run(cx); } else { content = line.ToString(); } if (content.Trim() != "") { if (ObjLuaFunction.DEBUG) { sw.Add(Helper.HexString(cx.PC) + ": " + plusindent + cx.Indent + content); } else { sw.Add(plusindent + cx.Indent + content); } } } }
internal void Unserialize(System.IO.BinaryReader reader) { contants.Clear(); functions.Clear(); codes.Clear(); srcln.Clear(); local.Clear(); upval.Clear(); name = ObjLua.ReadString(reader); linedef = reader.ReadUInt32(); nups = reader.ReadByte(); argc = reader.ReadByte(); isinout = reader.ReadByte(); stacksz = reader.ReadByte(); uint linenfosz = reader.ReadUInt32(); for (uint i = 0; i < linenfosz; i++) { ObjLuaSourceLine item = new ObjLuaSourceLine(this); item.Unserialize(reader); srcln.Add(item); } uint locvarsz = reader.ReadUInt32(); for (uint i = 0; i < locvarsz; i++) { ObjLuaLocalVar item = new ObjLuaLocalVar(this); item.Unserialize(reader); local.Add(item); } uint upvalsz = reader.ReadUInt32(); for (uint i = 0; i < upvalsz; i++) { ObjLuaUpValue item = new ObjLuaUpValue(this); item.Unserialize(reader); upval.Add(item); } uint constsz = reader.ReadUInt32(); for (uint i = 0; i < constsz; i++) { ObjLuaConstant item = new ObjLuaConstant(this); item.Unserialize(reader); contants.Add(item); } uint fncsz = reader.ReadUInt32(); for (uint i = 0; i < fncsz; i++) { ObjLuaFunction item = new ObjLuaFunction(this.parent); item.Unserialize(reader); functions.Add(item); } uint codesz = reader.ReadUInt32(); for (uint i = 0; i < codesz; i++) { ObjLuaCode item = ObjLuaCode.Unserialize(reader, this); codes.Add(item); } }
internal void ToSource(System.IO.StreamWriter writer, Lua.Context cx) { cx.Init(this); ObjLuaCode line = null; ArrayList endline = new ArrayList(); ArrayList elseline = new ArrayList(); ArrayList oplines = new ArrayList(); ArrayList sw = new ArrayList(); string pindent = ""; for (int i = 0; i < codes.Count - 1; i++) { oplines.Add(sw.Count); cx.GoToLine(i); line = cx.CurrentLine; //for loop check if (line is Lua.SUB) { int pc = cx.PC; ObjLuaCode nline = cx.NextLine(); if (nline is Lua.JMP) { cx.GoToLine(cx.PC + nline.SBX + 1); ObjLuaCode fline = cx.CurrentLine; if (fline is Lua.FORLOOP) { cx.GoToLine(pc); Lua.FORLOOP fl = fline as Lua.FORLOOP; fl.IsStart = true; PrintLine(sw, cx, fline, pindent); fl.IsStart = false; continue; } } cx.GoToLine(pc); } if (line is Lua.IAddEnd) { Lua.IAddEnd end = line as Lua.IAddEnd; endline.Add(cx.PC + end.Offset); } if (line is Lua.IIfOperator) { AddIndent(ref pindent); int pc = cx.PC; ObjLuaCode oline = line; line = cx.NextLine(); int ifblsz = line.SBX; if (ifblsz < 0) //while block { int npc = (int)oplines[oplines.Count + line.SBX + 1]; for (int id = npc; id < sw.Count; id++) { sw[id] = "\t" + sw[id].ToString(); } oline.A = (ushort)Math.Abs(oline.A - 1); sw.Insert(npc, pindent + "while " + ((Lua.IOperator)oline).ToString(cx).Replace("if ", "").Replace(" then", "") + " do"); oline.A = (ushort)Math.Abs(oline.A - 1); this.BackIndent(ref pindent); PrintLine(sw, cx, new Lua.TextLine(0, this, "end"), pindent); continue; } else { cx.PrepareJumpToLine(cx.PC + ifblsz); line = cx.NextLine(); if (line is Lua.JMP) //having an else Block { elseline.Add(cx.PC - 1); endline.Add(cx.PC + line.SBX); } else { endline.Add(cx.PC); } } cx.GoToLine(pc); line = cx.CurrentLine; } PrintLine(sw, cx, line, pindent); if (line is Lua.TFORREP) { int pc = cx.PC; ObjLuaCode eline = line; while (!(eline is Lua.TFORLOOP)) { eline = cx.NextLine(); } ((Lua.TFORLOOP)eline).Setup(cx); cx.GoToLine(pc); ((Lua.TFORREP)line).TFORLOOP = eline as Lua.TFORLOOP; PrintLine(sw, cx, line, pindent); ((Lua.TFORREP)line).TFORLOOP = null; } while (endline.Contains(i)) { BackIndent(ref pindent); sw.Add(pindent + cx.Indent + "end"); endline.Remove(i); } if (elseline.Contains(i)) { BackIndent(ref pindent); sw.Add(pindent + cx.Indent + "else"); AddIndent(ref pindent); } } foreach (string ln in sw) { writer.WriteLine(ln); } }