コード例 #1
0
        private static void PrintSemanticCheck(Goal goal)
        {
            var globalTypeVistor = new GlobalTypeVistor();
            var typeVisitor      = new TypeCheckerVistor();

            // Clear globals from any past run.
            Globals.TypeTable.Clear();
            Globals.TypeUtilization.Clear();
            Globals.StringTable.Clear();
            Globals.Errors.Clear();

            // Run the semantic checker.
            goal.Accept(globalTypeVistor);
            goal.Accept(typeVisitor);

            // Print errors.
            if (Globals.Errors.Any())
            {
                WriteLineColor($"Found {Globals.Errors.Count} errors.", ConsoleColor.Red, ConsoleColor.Black);
                WriteLine();
                foreach (var e in Globals.Errors)
                {
                    WriteLineColor(e, ConsoleColor.Red, ConsoleColor.Black);
                }
                WriteLine();
            }
            else
            {
                WriteLineColor("No errors!", ConsoleColor.Green, ConsoleColor.Black);
                WriteLine();
            }

            WriteLineColor("Type Table", ConsoleColor.DarkYellow, ConsoleColor.Black);
            WriteLine();

            // Print type table.
            foreach (var c in Globals.TypeTable.EnumerateValues().Where(t => !t.IsPrimitive && t != Class.Unknown).Cast <Class>())
            {
                WriteLine($"{c.Name} : {c.BaseClass?.Name ?? "void"}{(c.IsMainClass ? " (main)" : "")} [{c.DeclarationLocation.StartLine}, {c.DeclarationLocation.StartColumn}]");

                WriteLine(" Properties");
                foreach (var prop in c.Properties.EnumerateValues())
                {
                    WriteLine($"  {prop.Name}: {prop.RealizedType.Name} [{prop.DeclarationLocation.StartLine}, {prop.DeclarationLocation.StartColumn}]");
                }

                WriteLine(" Methods");
                foreach (var method in c.Methods.EnumerateValues())
                {
                    WriteLine($"  {method.Name}: {method.RealizedReturnType.Name} [{method.DeclarationLocation.StartLine}, {method.DeclarationLocation.StartColumn}]");

                    WriteLine("   Arguments");
                    foreach (var arg in method.Arguments.EnumerateValues())
                    {
                        WriteLine($"    {arg.Name}: {arg.RealizedType.Name} [{arg.DeclarationLocation.StartLine}, {arg.DeclarationLocation.StartColumn}]");
                    }
                }
            }
        }
コード例 #2
0
        private static void Build(Goal goal, string fileName)
        {
            var globalTypeVistor   = new GlobalTypeVistor();
            var typeVisitor        = new TypeCheckerVistor();
            var instructionVisitor = new InstructionVisitor();

            // Clear globals from any past run.
            Globals.TypeTable.Clear();
            Globals.TypeUtilization.Clear();
            Globals.StringTable.Clear();
            Globals.Errors.Clear();

            // Run the semantic checker.
            goal.Accept(globalTypeVistor);
            goal.Accept(typeVisitor);

            // Print errors.
            if (Globals.Errors.Any())
            {
                WriteLineColor($"Found {Globals.Errors.Count} errors.", ConsoleColor.Red, ConsoleColor.Black);
                WriteLine();
                foreach (var e in Globals.Errors)
                {
                    WriteLineColor(e, ConsoleColor.Red, ConsoleColor.Black);
                }
                WriteLine();
                return;
            }

            WriteLineColor("No errors!", ConsoleColor.Green, ConsoleColor.Black);
            WriteLine();

            Globals.Builder = new Builder(fileName);
            goal.Accept(instructionVisitor);
            Globals.Builder.BuildAndRun();
        }