Exemplo n.º 1
0
        private void Parse(string text, bool verbose = false)
        {
            var lexer = new PiLexer(text);

            Assert.IsTrue(lexer.Process());
            if (verbose)
            {
                WriteLine(lexer.ToString());
            }
            if (lexer.Failed)
            {
                WriteLine(lexer.Error);
            }
            PiParser = new PiParser(lexer);
            PiParser.Process(lexer, EStructure.None);
            if (verbose)
            {
                WriteLine(PiParser.PrintTree());
            }
            if (PiParser.Failed)
            {
                Debug.WriteLine(PiParser.Error);
            }
            Assert.IsFalse(PiParser.Failed);
            Assert.IsNotNull(PiParser.Root);
        }
Exemplo n.º 2
0
        public void TestSimpleTokens()
        {
            var lexer = new PiLexer("1 2 3");

            Assert.IsTrue(lexer.Process());
            var parser = new PiParser(lexer);

            Assert.IsTrue(parser.Process(lexer));
        }
Exemplo n.º 3
0
        private bool Pi()
        {
            var input = Current().Text;
            var lexer = new PiLexer(input);

            if (!lexer.Process())
            {
                return(FailLocation(lexer.Error));
            }

            var parser = new PiParser(lexer);

            if (!parser.Process(lexer))
            {
                return(FailLocation(parser.Error));
            }

            var pi = NewNode(Consume());

            pi.Value = parser.Root;
            Push(pi);
            return(true);
        }
Exemplo n.º 4
0
        static void Main(string[] args)
        {
            string src = File.ReadAllText("./src.pi");

            var lexer = new PiLexer(src);
            var l     = lexer.Lex();

            foreach (var error in lexer.Errors)
            {
                Console.WriteLine("{0} at line {1}, column {2}", error.Message, error.Location.Line, error.Location.Column);
            }

            if (l != null)
            {
                int prevLine = 0;

                foreach (var item in l)
                {
                    if (item.Begin.Line != prevLine)
                    {
                        Console.WriteLine();
                        Console.WriteLine("Line " + item.Begin.Line);
                        prevLine = item.Begin.Line;
                    }

                    Console.WriteLine("{0,-15}: \"{1}\"", item.Kind, item.Content);
                }
            }
            else
            {
                goto exit;
            }

            var parser = new PiParser();

            IEnumerable <Node> decl = parser.Parse(l);

            if (Debugger.IsAttached)
            {
                decl = decl.ToArray();
            }
            else
            {
                try
                {
                    decl = decl.ToArray();
                }
                catch (SyntaxException ex)
                {
                    var line     = src.Lines()[ex.Location.Line];
                    int tabCount = line.TakeWhile(o => o == '\t').Count();
                    line = new string(' ', tabCount) + line.TrimStart('\t');

                    Console.WriteLine();
                    Console.WriteLine(ex.Message);
                    Console.WriteLine(line);
                    Console.WriteLine(new string(' ', ex.Location.Column - 1) + "^");

                    goto exit;
                }
            }

            string dump = ObjectDumper.Dump(decl, DumpStyle.Console);

            Console.WriteLine(dump);
            File.WriteAllText("./dump.txt", dump);

            var inter = new PiInterpreter(decl);

            inter.Run();

            Console.WriteLine();
            Console.WriteLine("Done!");

exit:
            Console.ReadKey(true);
        }