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); } }