コード例 #1
0
        static void Main(string[] args)
        {
            Application.EnableVisualStyles();

            var path = "Tests/valid.ql";

            if (args.Length > 0)
            {
                path = args[0];
            }

            if (!File.Exists(path))
            {
                Console.WriteLine($"ABRT\tCannot open {path}");
                return;
            }

            Console.WriteLine($"INFO\tPath: {path}");

            // Parse form.
            var tree = new AstBuilder().BuildFromPath(path);

            // Print code.
            var source = new PrintSource().Visit(tree);

            Console.WriteLine("INFO\tCode listing");
            Console.WriteLine("---------------------------------");
            Console.WriteLine(source);
            Console.WriteLine("---------------------------------");

            // Validate and check.
            var questionTrav    = new QuestionInventory();
            var questionResults = questionTrav.Visit(tree);

            if (!questionTrav.Continue())
            {
                return;
            }

            // Perform type checking, use previously determined questions to type mapping.
            var typeCheck = new TypeChecker(questionResults.QuestionsWithTypes);

            typeCheck.Visit(tree);
            if (!typeCheck.Continue())
            {
                return;
            }

            // Build evaluator with list of tree to UI bindings. Use default factory.
            var    widgetFactory = new WidgetFactory();
            var    bindings      = new CreateBindings(widgetFactory).Visit(tree);
            var    evaluator     = new Evaluator(bindings, questionResults.ComputedQuestions.ToList());
            Action refresh       = () => evaluator.Visit(tree);

            // Open window and run!
            var window = new QuestionaireContainer(tree.Name, bindings, refresh);

            Application.Run(window);
        }
コード例 #2
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
        }