public static void TextCalculator() { SimpleCalculator calculator = new SimpleCalculator(); string script = "int a = b+3"; Console.WriteLine("解析变量声明语句:" + script); SimpleLexer lexer = new SimpleLexer(); TokenReader tokens = lexer.Tokenize(script); try { SimpleASTNode node = calculator.InitDeclare(tokens); calculator.DumpAST(node, ""); } catch (Exception ex) { Console.WriteLine(ex.Message); } // script = "2 + 3*5"; //Console.WriteLine("\n计算:" + script + ",看上去一切正常"); script = "2 + 3 + 4"; calculator.Evaluate(script); }
/// <summary> /// 解析脚本,并返回根节点 /// </summary> /// <param name="code"></param> /// <returns></returns> public ASTNode parse(string code) { SimpleLexer lexer = new SimpleLexer(); TokenReader tokens = lexer.Tokenize(code); ASTNode rootNode = Prog(tokens); return(rootNode); }
private static void TestLexer() { SimpleLexer lexer = new SimpleLexer(); string script = "int age = 45;"; SimpleTokenReader tokenReader = lexer.Tokenize(script); Dump(tokenReader); script = "inta age = 45;"; tokenReader = lexer.Tokenize(script); Dump(tokenReader); Console.ReadKey(); }