public void AddError(string message, SourceCode sourceCode, Severity severity, SourceSpan span)
 {
     errors.Add(new ErrorEntry(message, sourceCode.GetLines(span.Start.Line, span.End.Line), severity, span));
 }
Example #2
0
        static void Main(string[] args)
        {
            Lexical lexical = new Lexical();
            Parsers parser  = new Parsers();

            string code = "func main()\n" +
                          "{\n" +
                          "photo a;\n" +
                          "photo b;\n" +
                          "photo c;\n" +
                          "a = \"test1.png\";\n" +
                          "b = \"test2.png\";\n" +
                          "c = \"test1.png\";\n" +
                          "read(c);\n" +
                          "switch(c)\n" +
                          "{\n" +
                          "case a: print(a);\n" +
                          "case b: print(b);\n" +
                          "}\n" +
                          "}\n" +
                          "main();\n";

            Console.WriteLine(code);
            int count      = 0;
            var sourceCode = new SourceCode(code);
            var tokens     = lexical.LexFile(sourceCode).ToArray();

            foreach (var token in tokens)
            {
                if (token.Kind == TokenKind.Error)
                {
                    count++;
                }
                // Console.WriteLine($"line {token.Span.Start.Line} {token.Kind} ( \"{token.Value}\" ) "); //column {token.Span.Start.Column}-{token.Span.End.Column}
            }


            if (lexical.ErrorSink.Any() || count > 0)
            {
                Console.WriteLine($"\nLexer\n");
                Console.WriteLine($"Error");

                foreach (var error in lexical.ErrorSink)
                {
                    Console.WriteLine(new string('-', Console.WindowWidth / 3));

                    WriteError(error);
                }
                Console.WriteLine(new string('-', Console.WindowWidth / 2));
                lexical.ErrorSink.Clear();
            }
            else
            {
                parser.ParseFile(sourceCode, tokens);

                Semantic.Semantic semantic = new Semantic.Semantic(parser);
                semantic.AnalyzeFile();


                if (lexical.ErrorSink.Any())
                {
                    Console.WriteLine($"\nSyntax\n");
                    foreach (var error in lexical.ErrorSink)
                    {
                        Console.WriteLine(new string('-', Console.WindowWidth / 3));

                        WriteError(error);
                    }
                    Console.WriteLine(new string('-', Console.WindowWidth / 2));
                    lexical.ErrorSink.Clear();
                }

                else if (semantic.errors.Count > 0)
                {
                    Console.WriteLine($"\nSemantic\n");
                    var err = semantic.errors.Distinct();
                    foreach (var e in err)
                    {
                        Console.WriteLine($"{e}");
                    }
                    Console.WriteLine(new string('-', Console.WindowWidth / 2));
                }
                else
                {
                    Interpreter interpreter = new Interpreter(parser);
                    Console.WriteLine($"\nOutput\n");
                    interpreter.CreateCode();
                }
            }
            Console.ReadKey();
        }