コード例 #1
0
ファイル: Program.cs プロジェクト: DOML-Lang/DOML.net
        public static void Main(string[] args)
        {
            if (Directory.Exists(Directory.GetCurrentDirectory() + "/StaticBindings") == false)
            {
                Directory.CreateDirectory(Directory.GetCurrentDirectory() + "/StaticBindings");
            }

            CreateBindings.DirectoryPath = Directory.GetCurrentDirectory() + "/StaticBindings";
            CreateBindings.Create <Color>();
            CreateBindings.CreateFinalFile();

            //DOML.LinkBindings();
            Console.SetWindowSize((int)(Console.LargestWindowWidth / 1.5f), (int)(Console.LargestWindowHeight / 1.5f));
            Console.OutputEncoding = System.Text.Encoding.UTF8;
            using (StreamReader reader = new StreamReader(File.OpenRead("test.doml"))) {
                int c = reader.Read();
                while (c > 0)
                {
                    Console.Write((char)c);
                    c = reader.Read();
                }
            }
            TopLevelNode topLevel = new Parser().ParseAST(new StreamReader(File.OpenRead("test.doml")));

            topLevel.BasicCodegen(Console.Out, false);
            topLevel.BasicCodegen(Console.Out, true);
            foreach (Instruction instruction in topLevel.GetInstructions())
            {
                Console.Write(instruction.OpCode);
                foreach (object obj in instruction.Parameters)
                {
                    Console.Write(" " + obj);
                }
                Console.WriteLine();
            }

            topLevel.Print(Console.Out);
            Console.Read();
            return;

#if BenchmarkDotNet
            //Summary summary = BenchmarkRunner.Run<AllTests>();
            //Console.Read();
#elif StaticBindings
            if (Directory.Exists(Directory.GetCurrentDirectory() + "/StaticBindings") == false)
            {
                Directory.CreateDirectory(Directory.GetCurrentDirectory() + "/StaticBindings");
            }

            CreateBindings.DirectoryPath = Directory.GetCurrentDirectory() + "/StaticBindings";
            CreateBindings.Create <Color>("System");
            CreateBindings.CreateFinalFile();
#elif TestBindings
            DOMLBindings.LinkBindings();


            Interpreter interpreter = Parser.GetInterpreterFromText(@"
            // This is a comment
            // Construct a new System.Color
            @ Test        = System.Color ...
            ;             .RGB             = 255, 64, 128 // Implicit 'array'

            @ TheSame     = System.Color ...
            ;             .RGB->Normalised = 1, 0.25, 0.5, // You can have trailing commas

            @ AgainSame   = System.Color ...
            ;             .RGB->Hex        = 0xFF4080
            ;             .Name            = ""OtherName""

            /* Multi Line Comment Blocks are great */
            @ Copy = System.Color...
            ;             .RGB = Test.R, Test.G, Test.B
            ;             .Name = ""Copy""
            ");

            string withComments;
            string withoutComments;

            IRWriter.EmitToLocation(interpreter, Directory.GetCurrentDirectory() + "/CompactOutput.IR", false, false);
            IRWriter.EmitToLocation(interpreter, Directory.GetCurrentDirectory() + "/Output.IR", false, true);

            StringBuilder builder = new StringBuilder();

            IRWriter.EmitToString(interpreter, builder, true);
            withComments = builder.ToString();
            builder.Clear();
            IRWriter.EmitToString(interpreter, builder, false);
            withoutComments = builder.ToString();

            Interpreter c;

            using (StringReader reader = new StringReader(withComments))
            {
                c = Parser.GetInterpreterFromIR(reader);
                c.HandleSafeInstruction(new Instruction());
            }

            using (StringReader reader = new StringReader(withoutComments))
            {
                c = Parser.GetInterpreterFromIR(reader);
                c.HandleSafeInstruction(new Instruction());
            }

            Console.Read();

            return;

            TestDOML.RunStringTest(@"
            // This is a comment
            // Construct a new System.Color
            @ Test        = System.Color ...
            ;             .RGB             = 255, 64, 128 // Implicit 'array'

            @ TheSame     = System.Color ...
            ;             .RGB->Normalised = 1, 0.25, 0.5, // You can have trailing commas

            @ AgainSame   = System.Color ...
            ;             .RGB->Hex        = 0xFF4080
            ;             .Name            = ""OtherName""

            /* Multi Line Comment Blocks are great */
            @ Copy = System.Color...
            ;             .RGB = Test.R, Test.G, Test.B
            ;             .Name = ""Copy""
            ", 5, Config.READ_EMIT);

            Console.Write(Log.HandleLogs);

            Console.Read();
#else
            Log.LogHandler += Log_LogHandler;

            Action <InterpreterRuntime> Set_RGB = (InterpreterRuntime runtime) =>
            {
                if (runtime.Pop(out Colour result))
                {
                    // Handle
                    if (!runtime.Pop(out result.B) || !runtime.Pop(out result.G) || !runtime.Pop(out result.R))
                    {
                        Log.Error("Pops failed");
                        return;
                    }

                    result.R /= 255;
                    result.G /= 255;
                    result.B /= 255;
                }
            };

            Action <InterpreterRuntime> Get_RGB = (InterpreterRuntime runtime) =>
            {
                if (!runtime.Pop(out Colour result) || !runtime.Push(result.R, true) || !runtime.Push(result.G, true) || !runtime.Push(result.B, true))
                {
                    Log.Error("Pushes failed");
                }
            };

            InstructionRegister.RegisterConstructor("System.Color", (InterpreterRuntime runtime) =>
            {
                Colour colour = new Colour();
                Colours.Add(colour);
                if (!runtime.Push(colour, true))
                {
                    Log.Error("Creation failed");
                }
            });

            InstructionRegister.RegisterSetter("RGB", "System.Color", 3, Set_RGB);
            InstructionRegister.RegisterGetter("RGB", "System.Color", 3, Get_RGB);
            InstructionRegister.RegisterSetter("RGB.Normalised", "System.Color", 3, (InterpreterRuntime runtime) =>
            {
                if (runtime.Pop(out Colour result))
                {
                    if (!runtime.Pop(out result.B) || !runtime.Pop(out result.G) || !runtime.Pop(out result.R))
                    {
                        Log.Error("Pops failed");
                    }
                }
            });

            Parser.GetInterpreterFromText(@"
                @ Test = System.Color ... // Comment
                ;      .RGB(Normalised) = 0.5, 0.25, 0.1,
                ").Execute(true);//Emit(Directory.GetCurrentDirectory() + "/Test.doml", true, true);

            /*TestDOML.RunStringTest(@"
             * @ Test = System.Color ... // Comment
             * ;      .RGB(Normalised) = 0.5, 0.25, 0.1,
             * ", 100, Config.ALL);*/

            Colours.ForEach(y => Log.Info($"Colour RGB: {y.R} {y.G} {y.B}"));

            Console.Read();
#endif
        }