Example #1
0
        public LuaFile Compile(Ast.Chunk c, string name)
        {
            file                      = new LuaFile();
            block                     = new Block();
            block.Chunk.Name          = name;
            block.Chunk.ArgumentCount = 0;
            block.Chunk.Vararg        = 2;

            DoChunk(c);

            file.Main = block.Chunk;
            file.Main.ArgumentCount = 0;
            file.Main.Vararg        = 2;
            file.Main.UpvalueCount  = file.Main.Upvalues.Count;
            bool addRet = file.Main.Instructions.Count == 0;

            if (addRet == false)
            {
                addRet = file.Main.Instructions[file.Main.Instructions.Count - 1].Opcode != Instruction.LuaOpcode.RETURN;
            }
            if (addRet)
            {
                Instruction ret = new Instruction("RETURN");
                ret.A = 0;
                ret.B = 1;
                ret.C = 0;
                file.Main.Instructions.Add(ret);
            }
            return(file);
        }
Example #2
0
 public static void Main(string[] args)
 {
     while (true)
     {
         try
         {
             string            s     = Console.ReadLine();
             Lexer             l     = new Lexer();
             Parser            p     = new Parser(l.Lex(s));
             Ast.Chunk         c     = p.Parse();
             Compiler.Compiler cplr  = new SharpLua.Compiler.Compiler();
             LuaFile           proto = cplr.Compile(c, "<stdin>");
             Console.WriteLine("compiled!");
             FileStream fs = File.Open("out.sluac", FileMode.Create);
             foreach (char ch in proto.Compile())
             {
                 //Console.WriteLine(ch + " " + (int)ch);
                 fs.WriteByte((byte)ch);
             }
             fs.Close();
             Console.WriteLine("written to out.sluac!");
         }
         catch (Exception ex)
         {
             Console.WriteLine(ex.ToString());
         }
     }
     Console.Write("Press any key to continue . . . ");
     Console.ReadKey(true);
 }
Example #3
0
 void DoChunk(Ast.Chunk c, bool isloop = false)
 {
     DoChunk(c.Body, isloop);
 }