예제 #1
0
        public static void Main(string[] args)
        {
            SimpleCalculator calculator = new SimpleCalculator();

            String script = "int a = b + 3;";

            Console.WriteLine("解析变量声明语句:" + script);
            SimpleLexer  lexer  = new SimpleLexer();
            ITokenReader tokens = lexer.tokenize(script);

            try
            {
                SimpleASTNode node = calculator.intDeclare(tokens);
                calculator.dumpAST(node, "");
            }catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            script = "2+3*5";
            Console.WriteLine("\n计算:{0}", script);
            calculator.evaluate(script);

            script = "2+";
            Console.WriteLine("\n计算:{0}", script);
            calculator.evaluate(script);

            script = "2+3+4*2+10+10/5";
            Console.WriteLine("\n计算:{0}", script);
            calculator.evaluate(script);

            Console.ReadKey();
        }
예제 #2
0
        /*
         * 解析脚本,并返回根节点
         * @param code
         */
        public IASTNode parse(string code)
        {
            SimpleLexer  lexer  = new SimpleLexer();
            ITokenReader tokens = lexer.tokenize(code);

            IASTNode rootNode = prog(tokens);

            return(rootNode);
        }