コード例 #1
0
ファイル: Program.cs プロジェクト: TheRiverElder/RSI
        public static void TestLexer()
        {
            string       s      = "(996 + 1.2 - 6 * (prev - 1) / prev - 3 * 5 * 13.6 / ext / k)";
            StringReader reader = new StringReader(s);
            TokenStream  ts     = new TokenStream(reader);

            ts.AddTokenizers(
                new IdTokenizer(),
                new DecimalTokenizer(),
                new DigitTokenizer(),
                new StringTokenizer(),
                new NameTokenizer()
                );

            Lexer lexer = CreateLexer();

            //TestLexerWithInput(lexer, ts, "a + b * c + d", true);
            //TestLexerWithInput(lexer, ts, "(a + b) * c + d", true);
            //TestLexerWithInput(lexer, ts, "a + (b * c + d)", true);
            //TestLexerWithInput(lexer, ts, "a + (b) * c + d", true);
            //TestLexerWithInput(lexer, ts, "a - ((b) * (c + d))", true);
            //TestLexerWithInput(lexer, ts, "a", true);
            //TestLexerWithInput(lexer, ts, "(a)", true);
            //TestLexerWithInput(lexer, ts, "a + b + * c", false);
            //TestLexerWithInput(lexer, ts, "+ a +", false);
            //TestLexerWithInput(lexer, ts, "+ a", false);
            //TestLexerWithInput(lexer, ts, "a + ", false);
            //TestLexerWithInput(lexer, ts, "a + + +", false);
            //TestLexerWithInput(lexer, ts, "a - (b) * (c + d))", false);
            //TestLexerWithInput(lexer, ts, "a - b) * (c + d", false);
            //TestLexerWithInput(lexer, ts, "/print 123", true);
            //TestLexerWithInput(lexer, ts, "/print", true);
            //TestLexerWithInput(lexer, ts, "/print a", true);
            //TestLexerWithInput(lexer, ts, "/print a a:45", true);
            //TestLexerWithInput(lexer, ts, "/print (23 + 9) 8 (45)", true);
            //TestLexerWithInput(lexer, ts, "/print (23 + 9) 8 a b:34", true);
            //TestLexerWithInput(lexer, ts, "/print 34 c:8 a b:34", true);
            //TestLexerWithInput(lexer, ts, "/print (23 + 9) 8 a b:34 c:(23 - 4)", true);
            //TestLexerWithInput(lexer, ts, "/print (23 + 9) 8 a b:34 c:23-4", true);
            //TestLexerWithInput(lexer, ts, "/print (23 + 9) 8 a b:34 5:23", false);
            //TestLexerWithInput(lexer, ts, "/print (23 + 9) 8 a b:34 5:23 d (5)", false);
            TestLexerWithInput(lexer, ts, "(23 + 9) * 5", true);

            string input;

            Console.Write(">>> ");
            while (!string.IsNullOrEmpty(input = Console.ReadLine()))
            {
                TestLexerWithInput(lexer, ts, input, true);
                Console.Write(">>> ");
            }
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: TheRiverElder/RSI
        public static void TestParser()
        {
            string       s      = "(996 + 1.2 - 6 * (prev - 1) / prev - 3 * 5 * 13.6 / ext / k)";
            StringReader reader = new StringReader(s);
            TokenStream  ts     = new TokenStream(reader);

            ts.AddTokenizers(
                new IdTokenizer(),
                new DecimalTokenizer(),
                new DigitTokenizer(),
                new StringTokenizer(),
                new NameTokenizer()
                );

            Parser parser = CreateProgramParser();

            NestedEnv       env = Buildin.CreateBuindinEnv();
            List <IASTNode> res = new List <IASTNode>();

            Console.Write(">>> ");
            while ((s = Console.ReadLine()) != ".exit")
            {
                ts.Reset(new StringReader(s));
                if (!parser.Parse(ts, res) || res.Count == 0)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("Parse failed");
                    Console.ResetColor();
                }
                else
                {
                    IASTNode node = res[0].Reduce();
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine("Parse succeeded");
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.WriteLine(node.ToString());
                    Console.ResetColor();
                    string result = node.GetValue(null, env).ToString();
                    Console.WriteLine();
                    Console.ForegroundColor = ConsoleColor.Blue;
                    Console.WriteLine("==> " + result);
                    Console.ResetColor();
                    //PrintAST(node);
                }
                res.Clear();
                Console.Write(">>> ");
            }
        }
コード例 #3
0
ファイル: Program.cs プロジェクト: TheRiverElder/RSI
        public static void TestTokenStream()
        {
            string       s      = "   123 123.456 778 \"Hello World!\" \"I said \\\"Hello World\\\"\" “我说“问世界好在\\””  999";
            StringReader reader = new StringReader(s);
            TokenStream  ts     = new TokenStream(reader);

            ts.AddTokenizers(
                new DecimalTokenizer(),
                new DigitTokenizer(),
                new StringTokenizer()
                );

            while (ts.HasMore)
            {
                Console.WriteLine(ts.Read());
            }
        }